NodeCrypt
A simple nodejs library that makes life easier when you want to encrypt and decrypt string to multiple encodings.
This library was written in pure NodeJS with TypeScript.
How to use
-
With IV
Create a new instance of NodeCryptIV
const NodeCryptIV = NodeCryptIV;-
Random mode
const nodeCryptIV = secret: 'mysecret' ;const encrypted = nodeCryptIV;console; // emlSWvnh7cUwkEvWWeZafcR3Kp8YJafoRoJvJBQ_t38const decrypted = nodeCryptIV;console; // Pika Pika! -
No random mode
You can give the IV with an environment variable (NODECRYPT_IV) or directly in the NodeCryptIV instance. The function use first the IV in parameter and if it's not exist, it use the environment variable.
The IV must have a length of 16 characters.
const nodeCryptIV = secret: 'mysecret' iv: 'totototototototo' ;const encrypted = nodeCryptIV;console; // dG90b3RvdG90b3RvdG90b8bCKJ7SMuOaSWvijqvujXMconst decrypted = nodeCryptIV;console; // Pika Pika!
-
-
Without IV
Create a new instance of NodeCrypt
const NodeCrypt = NodeCrypt;const nodeCrypt = 'mysecret';const encrypted = nodeCrypt;console; // spsAgpAHVSClkOKb0LTT8Qconst decrypted = nodeCrypt;console; // Pika Pika!
Tips and trikcs
- If you want to decrypt a value with another instance of the library, don't forget to specify the encoding to use:// Encrypt value.const nodeCrypt = 'mysecret';const encrypted = nodeCrypt;// Decrypt value with the same instance.const decrypted = nodeCrypt;// Decrypt value with another instance.const newInstance = 'mysecret';// Specify the encoding of the encrypted text.newInstanceencoding = 'base64';newInstance;
Environements variables
You can give the above values by environements variables:
Environement variable | Code variable | Class |
---|---|---|
NODECRYPT_SECRET | secret | NodeCrypt, NodeCryptIV |
NODECRYPT_IV | iv | NodeCryptIV |
Author
Guillaume Quittet
https://www.linkedin.com/in/gquittet/