Encryption
#
IntroductionThis part of the package covers Encryption
and Decryption
data in your application.
Under the hood, Mongez uses crypto-js for encryption and decryption.
#
UsageThere are two function, encrypt
and decrypt
.
#
Encryptionimport { encrypt } from './encryption';
console.log(encrypt('my-value')); // the hash will be something like// U2FsdGVkX1/gBGs9e2ZbfMjS/9quyLFdwIfu64R+8/fDgduHglALzr98gINn
The encrypt function can accept any type of data such as objects and arrays.
#
Decryptionimport { decrypt } from './encryption';
console.log(decrypt('U2FsdGVkX1/gBGs9e2ZbfMjS/9quyLFdwIfu64R+8/fDgduHglALzr98gINn')); // my-value
#
API/** * Get the encrypted text of the given value * * @param {any} value * @param {string} key * @param {any} driver * @returns {string} */encrypt(value: any, key = config.get('encryption.key'), driver = AES): string
/** * Decrypt the given cypher text and return its original value, otherwise null will be returned. * * @param {string} cypher * @param {string} key * @param {any} driver * @returns {string|null} */decrypt(cypher: string, key: string = config.get('encryption.key'), driver: any = AES): string|null
#
Encryption AlgorithmBy default, the current used algorithm is AES
, but you can of course change it from Configuration.
#
ConfigurationsThe current interface illustrates the encryption configuration list
type EncryptionSettings = { /** * Encryption Settings */ encryption?: { /** * Encryption key */ key?: string; /** * Encryption Algorithm * * @see https://www.npmjs.com/package/crypto-js */ algorithm?: any; }};
The following table illustrates the available configuration under the encryption
prefix config.
Key | Type | Default Value | Description |
---|---|---|---|
key | string | The algorithm encryption key, the key should be provided for more security, its recommended to set it to be REACT_APP_KEY_NAME in Dot Env File | |
algorithm | object | AES | One of the Algorithms List |