54 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-03-25 17:02:04 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Password-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, March 2020
*/
2020-03-27 09:15:32 +01:00
/* eslint-disable no-magic-numbers */
2020-03-04 13:21:54 +01:00
'use strict';
const helper = require ('../index');
2020-03-27 09:15:32 +01:00
const argon2 = require ('argon2');
2020-03-04 13:21:54 +01:00
const test = require ('ava');
2020-03-27 09:15:32 +01:00
const encoding_helper = require ('@scode/encoding-helper');
const required_options = {
hashLength: 64,
saltLength: 32,
type: argon2.argon2id
};
const type_str = 'argon2id';
2020-03-04 13:21:54 +01:00
test ('create hash', async (t) => {
const hash = await helper.hash ('foo');
2020-03-27 09:15:32 +01:00
const regex = /\$(?<type>.*?)\$.*?\$.*?\$(?<salt>.*?)\$(?<hash>.*)/u;
const res = regex.exec (hash);
2020-03-04 13:21:54 +01:00
t.is (typeof hash, 'string');
2020-03-27 09:15:32 +01:00
t.not (res, null);
t.is (res.groups.type, type_str);
t.is (
encoding_helper.to_hex (res.groups.salt, 'base64').length,
required_options.saltLength * 2
);
t.is (
encoding_helper.to_hex (res.groups.hash, 'base64').length,
required_options.hashLength * 2
);
t.is (await argon2.verify (hash, 'foo', required_options), true);
2020-03-04 13:21:54 +01:00
});
test ('validate', async (t) => {
2020-03-27 09:15:32 +01:00
// eslint-disable-next-line max-len
const hash = '$argon2id$v=19$m=4096,t=3,p=1$IxY4EsXRazKmYijrGMEKap6MWeSjVaBIKykCu2fzNwg$ffJ4aOJTITnakX5NXVTAVvQZFIrj47mKVDuqcStu6uw1Ouo0LfILHP9z3beaZscGGR5BXJZhUFlXSsdsi/4pOg';
2020-03-04 13:21:54 +01:00
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);
});