This commit is contained in:
Timo Hocker 2020-03-04 13:21:54 +01:00
parent 8de034af69
commit 1e2e853fc0
3 changed files with 32 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
/node_modules/ /node_modules/
/dist/ /dist/
/.nyc_output/
/coverage/

View File

@ -6,7 +6,7 @@
/* eslint-disable no-magic-numbers */ /* eslint-disable no-magic-numbers */
'use strict'; 'use strict';
const argon2 = require('argon2'); const argon2 = require ('argon2');
const argon_options = { const argon_options = {
hashLength: 64, hashLength: 64,
@ -17,12 +17,12 @@ const argon_options = {
/** /**
* verify a plain text against an argon2 hash * verify a plain text against an argon2 hash
* *
* @param {string} hash argon hash * @param {string} hash_str hash
* @param {string} plain plain text * @param {string} plain plain text
* @returns {Promise<boolean>} true if valid * @returns {Promise<boolean>} true if valid
*/ */
function argon_verify (hash, plain) { function verify (hash_str, plain) {
return argon2.verify (hash, plain, argon_options); return argon2.verify (hash_str, plain, argon_options);
} }
/** /**
@ -31,11 +31,11 @@ function argon_verify (hash, plain) {
* @param {string} plain plain text * @param {string} plain plain text
* @returns {Promise<string>} hash * @returns {Promise<string>} hash
*/ */
function argon_hash (plain) { function hash (plain) {
return argon2.hash (plain, argon_options); return argon2.hash (plain, argon_options);
} }
module.exports = { module.exports = {
argon_hash, hash,
argon_verify verify
} };

21
test/index.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
const helper = require ('../index');
const test = require ('ava');
test ('create hash', async (t) => {
const hash = await helper.hash ('foo');
t.is (typeof hash, 'string');
});
test ('validate', async (t) => {
const hash = await helper.hash ('foo');
const valid = await helper.verify (hash, 'foo');
t.is (valid, true);
});
test ('fail validation', async (t) => {
const hash = await helper.hash ('foo');
const valid = await helper.verify (hash, 'bar');
t.is (valid, false);
});