Created
June 7, 2022 12:23
-
-
Save vko-online/b0883640c1e545451c2d7bfa358a46dd to your computer and use it in GitHub Desktop.
Password generate/compare
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
const crypto = require('crypto'); | |
const HASH_BYTES = 32; | |
const SALT_BYTES = 16; | |
const ITERATIONS = 15000; | |
const ENCODING = 'base64'; | |
const ALGORITHM = 'sha512'; | |
exports.hashPassword = password => { | |
return new Promise((resolve, reject) => { | |
crypto.randomBytes(SALT_BYTES, (err, salt) => { | |
if (err) { return reject('Failed to hash password'); } | |
crypto.pbkdf2(password, salt.toString(ENCODING), ITERATIONS, HASH_BYTES, ALGORITHM, (err, hash) => { | |
if (err) { return reject('Failed to hash password'); } | |
resolve({ salt: salt.toString(ENCODING), hashPassword: hash.toString(ENCODING) }); | |
}); | |
}); | |
}); | |
} | |
exports.authenticateUser = (password, salt, givenPassword) => { | |
return new Promise((resolve, reject) => { | |
crypto.pbkdf2(givenPassword, salt, ITERATIONS, HASH_BYTES, ALGORITHM, (err, verifyHash) => { | |
if (err) { return reject('Unknown Authentication Error'); } | |
if (verifyHash.toString(ENCODING) !== password) { | |
return reject('Authentication Error: Passwords do not match'); | |
} | |
resolve(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment