Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaschampagne committed Jan 23, 2024
1 parent 288fe1c commit 73d0385
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"typescript",
"typescriptreact",
"html",
"json",
"yaml"
]
}
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crypto from 'crypto';

/**
* @author Thomas Champagne
* @description Password generator used as demo for the purpose of this starter library
Expand All @@ -7,16 +9,23 @@ export class Sesame {
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*{}(),.;:/<>?|_-+=';

/**
* Create a password according length parameter
* Create a password according to the length parameter
*
* @param length the length of the password to be created.
* @returns password created
*/
public static create(length: number): string {
const availableChars = Sesame.AVAILABLE_CHARS;
const randomChars = Array.from({ length }, () =>
availableChars.charAt(Math.floor(Math.random() * availableChars.length))
);
const randomChars = new Array(length);
const cryptoArray = new Uint32Array(length);

crypto.getRandomValues(cryptoArray);

for (let i = 0; i < length; i++) {
const randomIndex = cryptoArray[i] % availableChars.length;
randomChars[i] = availableChars.charAt(randomIndex);
}

return randomChars.join('');
}
}
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Sesame } from '../src';
describe('Sesame', () => {
it('should create and assert password length', () => {
// Given
const expectedLength = 24;
const expectedLength = 64;

// When
const password = Sesame.create(expectedLength);
Expand Down

0 comments on commit 73d0385

Please sign in to comment.