This commit is contained in:
parent
276a6ecaa3
commit
e078eb9c34
14
jasmine.json
Normal file
14
jasmine.json
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
{
|
||||
"spec_dir": "test",
|
||||
"spec_files": [
|
||||
"spec/*.js",
|
||||
"spec/*.ts"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/*.js",
|
||||
"helpers/*.ts"
|
||||
],
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": false
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@sapphirecode/password-helper",
|
||||
"version": "1.0.48",
|
||||
"version": "1.0.49",
|
||||
"main": "index.js",
|
||||
"author": {
|
||||
"name": "Timo Hocker",
|
||||
@ -17,14 +17,17 @@
|
||||
"@sapphirecode/encoding-helper": "^1.0.38",
|
||||
"@sapphirecode/eslint-config": "^2.1.4",
|
||||
"@stryker-mutator/core": "^3.2.3",
|
||||
"@stryker-mutator/jasmine-framework": "^3.3.1",
|
||||
"@stryker-mutator/jasmine-runner": "^3.3.1",
|
||||
"@stryker-mutator/javascript-mutator": "^3.2.3",
|
||||
"ava": "^3.8.2",
|
||||
"@types/jasmine": "^3.5.14",
|
||||
"eslint": "^7.0.0",
|
||||
"jasmine": "^3.6.1",
|
||||
"nyc": "^15.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
||||
"test": "nyc ava",
|
||||
"test": "nyc jasmine --config=\"jasmine.json\"",
|
||||
"mutate": "stryker run",
|
||||
"compile": "tsc --allowJs --declaration --emitDeclarationOnly index.js"
|
||||
},
|
||||
|
@ -17,8 +17,10 @@ module.exports = {
|
||||
'clear-text',
|
||||
'progress'
|
||||
],
|
||||
testRunner: 'command',
|
||||
transpilers: [],
|
||||
coverageAnalysis: 'all',
|
||||
mutate: [ 'index.js' ]
|
||||
testRunner: 'jasmine',
|
||||
testFramework: 'jasmine',
|
||||
jasmineConfigFile: 'jasmine.json',
|
||||
transpilers: [],
|
||||
coverageAnalysis: 'perTest',
|
||||
mutate: [ 'index.js' ]
|
||||
};
|
||||
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* eslint-disable no-magic-numbers */
|
||||
|
||||
'use strict';
|
||||
|
||||
const helper = require ('../index');
|
||||
const argon2 = require ('argon2');
|
||||
const test = require ('ava');
|
||||
const encoding_helper = require ('@sapphirecode/encoding-helper');
|
||||
|
||||
const required_options = {
|
||||
hashLength: 64,
|
||||
saltLength: 32,
|
||||
type: argon2.argon2id
|
||||
};
|
||||
const type_str = 'argon2id';
|
||||
|
||||
test ('create hash', async (t) => {
|
||||
const hash = await helper.hash ('foo');
|
||||
const regex = /\$(?<type>.*?)\$.*?\$.*?\$(?<salt>.*?)\$(?<hash>.*)/u;
|
||||
const res = regex.exec (hash);
|
||||
t.is (typeof hash, 'string');
|
||||
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);
|
||||
});
|
||||
|
||||
test ('validate', async (t) => {
|
||||
// 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');
|
||||
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);
|
||||
});
|
61
test/spec/index.js
Normal file
61
test/spec/index.js
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user