crypto-helper/README.md

44 lines
1.6 KiB
Markdown
Raw Normal View History

2020-05-15 16:28:17 +02:00
# @sapphirecode/crypto-helper
2020-03-06 08:40:58 +01:00
2020-12-13 13:09:34 +01:00
version: 1.2.x
2020-05-15 16:28:17 +02:00
simple functions for cryptography
## Installation
npm:
> npm i --save @sapphirecode/crypto-helper
yarn:
> yarn add @sapphirecode/crypto-helper
2020-03-06 08:40:58 +01:00
## Usage
```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
// jwt like object signing
const signed = crypto.sign_object({foo: 'bar'}, 'secret');
2020-05-15 16:28:17 +02:00
const info = crypto.get_signature_info(signed); // returns an object with iat (issued at), key_info and data
2020-03-06 08:40:58 +01:00
const dec = crypto.decode_signed(signed); // decode a signed object without verifying the signature
const ver = 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.
2020-12-13 13:09:34 +01:00
const ver_info = crypto.verify_signature_get_info(signed, 'secret', 10000); // verify a signature and get signature information like iat and key_info
const ver_func = crypto.verify_signature(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');
2020-03-06 08:40:58 +01:00
```
2020-05-15 16:28:17 +02:00
## License
MIT © Timo Hocker <timo@scode.ovh>