A stronger encryption and description in Node.js based on implementation that can be found here Stronger Encryption and Decryption in Node.js | Vance Lucas.
In short, a strong encryption will always produce a unique output and it will always return the raw data after the decryption. A 256 bytes (or 32 characters) salt is needed to do the encryption and decryption process. This salt can be generated by making use of one of the great NPM modules that is available out there - randomstring.
import randomstring from 'randomstring';
import { encrypt, decrypt } from 'nodemod/dist/scryptify/index.js';
(async () => {
/** 256 bytes or 32 characters salt */
const secret = randomstring.generate(32);
const rawData = '5ome_rand0m_m3ss4g3';
const encrypted = await encrypt(rawData, secret);
const decrypted = await decrypt(encrypted, secret);
decrypted === rawData; /** true */
})();
- text <string> Input string to be encrypted.
- secret <string> A 256 bytes (or 32 characters) salt for the encryption.
- returns: <Promise<string>> Promise which resolves with an encrypted output.
This methods works the same as encrypt(text, secret)
except that this is the synchronous version.
- text <string> Encrypted input string.
- secret <string> A 256 bytes (or 32 characters) salt for the decryption. This must be the exact same salt that is used in encrypting the input string.
- returns: <Promise<string>> Promise which resolves with the raw content of the encrypted data.
This methods works the same as decrypt(text, secret)
except that this is the synchronous version.
MIT License © Rong Sen Ng