@krypton-org/krypton-auth
Version:
Express authentication middleware, using GraphQL and JSON Web Tokens.
46 lines • 1.73 kB
JavaScript
;
/**
* Module defining functions for password encryption.
* @module crypto/PasswordEncryption
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = __importDefault(require("crypto"));
const SALT_LENGTH = 64; // Length of the salt, in bytes
const HASH_LENGTH = 64; // Length of the hash, in bytes
const HASH_ITERATIONS = 128;
/**
* Function generating random salt, destinated to encrypt the `password`. Returning the encrypted `password` hash and salt.
* @param {string} password
* @returns {Promise<{ salt: string; hash: string }>} Promise to the password encrypted hash and salt
*/
exports.hashAndSalt = (password) => {
const salt = crypto_1.default.randomBytes(SALT_LENGTH);
return exports.hash(password, salt);
};
/**
* Encrypting `password` with the given `salt`. Returning hash and salt.
* @param {string} password
* @param {Buffer} salt
* @returns {Promise<{ salt: string; hash: string }>} Promise to the password encrypted hash and salt
*/
exports.hash = (password, salt) => {
if (typeof salt === 'string') {
salt = new Buffer(salt, 'base64');
}
return new Promise((resolve, reject) => {
crypto_1.default.pbkdf2(password, salt, HASH_ITERATIONS, HASH_LENGTH, 'sha512', function (err, hashStr) {
if (err) {
reject(err);
return;
}
resolve({
hash: hashStr.toString('base64'),
salt: salt.toString('base64'),
});
});
});
};
//# sourceMappingURL=PasswordEncryption.js.map