scryptify
v0.2.2scryptify
A stronger encryption and decryption in Node.js
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 packages that is available out there - randomstring.
Table of contents
Pre-requisite
How to use
Install
# Install via NPM
$ npm install --save scryptify
Usage
Node.js
/** Import the package */
const randomstring = require('randomstring');
const scryptify = require('scryptify');
/** Setup a 256 bytes or 32 characters secret with the randomstring package */
const secret = randomstring.generate(32); /** xSZp9AmSufil7sMNQcCyrbVeGoHqJEJn */
/** To encrypt */
const encrypted = scryptify.encrypt('5ome_rand0m_m3ss4g3', secret);
const encrypted1 = scryptify.encrypt('5ome_rand0m_m3ss4g3', secret);
const encrypted2 = scryptify.encrypt('5ome_rand0m_m3ss4g3', secret);
/** The output will always be unique given the exact same raw data and salt. */
console.log({
encrypted, /** 7f087dcd618d825cfdb56884b97acd3a:50728aaa04bf8ae4ba434806e62d4e917cf4d1632716f013173813e812429f52 */
encrypted1, /** e472c45e697be97ee6fd2eac19900b00:c68590eef8f7d86c6b4486c387f31aebf5004aa14b01b925d3219a638490add9 */
encrypted2, /** 44b660bae95ad187079c78a4969c6218:39f16bce734e63784912189967deee68510b0ad93bc7244b2e12e31e65cd3fa0 */
});
/** To decrypt */
const decrypted = scryptify.decrypt(encrypted, secret);
const decrypted1 = scryptify.decrypt(encrypted, secret);
const decrypted2 = scryptify.decrypt(encrypted, secret);
/** The output will always return the same raw data after the decryption given with the same salt that is used in the encryption */
console.log({
decrypted, /** 5ome_rand0m_m3ss4g3 */
decrypted1, /** 5ome_rand0m_m3ss4g3 */
decrypted2, /** 5ome_rand0m_m3ss4g3 */
});
Native ES modules or TypeScript
import assert from 'assert';
import randomstring from 'randomstring';
import { encrypt, decrypt } from 'scryptify';
/**
* OR, use default import
*
* import scryptify from 'scryptify';
*
* const encrypt = scryptify.encrypt;
* const decrypt = scryptify.decrypt;
*/
const secret = randomstring.generate(32); // 256 bytes or 32 characters salt
const rawData = '5ome_rand0m_m3ss4g3';
const encrypted = encrypt(rawData, secret);
const decrypted = decrypt(encrypted, secret);
/** Assertion should pass */
assert.strictEqual(encrypted, decrypted); // OK
API Reference
encrypt(text, secret)
- 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.
encryptSync(text, secret)
This methods works the same as encrypt(text, secret)
except that this is the synchronous version.
decrypt(text, secret)
- 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.
decryptSync(text, secret)
This methods works the same as decrypt(text, secret)
except that this is the synchronous version.
License
MIT License © Rong Sen Ng