62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
/*
|
|
* 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>, May 2020
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const helper = require ('../../index');
|
|
const argon2 = require ('argon2');
|
|
const encoding_helper = require ('@sapphirecode/encoding-helper');
|
|
|
|
const required_options = {
|
|
hashLength: 64,
|
|
saltLength: 32,
|
|
type: argon2.argon2id
|
|
};
|
|
const type_str = 'argon2id';
|
|
|
|
describe ('password-helper', () => {
|
|
it ('should create hash', async () => {
|
|
const hash = await helper.hash ('foo');
|
|
const regex = /\$(?<type>.*?)\$.*?\$.*?\$(?<salt>.*?)\$(?<hash>.*)/u;
|
|
const res = regex.exec (hash);
|
|
expect (typeof hash)
|
|
.toEqual ('string');
|
|
expect (res).not.toBeNull ();
|
|
expect (res.groups.type)
|
|
.toEqual (type_str);
|
|
expect (
|
|
encoding_helper.to_hex (res.groups.salt, 'base64').length
|
|
)
|
|
.toEqual (
|
|
required_options.saltLength * 2
|
|
);
|
|
expect (
|
|
encoding_helper.to_hex (res.groups.hash, 'base64').length
|
|
)
|
|
.toEqual (
|
|
required_options.hashLength * 2
|
|
);
|
|
expect (await argon2.verify (hash, 'foo', required_options))
|
|
.toEqual (true);
|
|
});
|
|
|
|
it ('should validate', async () => {
|
|
// eslint-disable-next-line max-len
|
|
const hash = '$argon2id$v=19$m=4096,t=3,p=1$IxY4EsXRazKmYijrGMEKap6MWeSjVaBIKykCu2fzNwg$ffJ4aOJTITnakX5NXVTAVvQZFIrj47mKVDuqcStu6uw1Ouo0LfILHP9z3beaZscGGR5BXJZhUFlXSsdsi/4pOg';
|
|
const valid = await helper.verify (hash, 'foo');
|
|
expect (valid)
|
|
.toEqual (true);
|
|
});
|
|
|
|
it ('should fail validation', async () => {
|
|
const hash = await helper.hash ('foo');
|
|
const valid = await helper.verify (hash, 'bar');
|
|
expect (valid)
|
|
.toEqual (false);
|
|
});
|
|
});
|