fix
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,2 +1,4 @@ | |||||||
| /node_modules/ | /node_modules/ | ||||||
| /dist/ | /dist/ | ||||||
|  | /.nyc_output/ | ||||||
|  | /coverage/ | ||||||
|   | |||||||
| @@ -14,5 +14,8 @@ | |||||||
|     "lint": "eslint .", |     "lint": "eslint .", | ||||||
|     "test": "nyc ava", |     "test": "nyc ava", | ||||||
|     "ci": "yarn && node jenkins.js" |     "ci": "yarn && node jenkins.js" | ||||||
|  |   }, | ||||||
|  |   "dependencies": { | ||||||
|  |     "@scode/encoding-helper": "^1.0.1" | ||||||
|   } |   } | ||||||
| } | } | ||||||
							
								
								
									
										111
									
								
								test/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								test/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,111 @@ | |||||||
|  | /* | ||||||
|  |  * Copyright (C) Sapphirecode - All Rights Reserved | ||||||
|  |  * Created by Timo Hocker <timo@scode.ovh>, March 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) => { | ||||||
|  |   t.throws (() => (crypto.random_hex (0))); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | 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) => { | ||||||
|  |   t.throws (() => (crypto.random_string (0))); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | 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 ('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 ('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 }); | ||||||
|  | }); | ||||||
| @@ -162,6 +162,11 @@ | |||||||
|     "@nodelib/fs.scandir" "2.1.3" |     "@nodelib/fs.scandir" "2.1.3" | ||||||
|     fastq "^1.6.0" |     fastq "^1.6.0" | ||||||
|  |  | ||||||
|  | "@scode/encoding-helper@^1.0.1": | ||||||
|  |   version "1.0.1" | ||||||
|  |   resolved "https://npm.scode.ovh/@scode%2fencoding-helper/-/encoding-helper-1.0.1.tgz#dfac0f5f8ae6060c5305575ae7229e05365fa518" | ||||||
|  |   integrity sha512-EZiNDnvcbAsnbtb81Ypz7j0QUsmtfNlSVOkXOvDqEqJNfsrFr+e/5IvQJH8TPP0Iwzu9etbHXhEzRg6eOStGng== | ||||||
|  |  | ||||||
| "@scode/eslint-config@^1.2.25": | "@scode/eslint-config@^1.2.25": | ||||||
|   version "1.2.25" |   version "1.2.25" | ||||||
|   resolved "https://npm.scode.ovh/@scode%2feslint-config/-/eslint-config-1.2.25.tgz#b126e2e23ecf418c66e634fa279fd0e8e06ab4db" |   resolved "https://npm.scode.ovh/@scode%2feslint-config/-/eslint-config-1.2.25.tgz#b126e2e23ecf418c66e634fa279fd0e8e06ab4db" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user