Package Exports
- symcryptor
- symcryptor/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (symcryptor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
SymCryptor
SymCryptor allows you to easy use a symmetric encryption (with AES-CTR-256) and a signature method (with KMAC-256)
Installation
npm i symcryptorTest
npm testUsage
const symCryptor = require( 'symcryptor' );
( async () => {
const clearText = 'Hello world!';
// For hashing
const hashSecret = await symCryptor.rndBytes( 32 ); // Return a random 256-bit Buffer
const digest = symCryptor.getHmac( clearText, hashSecret ); // Return a 512-bit Buffer as digest
// For encryption
const key = await symCryptor.rndBytes( 32 ); // Key must be a Buffer or Uint8Array of 512-bit
const encrypted = await symCryptor.encrypt( clearText, key, hashSecret ); // Return a Buffer
// For decryption
const decrypted = await symCryptor.decrypt( clearText, key, hashSecret ); // Return a Buffer
} )();Methods
symCryptor.rndBytes
symCryptor.rndBytes( length: Number [, bytes: Boolean = false] )Parameters
lengthRequired - The length of random data in bytesbytesOptional - IftruereturnUint8Arrayinstead ofBufferon fulfillment
Return
Random Buffer | Uint8Array of selected length when Promise resolved else throw an Error
symCryptor.getHmac
symCryptor.getHmac( data: String | Buffer | Uint8Array, key: Buffer | Uint8Array [, customization: String | Buffer | Uint8Array = '' [, bytes: Boolean = false]] )Parameters
dataRequired - The data you want hashkeyRequired - The secret key (it should be of 256-bit)customizationOptional - Some data you want to pass to hash algorithm (like AAD in AES-GCM)bytesOptional - IftruereturnUint8Arrayinstead ofBuffer
Return
A 512-bit Buffer | Uint8Array as digest else throw an Error
symCryptor.encrypt
symCryptor.encrypt( data: String | Buffer | Uint8Array, key: Buffer | Uint8Array [, hashKey: Buffer | Uint8Array [, customization: String | Buffer | Uint8Array = '' [, bytes: Boolean = false]]] )Parameters
dataRequired - The data you want to encryptkeyRequired - The key you want to use for encryption (it must be of 256-bit)hashKeyOptional - The key you want to use to sign encrypted datacustomizationOptional - Some data you want to pass to hash algorithm (like AAD in AES-GCM)bytesOptional - IftruereturnUint8Arrayinstead ofBufferon fulfillment
Return
Buffer | Uint8Array when Promise resolved else throw an Error
symCryptor.decrypt
symCryptor.decrypt( data: Buffer | Uint8Array, key: Buffer | Uint8Array [, hashKey: Buffer | Uint8Array [, customization: String | Buffer | Uint8Array = '' [, bytes: Boolean = false]]] )Parameters
dataRequired - The encrypted data you want to decryptkeyRequired - The key you have to use for decryption (it must be of 256-bit)hashKeyOptional - The key you have to use to verify signature of encrypted data (required if data was signed)customizationOptional - Some data you have to pass to hash algorithm (like AAD in AES-GCM; required if it was passed during encryption)bytesOptional - IftruereturnUint8Arrayinstead ofBufferon fulfillment
Return
Buffer | Uint8Array when Promise resolved else throw an Error
Note
- IV and signature are automatically added to encrypted data and removed when data will be decrypted