Skip to content

Commit

Permalink
Merge pull request #15 from almasen/dev
Browse files Browse the repository at this point in the history
2.2.1
  • Loading branch information
almasen authored Sep 13, 2023
2 parents 4d5566b + f6c82c1 commit 2ec9548
Show file tree
Hide file tree
Showing 32 changed files with 4,078 additions and 7,786 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tabWidth": 4,
"tabWidth": 2,
"trailingComma": "all",
"singleQuote": true
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Change log

## 2.2.1 - 2023-09-13

### Added

* Upgrade development dependencies
* Refactor add minor missing function return types
* Refactor style lint rule to use standard 2 indent width

## 2.2.0 - 2023-01-03

### Added
Expand Down
10,499 changes: 3,390 additions & 7,109 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "n-digit-token",
"version": "2.2.0",
"version": "2.2.1",
"description": "Cryptographically secure pseudo-random token of n digits",
"keywords": [
"token",
Expand Down Expand Up @@ -42,15 +42,15 @@
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/jest": "^29.5.4",
"@types/node": "^14.14.31",
"crypto-browserify": "^3.12.0",
"jest": "^26.6.3",
"jest": "^29.7.0",
"prettier": "^1.13.2",
"ts-jest": "^26.5.1",
"ts-jest": "^29.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.13.0",
"typescript": "^4.9.4"
"typescript": "^4.9.5"
},
"jest": {
"transform": {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/byte-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { generateSecureToken } from '../generateSecureToken';
import { randomBytes } from 'crypto-browserify';

test('token generation algorithm works with custom byte stream', () => {
const token = generateSecureToken(16, { customByteStream: randomBytes });
expect(token.length).toStrictEqual(16);
const token = generateSecureToken(16, { customByteStream: randomBytes });
expect(token.length).toStrictEqual(16);
});
26 changes: 13 additions & 13 deletions src/__tests__/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { generateSecureToken } from '../generateSecureToken';

test('token generation algorithm returns a string by default', () => {
for (let length = 1; length <= 32; length++) {
const token: string = generateSecureToken(length);
expect(typeof token).toStrictEqual('string');
}
for (let length = 1; length <= 32; length++) {
const token: string = generateSecureToken(length);
expect(typeof token).toStrictEqual('string');
}
});

test('token generation algorithm returns a token of input length by default', () => {
for (let length = 1; length <= 32; length++) {
for (let i = 0; i < 100; i++) {
const token = generateSecureToken(length);
expect(token.length).toStrictEqual(length);
}
for (let length = 1; length <= 32; length++) {
for (let i = 0; i < 100; i++) {
const token = generateSecureToken(length);
expect(token.length).toStrictEqual(length);
}
}
});

test('token generation algorithm always returns a token consisting of digits only', () => {
for (let length = 1; length <= 64; length++) {
const token = generateSecureToken(length);
expect(/^\d+$/.test(token)).toStrictEqual(true);
}
for (let length = 1; length <= 64; length++) {
const token = generateSecureToken(length);
expect(/^\d+$/.test(token)).toStrictEqual(true);
}
});
58 changes: 29 additions & 29 deletions src/__tests__/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ import { generateSecureToken } from '../generateSecureToken';
jest.spyOn(console, 'warn').mockImplementation();

afterEach(() => {
jest.clearAllMocks();
jest.clearAllMocks();
});

test('by default token generation algorithm returns values within expected distribution', () => {
const total = 100000;
const map = new Map();
for (let i = 0; i < total; i++) {
const token = generateSecureToken(1);
if (map.has(token)) {
map.set(token, map.get(token) + 1);
} else {
map.set(token, 1);
}
}
for (let i = 1; i < 10; i++) {
const count = map.get(`${i}`);
expect(count > Math.floor(total * 0.07)).toStrictEqual(true);
expect(count < Math.ceil(total * 0.13)).toStrictEqual(true);
const total = 100000;
const map = new Map();
for (let i = 0; i < total; i++) {
const token = generateSecureToken(1);
if (map.has(token)) {
map.set(token, map.get(token) + 1);
} else {
map.set(token, 1);
}
}
for (let i = 1; i < 10; i++) {
const count = map.get(`${i}`);
expect(count > Math.floor(total * 0.07)).toStrictEqual(true);
expect(count < Math.ceil(total * 0.13)).toStrictEqual(true);
}
});

test('token generation algorithm provided less than ideal memory still returns values within expected distribution', () => {
const total = 100000;
const map = new Map();
for (let i = 0; i < total; i++) {
const token = generateSecureToken(1, { customMemory: 1 });
if (map.has(token)) {
map.set(token, map.get(token) + 1);
} else {
map.set(token, 1);
}
}
for (let i = 1; i < 10; i++) {
const count = map.get(`${i}`);
expect(count > Math.floor(total * 0.07)).toStrictEqual(true);
expect(count < Math.ceil(total * 0.13)).toStrictEqual(true);
const total = 100000;
const map = new Map();
for (let i = 0; i < total; i++) {
const token = generateSecureToken(1, { customMemory: 1 });
if (map.has(token)) {
map.set(token, map.get(token) + 1);
} else {
map.set(token, 1);
}
}
for (let i = 1; i < 10; i++) {
const count = map.get(`${i}`);
expect(count > Math.floor(total * 0.07)).toStrictEqual(true);
expect(count < Math.ceil(total * 0.13)).toStrictEqual(true);
}
});
12 changes: 6 additions & 6 deletions src/__tests__/extended.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { generateSecureToken } from '../generateSecureToken';

test('token generation algorithm always returns secure random token of digits of expected length', () => {
for (let length = 1; length <= 1000; length++) {
for (let i = 0; i < 50; i++) {
const token = generateSecureToken(length);
expect(token.length).toStrictEqual(length);
expect(/^\d+$/.test(token)).toStrictEqual(true);
}
for (let length = 1; length <= 1000; length++) {
for (let i = 0; i < 50; i++) {
const token = generateSecureToken(length);
expect(token.length).toStrictEqual(length);
expect(/^\d+$/.test(token)).toStrictEqual(true);
}
}
});
12 changes: 6 additions & 6 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { generateSecureToken } from '../generateSecureToken';

test('simple token generation works', () => {
const token = generateSecureToken(6);
expect(token.length).toStrictEqual(6);
expect(typeof token).toStrictEqual('string');
const token = generateSecureToken(6);
expect(token.length).toStrictEqual(6);
expect(typeof token).toStrictEqual('string');
});

test('simple token generation shorthand works', () => {
const token = generateSecureToken(6);
expect(token.length).toStrictEqual(6);
expect(typeof token).toStrictEqual('string');
const token = generateSecureToken(6);
expect(token.length).toStrictEqual(6);
expect(typeof token).toStrictEqual('string');
});
Loading

0 comments on commit 2ec9548

Please sign in to comment.