Add basic scrypt password hashing helper
This commit is contained in:
parent
8dac92464d
commit
a7a4ac7d4b
30
app/helpers/password-hash.js
Normal file
30
app/helpers/password-hash.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
const scrypt = require('scrypt');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Hash a password with scrypt
|
||||
*
|
||||
* @param {string} str - the password to hash
|
||||
* @return {Promise} - a promise returning the hash
|
||||
*/
|
||||
hash(str) {
|
||||
return scrypt.kdf(str, {
|
||||
N:16, // Number of rounds
|
||||
r:8, // Block-size of hash
|
||||
p:1, // Parallization
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Verify a password hash matches
|
||||
*
|
||||
* @param {string} hash - the hash to check against
|
||||
* @param {string} str - the password to check against the computed hash
|
||||
* @return {Promise} - a promise returning a boolean
|
||||
*/
|
||||
verify(hash, str) {
|
||||
return scrypt.verifyKdf(hash, str);
|
||||
},
|
||||
};
|
@ -107,6 +107,7 @@ gulp.task('lint-tests', () => {
|
||||
const LINT_TESTS_FILES = TEST_FILES.concat([
|
||||
'gulpfile.js',
|
||||
'server.js',
|
||||
'migrations/*.js',
|
||||
]);
|
||||
|
||||
// eslint
|
||||
|
23
test/unit/helpers/passwordhash_test.js
Normal file
23
test/unit/helpers/passwordhash_test.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
const testBase = require('../../test-base');
|
||||
const expect = testBase.expect;
|
||||
const hasher = testBase.require('app/helpers/password-hash');
|
||||
|
||||
let raw = 'password';
|
||||
let hash = '';
|
||||
|
||||
hasher.hash(raw).then((newHash) => {
|
||||
suite('Scrypt password hashing tests', () => {
|
||||
hash = newHash;
|
||||
test('Created a hash', () => {
|
||||
expect(hash).to.be.ok;
|
||||
});
|
||||
test('Hash matches password', (done) => {
|
||||
hasher.verify(hash, raw).then((matches) => {
|
||||
expect(matches).to.be.true;
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user