diff --git a/app/helpers/password-hash.js b/app/helpers/password-hash.js new file mode 100644 index 0000000..3361e1e --- /dev/null +++ b/app/helpers/password-hash.js @@ -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); + }, +}; \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index e1ca9b6..e91ef4a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -107,6 +107,7 @@ gulp.task('lint-tests', () => { const LINT_TESTS_FILES = TEST_FILES.concat([ 'gulpfile.js', 'server.js', + 'migrations/*.js', ]); // eslint diff --git a/test/unit/helpers/passwordhash_test.js b/test/unit/helpers/passwordhash_test.js new file mode 100644 index 0000000..a3ea956 --- /dev/null +++ b/test/unit/helpers/passwordhash_test.js @@ -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); + }); + }); +}); \ No newline at end of file