@andrewthecoder/secure-password-generator 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrew S Erwin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Secure Password Generator
2
+
3
+ A secure password generator written in TypeScript that generates cryptographically strong random passwords.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @andrewthecoder/secure-password-generator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { generate } from '@andrewthecoder/secure-password-generator';
15
+
16
+ // Generate a password between 8 and 12 characters
17
+ const password = generate(8, 12);
18
+ console.log(password);
19
+ ```
20
+
21
+ ## Features
22
+
23
+ - Cryptographically secure random number generation
24
+ - Configurable password length range
25
+ - Unbiased character selection
26
+ - Written in TypeScript with full type definitions
27
+ - Comprehensive test suite
28
+
29
+ ## API
30
+
31
+ ### generate(minLength: number, maxLength: number): string
32
+
33
+ Generates a random password with length between minLength and maxLength (inclusive).
34
+
35
+ Parameters:
36
+
37
+ - minLength: Minimum password length (must be >= 1)
38
+ - maxLength: Maximum password length (must be >= minLength)
39
+
40
+ Returns:
41
+
42
+ - A random password string
43
+
44
+ Throws:
45
+
46
+ - PasswordGenerationError if parameters are invalid
47
+
48
+ ## License
49
+
50
+ MIT
@@ -0,0 +1,4 @@
1
+ export declare class PasswordGenerationError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare function generate(minLength: number, maxLength: number): string;
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import { randomBytes } from 'crypto';
2
+ export class PasswordGenerationError extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = 'PasswordGenerationError';
6
+ }
7
+ }
8
+ export function generate(minLength, maxLength) {
9
+ // Input validation
10
+ if (minLength < 1)
11
+ throw new PasswordGenerationError('Minimum length must be at least 1');
12
+ if (maxLength < minLength)
13
+ throw new PasswordGenerationError('Maximum length must be greater than or equal to minimum length');
14
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~';
15
+ // Generate random length between minLength and maxLength
16
+ const lengthRange = maxLength - minLength + 1;
17
+ const randomBytesForLength = randomBytes(4);
18
+ const length = minLength + (randomBytesForLength.readUInt32BE(0) % lengthRange);
19
+ // Calculate values needed for unbiased selection
20
+ const charLen = chars.length;
21
+ const maxrb = 256 - (256 % charLen);
22
+ let result = '';
23
+ // Generate password
24
+ while (result.length < length) {
25
+ const randomChunk = randomBytes(Math.ceil(length * 1.25)); // Get more bytes than needed to account for rejected values
26
+ for (const byte of randomChunk) {
27
+ if (byte >= maxrb)
28
+ continue; // Skip values that would bias the selection
29
+ result += chars[byte % charLen];
30
+ if (result.length === length)
31
+ break;
32
+ }
33
+ }
34
+ return result;
35
+ }
package/jest.config.js ADDED
@@ -0,0 +1,16 @@
1
+ export default {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ extensionsToTreatAsEsm: ['.ts'],
5
+ moduleNameMapper: {
6
+ '^(\\.{1,2}/.*)\\.js$': '$1',
7
+ },
8
+ transform: {
9
+ '^.+\\.tsx?$': [
10
+ 'ts-jest',
11
+ {
12
+ useESM: true,
13
+ },
14
+ ],
15
+ },
16
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@andrewthecoder/secure-password-generator",
3
+ "version": "0.1.2",
4
+ "description": "A secure password generator written in TypeScript",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "type": "module",
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "test": "jest",
14
+ "prepare": "npm run build"
15
+ },
16
+ "keywords": [
17
+ "password",
18
+ "generator",
19
+ "typescript",
20
+ "security"
21
+ ],
22
+ "author": "Andrew S Erwin",
23
+ "license": "MIT",
24
+ "devDependencies": {
25
+ "@types/jest": "^29.5.0",
26
+ "@types/node": "^18.15.0",
27
+ "jest": "^29.5.0",
28
+ "ts-jest": "^29.1.0",
29
+ "typescript": "^5.0.0"
30
+ }
31
+ }
@@ -0,0 +1,25 @@
1
+ import { generate, PasswordGenerationError } from '../index.js';
2
+
3
+ describe('Password Generator', () => {
4
+ test('generates password with correct length range', () => {
5
+ const minLength = 8;
6
+ const maxLength = 12;
7
+ const password = generate(minLength, maxLength);
8
+ expect(password.length).toBeGreaterThanOrEqual(minLength);
9
+ expect(password.length).toBeLessThanOrEqual(maxLength);
10
+ });
11
+
12
+ test('throws error for invalid minimum length', () => {
13
+ expect(() => generate(0, 10)).toThrow(PasswordGenerationError);
14
+ });
15
+
16
+ test('throws error when max length is less than min length', () => {
17
+ expect(() => generate(10, 5)).toThrow(PasswordGenerationError);
18
+ });
19
+
20
+ test('generates different passwords', () => {
21
+ const password1 = generate(10, 10);
22
+ const password2 = generate(10, 10);
23
+ expect(password1).not.toBe(password2);
24
+ });
25
+ });
package/src/index.ts ADDED
@@ -0,0 +1,41 @@
1
+ import { randomBytes } from 'crypto';
2
+
3
+ export class PasswordGenerationError extends Error {
4
+ constructor(message: string) {
5
+ super(message);
6
+ this.name = 'PasswordGenerationError';
7
+ }
8
+ }
9
+
10
+ export function generate(minLength: number, maxLength: number): string {
11
+ // Input validation
12
+ if (minLength < 1) throw new PasswordGenerationError('Minimum length must be at least 1');
13
+ if (maxLength < minLength) throw new PasswordGenerationError('Maximum length must be greater than or equal to minimum length');
14
+
15
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~';
16
+
17
+ // Generate random length between minLength and maxLength
18
+ const lengthRange = maxLength - minLength + 1;
19
+ const randomBytesForLength = randomBytes(4);
20
+ const length = minLength + (randomBytesForLength.readUInt32BE(0) % lengthRange);
21
+
22
+ // Calculate values needed for unbiased selection
23
+ const charLen = chars.length;
24
+ const maxrb = 256 - (256 % charLen);
25
+
26
+ let result = '';
27
+
28
+ // Generate password
29
+ while (result.length < length) {
30
+ const randomChunk = randomBytes(Math.ceil(length * 1.25)); // Get more bytes than needed to account for rejected values
31
+
32
+ for (const byte of randomChunk) {
33
+ if (byte >= maxrb) continue; // Skip values that would bias the selection
34
+
35
+ result += chars[byte % charLen];
36
+ if (result.length === length) break;
37
+ }
38
+ }
39
+
40
+ return result;
41
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "declaration": true,
7
+ "outDir": "./dist",
8
+ "strict": true,
9
+ "esModuleInterop": true
10
+ },
11
+ "include": [
12
+ "src"
13
+ ],
14
+ "exclude": [
15
+ "node_modules",
16
+ "**/__tests__/*"
17
+ ]
18
+ }