Compare commits
3 Commits
e470b68ea5
...
8f227d8c0e
Author | SHA1 | Date | |
---|---|---|---|
8f227d8c0e | |||
cb9c142547 | |||
c0ec49fd4b |
1
index.js
1
index.js
@ -39,7 +39,6 @@ function random_string (len = 8) {
|
|||||||
throw new Error ('invalid length');
|
throw new Error ('invalid length');
|
||||||
return crypto.randomBytes (Math.ceil (len * 3 / 4))
|
return crypto.randomBytes (Math.ceil (len * 3 / 4))
|
||||||
.toString ('base64')
|
.toString ('base64')
|
||||||
.replace (/[=]+$/u, '')
|
|
||||||
.substr (0, len);
|
.substr (0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ module.exports = function(config) {
|
|||||||
config.set({
|
config.set({
|
||||||
mutator: "javascript",
|
mutator: "javascript",
|
||||||
packageManager: "yarn",
|
packageManager: "yarn",
|
||||||
reporters: ["clear-text"],
|
reporters: ["clear-text", "progress"],
|
||||||
testRunner: "command",
|
testRunner: "command",
|
||||||
transpilers: [],
|
transpilers: [],
|
||||||
coverageAnalysis: "all",
|
coverageAnalysis: "all",
|
||||||
|
@ -29,6 +29,13 @@ test ('random_hex should refuse lenght smaller 1', (t) => {
|
|||||||
t.is(error.message, 'invalid length');
|
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) => {
|
test ('random_string', (t) => {
|
||||||
const str = crypto.random_string (16);
|
const str = crypto.random_string (16);
|
||||||
t.is (str.length, 16);
|
t.is (str.length, 16);
|
||||||
@ -46,6 +53,14 @@ test ('random_string should refuse lenght smaller 1', (t) => {
|
|||||||
t.is(error.message, 'invalid length');
|
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) => {
|
test ('hash_sha512', (t) => {
|
||||||
const hash = crypto.hash_sha512 ('a', 'b');
|
const hash = crypto.hash_sha512 ('a', 'b');
|
||||||
t.is (
|
t.is (
|
||||||
@ -92,6 +107,16 @@ test ('verify_signature', (t) => {
|
|||||||
t.deepEqual (obj, dec);
|
t.deepEqual (obj, dec);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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) => {
|
test ('reject tampered signatures', (t) => {
|
||||||
const obj = { foo: 'bar' };
|
const obj = { foo: 'bar' };
|
||||||
const str = crypto.sign_object (obj, 'baz');
|
const str = crypto.sign_object (obj, 'baz');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user