Last active
February 11, 2020 23:21
-
-
Save destinmoulton/9acc57a82b40906cd1ce929b84b87da9 to your computer and use it in GitHub Desktop.
NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Encrypt a Password using PBKDF2 | |
| * | |
| * @param {string} password - Plaintext password | |
| * @returns {Promise} | |
| * @resolve {string} The hex value of derived key | |
| * @reject {Error} | |
| */ | |
| function encryptPassword(password) { | |
| return new Promise((resolve, reject) => { | |
| crypto.pbkdf2( | |
| password, | |
| "salt_here", // 64 character long random string | |
| 10000, | |
| 64, // Output key length in bits | |
| "sha512", | |
| (err, derivedKey) => { | |
| if (err) reject(err); | |
| // Resolve the derived key to hex | |
| resolve(derivedKey.toString("hex")); | |
| } | |
| ); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment