2020-05-15 16:28:17 +02:00
# @sapphirecode/crypto-helper
2020-03-06 08:40:58 +01:00
2023-04-04 21:52:10 +02:00
version: 2.1.x
2020-05-15 16:28:17 +02:00
simple functions for cryptography
## Installation
npm:
> npm i --save @sapphirecode/crypto-helper
yarn:
2021-01-06 15:13:04 +01:00
> yarn add @sapphirecode/crypto-helper
2020-03-06 08:40:58 +01:00
## Usage
2021-01-06 15:13:04 +01:00
### Examples
2020-03-06 08:40:58 +01:00
```js
2020-05-17 18:56:46 +02:00
const crypto = require('@sapphirecode/crypto -helper');
2020-03-06 08:40:58 +01:00
const rand_hex = crypto.random_hex(16); // outputs 16 byte random hex
const rand_salt = crypto.create_salt(); // same as random_hex, but with fixed length of 32 bytes
2020-05-15 16:28:17 +02:00
const random_string = crypto.random_string(16); // output 16 character long random string
2020-03-06 08:40:58 +01:00
const hash = crypto.hash_sha512(random_string, random_hex); // returns sha 512 hex
const check = crypto.checksum('foo'); // returns a sha 256 hex
2023-04-04 21:52:10 +02:00
const scrypt_hash = await crypto.pbkdf_scrypt('foo', 'bar'); // returns a scrypt hash
2020-03-06 08:40:58 +01:00
// jwt like object signing
const signed = crypto.sign_object({foo: 'bar'}, 'secret');
2022-08-08 13:07:06 +02:00
const info = await crypto.get_signature_info(signed); // returns an object with iat (issued at), key_info and data
const dec = await crypto.decode_signed(signed); // decode a signed object without verifying the signature
const ver = await crypto.verify_signature(signed, 'secret', 10000); // verifies the signature and returns the contents. the timeout is in milliseconds and optional, timing will be ignored if omitted.
const ver_info = await crypto.verify_signature_get_info(signed, 'secret', 10000); // verify a signature and get signature information like iat and key_info
const ver_func = await crypto.verify_signature(
2021-01-06 15:13:04 +01:00
signed,
(signature_info) => 'secret',
10000
); // verify a signature, retrieve the key using the signature info
2020-05-15 16:28:17 +02:00
// encryption
const enc = crypto.encrypt_aes('foo', 'bar');
const dec = crypto.decrypt_aes(enc, 'bar');
2021-01-06 15:13:04 +01:00
// asymmetric encryption and signatures
const keys = await crypto.generate_keypair(2048); // generate private and public key (length is optional and 2048 by default)
const aenc = crypto.asym_encrypt('foo', keys.public_key); // encrypt
const adec = crypto.asym_decrypt(aenc, key.private_key); // decrypt
const asig = crypto.asym_sign('foo', keys.private_key); // create signature
const aver = crypto.asym_verify('foo', keys.public_key, asig); // verify signature, returns boolean
2020-03-06 08:40:58 +01:00
```
2020-05-15 16:28:17 +02:00
2021-01-06 15:13:04 +01:00
### Asymmetric signatures on object signing
the functions `sign_object` , `verify_signature` , ... will automatically detect
rsa keys and use them to sign objects asymmetrically. Note that keys have to be
provided in the correct order (private key for signing, public key for
verifying). Else the keys will just be interpreted as symmetric and verification
will fail.
2020-05-15 16:28:17 +02:00
## License
MIT © Timo Hocker < timo @scode .ovh >