This commit is contained in:
parent
7152ae032c
commit
4004a93bb2
2
index.js
2
index.js
@ -41,7 +41,7 @@ const encryption_mode_cbc_128 = {
|
|||||||
/**
|
/**
|
||||||
* creates a random string
|
* creates a random string
|
||||||
*
|
*
|
||||||
* @param {number} len string length default: 6
|
* @param {number} len string length default: 8
|
||||||
* @returns {string} random string
|
* @returns {string} random string
|
||||||
*/
|
*/
|
||||||
function random_string (len = 8) {
|
function random_string (len = 8) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@sapphirecode/crypto-helper",
|
"name": "@sapphirecode/crypto-helper",
|
||||||
"version": "1.1.61",
|
"version": "1.1.62",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Timo Hocker",
|
"name": "Timo Hocker",
|
||||||
|
@ -28,7 +28,7 @@ describe ('crypto helper', () => {
|
|||||||
.toMatch (/^[0-9a-f]+$/iu);
|
.toMatch (/^[0-9a-f]+$/iu);
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('random_hex should refuse lenght smaller 1', () => {
|
it ('random_hex should refuse length smaller 1', () => {
|
||||||
expect (
|
expect (
|
||||||
() => (crypto.random_hex (0))
|
() => (crypto.random_hex (0))
|
||||||
)
|
)
|
||||||
@ -55,7 +55,7 @@ describe ('crypto helper', () => {
|
|||||||
.toEqual (8);
|
.toEqual (8);
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('random_string should refuse lenght smaller 1', () => {
|
it ('random_string should refuse length smaller 1', () => {
|
||||||
expect (
|
expect (
|
||||||
() => (crypto.random_string (0))
|
() => (crypto.random_string (0))
|
||||||
)
|
)
|
||||||
@ -126,6 +126,14 @@ describe ('crypto helper', () => {
|
|||||||
.toEqual (dec);
|
.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 return null on invalid input', () => {
|
it ('should return null on invalid input', () => {
|
||||||
const ver = crypto.verify_signature (null, 'foo');
|
const ver = crypto.verify_signature (null, 'foo');
|
||||||
expect (ver)
|
expect (ver)
|
||||||
@ -136,44 +144,86 @@ describe ('crypto helper', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it ('should not fail verification if timeout unspecified', async () => {
|
it ('should not fail verification if timeout unspecified', async () => {
|
||||||
|
jasmine.clock ()
|
||||||
|
.install ();
|
||||||
|
const base_time = (new Date);
|
||||||
|
jasmine.clock ()
|
||||||
|
.mockDate (base_time);
|
||||||
|
|
||||||
const obj = { foo: 'bar' };
|
const obj = { foo: 'bar' };
|
||||||
const str = crypto.sign_object (obj, 'baz');
|
const str = crypto.sign_object (obj, 'baz');
|
||||||
await new Promise ((res) => {
|
|
||||||
setTimeout (res, 10);
|
jasmine.clock ()
|
||||||
});
|
.tick (36e5);
|
||||||
|
|
||||||
const dec = crypto.verify_signature (str, 'baz');
|
const dec = crypto.verify_signature (str, 'baz');
|
||||||
expect (obj)
|
expect (obj)
|
||||||
.toEqual (dec);
|
.toEqual (dec);
|
||||||
});
|
|
||||||
|
|
||||||
it ('should reject tampered signatures', () => {
|
jasmine.clock ()
|
||||||
const obj = { foo: 'bar' };
|
.uninstall ();
|
||||||
const str = crypto.sign_object (obj, 'baz');
|
|
||||||
const dec = crypto.verify_signature (str, 'foo');
|
|
||||||
expect (dec)
|
|
||||||
.toEqual (null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should reject old signatures', async () => {
|
it ('should reject old signatures', async () => {
|
||||||
|
jasmine.clock ()
|
||||||
|
.install ();
|
||||||
|
const base_time = (new Date);
|
||||||
|
jasmine.clock ()
|
||||||
|
.mockDate (base_time);
|
||||||
|
|
||||||
const obj = { foo: 'bar' };
|
const obj = { foo: 'bar' };
|
||||||
const str = crypto.sign_object (obj, 'baz');
|
const str = crypto.sign_object (obj, 'baz');
|
||||||
await new Promise ((res) => {
|
|
||||||
setTimeout (res, 10);
|
jasmine.clock ()
|
||||||
});
|
.tick (50);
|
||||||
|
|
||||||
const dec = crypto.verify_signature (str, 'baz', 1);
|
const dec = crypto.verify_signature (str, 'baz', 1);
|
||||||
expect (dec)
|
expect (dec)
|
||||||
.toEqual (null);
|
.toEqual (null);
|
||||||
|
|
||||||
|
jasmine.clock ()
|
||||||
|
.uninstall ();
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should not reject valid signatures', async () => {
|
it ('should not reject valid signatures', async () => {
|
||||||
|
jasmine.clock ()
|
||||||
|
.install ();
|
||||||
|
const base_time = (new Date);
|
||||||
|
jasmine.clock ()
|
||||||
|
.mockDate (base_time);
|
||||||
|
|
||||||
const obj = { foo: 'bar' };
|
const obj = { foo: 'bar' };
|
||||||
const str = crypto.sign_object (obj, 'baz');
|
const str = crypto.sign_object (obj, 'baz');
|
||||||
await new Promise ((res) => {
|
|
||||||
setTimeout (res, 10);
|
jasmine.clock ()
|
||||||
});
|
.tick (50);
|
||||||
|
|
||||||
const dec = crypto.verify_signature (str, 'baz', 100);
|
const dec = crypto.verify_signature (str, 'baz', 100);
|
||||||
expect (obj)
|
expect (obj)
|
||||||
.toEqual (dec);
|
.toEqual (dec);
|
||||||
|
|
||||||
|
jasmine.clock ()
|
||||||
|
.uninstall ();
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('verify_signature on almost timed out packet', () => {
|
||||||
|
jasmine.clock ()
|
||||||
|
.install ();
|
||||||
|
const base_time = (new Date);
|
||||||
|
jasmine.clock ()
|
||||||
|
.mockDate (base_time);
|
||||||
|
|
||||||
|
const obj = { foo: 'bar' };
|
||||||
|
const str = crypto.sign_object (obj, 'baz');
|
||||||
|
|
||||||
|
jasmine.clock ()
|
||||||
|
.tick (10);
|
||||||
|
const dec = crypto.verify_signature (str, 'baz', 10);
|
||||||
|
expect (obj)
|
||||||
|
.toEqual (dec);
|
||||||
|
|
||||||
|
jasmine.clock ()
|
||||||
|
.uninstall ();
|
||||||
});
|
});
|
||||||
|
|
||||||
it ('should decode problematic token', () => {
|
it ('should decode problematic token', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user