@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
41 lines (40 loc) • 1.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyPassword = verifyPassword;
exports.generateSaltAndHash = generateSaltAndHash;
const crypto_1 = __importDefault(require("crypto"));
/**
*
* @param password - The plain text password
* @param hash - The hash stored in the database
* @param salt - The salt stored in the database
*
* This function uses the crypto library to decrypt the hash using the salt and then compares
* the decrypted hash/salt with the password that the user provided at login.
*/
function verifyPassword(password, hash, salt) {
const hashVerify = crypto_1.default
.pbkdf2Sync(password, salt, 10000, 64, "sha512")
.toString("hex");
return hash === hashVerify;
}
/**
*
* @param password - The password string that the user inputs to the password field in the register form
*
* This function takes a plain text password and creates a salt and hash out of it.
* Instead of storing the plaintext password in the database, the salt and hash are stored for security.
*/
function generateSaltAndHash(password) {
const salt = crypto_1.default.randomBytes(32).toString("hex");
const genHash = crypto_1.default
.pbkdf2Sync(password, salt, 10000, 64, "sha512")
.toString("hex");
return {
salt: salt,
hash: genHash,
};
}