Add basic scrypt password hashing helper

This commit is contained in:
Timothy Warren 2016-02-24 14:02:03 -05:00
parent 8dac92464d
commit a7a4ac7d4b
3 changed files with 54 additions and 0 deletions

View 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);
},
};

View File

@ -107,6 +107,7 @@ gulp.task('lint-tests', () => {
const LINT_TESTS_FILES = TEST_FILES.concat([ const LINT_TESTS_FILES = TEST_FILES.concat([
'gulpfile.js', 'gulpfile.js',
'server.js', 'server.js',
'migrations/*.js',
]); ]);
// eslint // eslint

View 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);
});
});
});