🔐 encrypt-with-password
Securely encrypt text with a password in JavaScript.
encrypt-with-password simplifies encrypting text and data securely with leading security standards. It uses PBKDF2 for key derivation and AES 256-bit for encryption and decryption.
Download
encrypt-with-password can be downloaded from NPM with the command:
npm install encrypt-with-password
or:
yarn add encrypt-with-password
Encrypting and Decrypting Text with a Password
encryptpwd.encrypt(text, password)
for encrypting text with a password.
encryptpwd.decrypt(encryptedValue, password)
for decrypting text with the password used when encrypting.
Example:
const encryptpwd = require('encrypt-with-password');
const text = 'Hello, World!';
const password = 'examplepassword';
const encrypted = encryptpwd.encrypt(text, password);
const decrypted = encryptpwd.decrypt(encrypted, password)
NOTE: the encrypted output value of given text and password will likely change when encrypting multiple times with the same text and password. This is because of the nature of AES, block ciphers, and randomized IVs (initialization vectors).
Encrypting and Decrypting JavaScript Objects (JSON) with a Password
encryptpwd.encryptJSON(jsObject, password)
for encrypting JavaScript objects and variables with a password.
encryptpwd.decryptJSON(encryptedValue, password)
for decrypting JavaScript objects with the password used when encrypting.
Example:
const encryptpwd = require('encrypt-with-password');
const jsObject = {
aString: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
aNumber: 5,
anArray: [1, 2, 3, 4, 5],
anObj: {
aKey: 'a value'
}
};
const password = 'examplepassword2';
const encrypted = encryptpwd.encryptJSON(jsObject, password);
const decrypted = encryptpwd.decryptJSON(encrypted, password)
NOTE: encrypted objects are stored in JSON. This means that some richer objects like Date and custom classes may not be supported as seemlessly as normal numbers and strings.
Tests
Run npm run test
to run tests. To see test coverage along with running tests, run npm run test-with-coverage
.
Third Party Software Used
- AES-JS for AES and encryption
- PBKDF2 for PBKDF2 and key derivation
- Mocha and Chai for testing and assertion
- NYC for test coverage
File Structure
test/
— includes tests and test directories that are run with npm run test
.index.js
— the main code for the package.img
— any images used in the README or the package.
License and Credits
encrypt-with-password was built solely by web developer and student Fred Adams.
The code is completely OSS and is MIT Licensed. See LICENSE.txt for details.