This commit is contained in:
parent
8a8689abc5
commit
b2dbea979f
12
jasmine.json
Normal file
12
jasmine.json
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
{
|
||||
"spec_dir": "test",
|
||||
"spec_files": [
|
||||
"spec/*.js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/*.js"
|
||||
],
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": false
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@sapphirecode/crypto-helper",
|
||||
"version": "1.1.57",
|
||||
"version": "1.1.58",
|
||||
"main": "index.js",
|
||||
"author": {
|
||||
"name": "Timo Hocker",
|
||||
@ -22,13 +22,14 @@
|
||||
"@sapphirecode/eslint-config": "^2.1.4",
|
||||
"@stryker-mutator/core": "^3.2.3",
|
||||
"@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"
|
||||
},
|
||||
@ -39,4 +40,4 @@
|
||||
"LICENSE",
|
||||
"index.js"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of crypto-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 */
|
||||
// @ts-nocheck
|
||||
'use strict';
|
||||
|
||||
const test = require ('ava');
|
||||
const crypto = require ('../index');
|
||||
|
||||
test ('encryption', (t) => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
t.is (typeof enc, 'string');
|
||||
});
|
||||
|
||||
test ('decryption', (t) => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
const dec = crypto.decrypt_aes (enc, 'bar');
|
||||
t.is (dec, 'foo');
|
||||
});
|
||||
|
||||
test ('encryption 128', (t) => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
t.is (typeof enc, 'string');
|
||||
});
|
||||
|
||||
test ('decryption 128', (t) => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
const dec = crypto.decrypt_aes (
|
||||
enc,
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
t.is (dec, 'foo');
|
||||
});
|
||||
|
||||
test ('encryption 256_quick', (t) => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
t.is (typeof enc, 'string');
|
||||
});
|
||||
|
||||
test ('decryption 256_quick', (t) => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
const dec = crypto.decrypt_aes (
|
||||
enc,
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
t.is (dec, 'foo');
|
||||
});
|
||||
|
||||
test ('fail decryption', (t) => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
const dec = crypto.decrypt_aes (enc, 'baz');
|
||||
t.is (dec, null);
|
||||
});
|
||||
|
||||
test ('rethrow decryption', (t) => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
t.throws (() => {
|
||||
crypto.decrypt_aes (
|
||||
enc,
|
||||
'baz',
|
||||
crypto.encryption_mode_cbc_256,
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test ('unique crypto strings', (t) => {
|
||||
const enc = [
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar')
|
||||
];
|
||||
const unique = enc.filter ((v, i) => enc.indexOf (v) === i).length;
|
||||
t.is (unique, 8);
|
||||
});
|
161
test/index.js
161
test/index.js
@ -1,161 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of crypto-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 */
|
||||
// @ts-nocheck
|
||||
'use strict';
|
||||
|
||||
const test = require ('ava');
|
||||
const crypto = require ('../index');
|
||||
|
||||
test ('random_hex', (t) => {
|
||||
const hex = crypto.random_hex (16);
|
||||
t.is (hex.length, 16);
|
||||
t.regex (hex, /^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
test ('random_hex with default length', (t) => {
|
||||
const hex = crypto.random_hex ();
|
||||
t.is (hex.length, 8);
|
||||
t.regex (hex, /^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
test ('random_hex should refuse lenght smaller 1', (t) => {
|
||||
const error = t.throws (
|
||||
() => (crypto.random_hex (0))
|
||||
);
|
||||
t.is (error.message, 'invalid length');
|
||||
});
|
||||
|
||||
test ('random_hex should always return correct length', (t) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const hex = crypto.random_hex (i);
|
||||
t.is (hex.length, i);
|
||||
}
|
||||
});
|
||||
|
||||
test ('random_string', (t) => {
|
||||
const str = crypto.random_string (16);
|
||||
t.is (str.length, 16);
|
||||
});
|
||||
|
||||
test ('random_string with default length', (t) => {
|
||||
const str = crypto.random_string ();
|
||||
t.is (str.length, 8);
|
||||
});
|
||||
|
||||
test ('random_string should refuse lenght smaller 1', (t) => {
|
||||
const error = t.throws (
|
||||
() => (crypto.random_string (0))
|
||||
);
|
||||
t.is (error.message, 'invalid length');
|
||||
});
|
||||
|
||||
test ('random_string should always return correct length', (t) => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const str = crypto.random_string (i);
|
||||
t.is (str.length, i);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
test ('hash_sha512', (t) => {
|
||||
const hash = crypto.hash_sha512 ('a', 'b');
|
||||
t.is (
|
||||
hash,
|
||||
// eslint-disable-next-line max-len
|
||||
'2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d'
|
||||
);
|
||||
});
|
||||
|
||||
test ('checksum', (t) => {
|
||||
const hash = crypto.checksum ('foo');
|
||||
t.is (
|
||||
hash,
|
||||
// eslint-disable-next-line max-len
|
||||
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
|
||||
);
|
||||
});
|
||||
|
||||
test ('create_salt', (t) => {
|
||||
const salt = crypto.create_salt ();
|
||||
t.is (salt.length, 64);
|
||||
t.regex (salt, /^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
test ('sign_object', (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
t.notThrows (() => {
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
t.is (typeof str, 'string');
|
||||
});
|
||||
});
|
||||
|
||||
test ('decode_signed', (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.decode_signed (str);
|
||||
t.deepEqual (obj, dec);
|
||||
});
|
||||
|
||||
test ('verify_signature', (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.verify_signature (str, 'baz');
|
||||
t.deepEqual (obj, dec);
|
||||
});
|
||||
|
||||
test ('return null on invalid input', (t) => {
|
||||
const ver = crypto.verify_signature (null, 'foo');
|
||||
t.is (ver, null);
|
||||
const dec = crypto.decode_signed (null, 'foo');
|
||||
t.is (dec, null);
|
||||
});
|
||||
|
||||
test ('do not fail verification if timeout unspecified', async (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz');
|
||||
t.deepEqual (obj, dec);
|
||||
});
|
||||
|
||||
test ('reject tampered signatures', (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.verify_signature (str, 'foo');
|
||||
t.is (dec, null);
|
||||
});
|
||||
|
||||
test ('reject old signatures', async (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz', 1);
|
||||
t.is (dec, null);
|
||||
});
|
||||
|
||||
test ('do not reject valid signatures', async (t) => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz', 100);
|
||||
t.deepEqual (obj, dec);
|
||||
});
|
||||
|
||||
test ('decode problematic token', (t) => {
|
||||
// eslint-disable-next-line max-len
|
||||
const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D';
|
||||
const obj = crypto.decode_signed (str);
|
||||
t.deepEqual (obj, { id: 1 });
|
||||
});
|
116
test/spec/encryption.js
Normal file
116
test/spec/encryption.js
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of crypto-helper which is released under MIT.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||
*/
|
||||
|
||||
// @ts-nocheck
|
||||
'use strict';
|
||||
|
||||
const crypto = require ('../../index');
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
describe ('encryption', () => {
|
||||
it ('encryption', () => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
expect (typeof enc)
|
||||
.toEqual ('string');
|
||||
});
|
||||
|
||||
it ('decryption', () => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
const dec = crypto.decrypt_aes (enc, 'bar');
|
||||
expect (dec)
|
||||
.toEqual ('foo');
|
||||
});
|
||||
|
||||
it ('encryption 128', () => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
expect (typeof enc)
|
||||
.toEqual ('string');
|
||||
});
|
||||
|
||||
it ('decryption 128', () => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
const dec = crypto.decrypt_aes (
|
||||
enc,
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_128
|
||||
);
|
||||
expect (dec)
|
||||
.toEqual ('foo');
|
||||
});
|
||||
|
||||
it ('encryption 256_quick', () => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
expect (typeof enc)
|
||||
.toEqual ('string');
|
||||
});
|
||||
|
||||
it ('decryption 256_quick', () => {
|
||||
const enc = crypto.encrypt_aes (
|
||||
'foo',
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
const dec = crypto.decrypt_aes (
|
||||
enc,
|
||||
'bar',
|
||||
crypto.encryption_mode_cbc_256_quick
|
||||
);
|
||||
expect (dec)
|
||||
.toEqual ('foo');
|
||||
});
|
||||
|
||||
it ('fail decryption', () => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
const dec = crypto.decrypt_aes (enc, 'baz');
|
||||
expect (dec)
|
||||
.toEqual (null);
|
||||
});
|
||||
|
||||
it ('rethrow decryption', () => {
|
||||
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||||
expect (() => {
|
||||
crypto.decrypt_aes (
|
||||
enc,
|
||||
'baz',
|
||||
crypto.encryption_mode_cbc_256,
|
||||
true
|
||||
);
|
||||
})
|
||||
.toThrowError (
|
||||
// eslint-disable-next-line max-len
|
||||
'error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt'
|
||||
);
|
||||
});
|
||||
|
||||
it ('unique crypto strings', () => {
|
||||
const enc = [
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar'),
|
||||
crypto.encrypt_aes ('foo', 'bar')
|
||||
];
|
||||
const unique = enc.filter ((v, i) => enc.indexOf (v) === i).length;
|
||||
expect (unique)
|
||||
.toEqual (8);
|
||||
});
|
||||
});
|
186
test/spec/index.js
Normal file
186
test/spec/index.js
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of crypto-helper which is released under MIT.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||
*/
|
||||
|
||||
// @ts-nocheck
|
||||
'use strict';
|
||||
|
||||
const crypto = require ('../../index');
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
describe ('crypto helper', () => {
|
||||
it ('random_hex', () => {
|
||||
const hex = crypto.random_hex (16);
|
||||
expect (hex.length)
|
||||
.toEqual (16);
|
||||
expect (hex)
|
||||
.toMatch (/^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
it ('random_hex with default length', () => {
|
||||
const hex = crypto.random_hex ();
|
||||
expect (hex.length)
|
||||
.toEqual (8);
|
||||
expect (hex)
|
||||
.toMatch (/^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
it ('random_hex should refuse lenght smaller 1', () => {
|
||||
expect (
|
||||
() => (crypto.random_hex (0))
|
||||
)
|
||||
.toThrowError ('invalid length');
|
||||
});
|
||||
|
||||
it ('random_hex should always return correct length', () => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const hex = crypto.random_hex (i);
|
||||
expect (hex.length)
|
||||
.toEqual (i);
|
||||
}
|
||||
});
|
||||
|
||||
it ('random_string', () => {
|
||||
const str = crypto.random_string (16);
|
||||
expect (str.length)
|
||||
.toEqual (16);
|
||||
});
|
||||
|
||||
it ('random_string with default length', () => {
|
||||
const str = crypto.random_string ();
|
||||
expect (str.length)
|
||||
.toEqual (8);
|
||||
});
|
||||
|
||||
it ('random_string should refuse lenght smaller 1', () => {
|
||||
expect (
|
||||
() => (crypto.random_string (0))
|
||||
)
|
||||
.toThrowError ('invalid length');
|
||||
});
|
||||
|
||||
it ('random_string should always return correct length', () => {
|
||||
for (let i = 1; i < 32; i++) {
|
||||
const str = crypto.random_string (i);
|
||||
expect (str.length)
|
||||
.toEqual (i);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it ('hash_sha512', () => {
|
||||
const hash = crypto.hash_sha512 ('a', 'b');
|
||||
expect (
|
||||
hash
|
||||
)
|
||||
.toEqual (
|
||||
// eslint-disable-next-line max-len
|
||||
'2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d'
|
||||
);
|
||||
});
|
||||
|
||||
it ('checksum', () => {
|
||||
const hash = crypto.checksum ('foo');
|
||||
expect (
|
||||
hash
|
||||
)
|
||||
.toEqual (
|
||||
// eslint-disable-next-line max-len
|
||||
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
|
||||
);
|
||||
});
|
||||
|
||||
it ('create_salt', () => {
|
||||
const salt = crypto.create_salt ();
|
||||
expect (salt.length)
|
||||
.toEqual (64);
|
||||
expect (salt)
|
||||
.toMatch (/^[0-9a-f]+$/iu);
|
||||
});
|
||||
|
||||
it ('sign_object', () => {
|
||||
const obj = { foo: 'bar' };
|
||||
expect (() => {
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
expect (typeof str)
|
||||
.toEqual ('string');
|
||||
}).not.toThrow ();
|
||||
});
|
||||
|
||||
it ('decode_signed', () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.decode_signed (str);
|
||||
expect (obj)
|
||||
.toEqual (dec);
|
||||
});
|
||||
|
||||
it ('verify_signature', () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.verify_signature (str, 'baz');
|
||||
expect (obj)
|
||||
.toEqual (dec);
|
||||
});
|
||||
|
||||
it ('should return null on invalid input', () => {
|
||||
const ver = crypto.verify_signature (null, 'foo');
|
||||
expect (ver)
|
||||
.toEqual (null);
|
||||
const dec = crypto.decode_signed (null, 'foo');
|
||||
expect (dec)
|
||||
.toEqual (null);
|
||||
});
|
||||
|
||||
it ('should not fail verification if timeout unspecified', async () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz');
|
||||
expect (obj)
|
||||
.toEqual (dec);
|
||||
});
|
||||
|
||||
it ('should reject tampered signatures', () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
const dec = crypto.verify_signature (str, 'foo');
|
||||
expect (dec)
|
||||
.toEqual (null);
|
||||
});
|
||||
|
||||
it ('should reject old signatures', async () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz', 1);
|
||||
expect (dec)
|
||||
.toEqual (null);
|
||||
});
|
||||
|
||||
it ('should not reject valid signatures', async () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const str = crypto.sign_object (obj, 'baz');
|
||||
await new Promise ((res) => {
|
||||
setTimeout (res, 10);
|
||||
});
|
||||
const dec = crypto.verify_signature (str, 'baz', 100);
|
||||
expect (obj)
|
||||
.toEqual (dec);
|
||||
});
|
||||
|
||||
it ('should decode problematic token', () => {
|
||||
// eslint-disable-next-line max-len
|
||||
const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D';
|
||||
const obj = crypto.decode_signed (str);
|
||||
expect (obj)
|
||||
.toEqual ({ id: 1 });
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user