|
| 1 | +const UPPERCASE_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 2 | +const LOWERCASE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'; |
| 3 | +const NUMBERS = '0123456789'; |
| 4 | +const SPECIAL_CHARACTERS = '@#$^_+-'; |
| 5 | + |
| 6 | +interface PasswordOptions { |
| 7 | + length: number; |
| 8 | + includeUppercase?: boolean; |
| 9 | + includeLowercase?: boolean; |
| 10 | + includeNumbers?: boolean; |
| 11 | + includeSpecial?: boolean; |
| 12 | +} |
| 13 | +function generatePassword(options: PasswordOptions): string { |
| 14 | + const { length, includeUppercase = true, includeLowercase = true, includeNumbers = true, includeSpecial = true } = options; |
| 15 | + |
| 16 | + let allowedChars = ''; |
| 17 | + |
| 18 | + if (includeUppercase) allowedChars += UPPERCASE_LETTERS; |
| 19 | + if (includeLowercase) allowedChars += LOWERCASE_LETTERS; |
| 20 | + if (includeNumbers) allowedChars += NUMBERS; |
| 21 | + if (includeSpecial) allowedChars += SPECIAL_CHARACTERS; |
| 22 | + |
| 23 | + if (allowedChars.length === 0) { |
| 24 | + throw new Error('No character type is selected for the password'); |
| 25 | + } |
| 26 | + |
| 27 | + if (length < 4) { |
| 28 | + throw new Error('The password must be at least 4 characters long'); |
| 29 | + } |
| 30 | + |
| 31 | + let password = ''; |
| 32 | + const randomValues = new Uint32Array(length); |
| 33 | + |
| 34 | + crypto.getRandomValues(randomValues); |
| 35 | + |
| 36 | + for (let i = 0; i < length; i++) { |
| 37 | + const randomIndex = randomValues[i] % allowedChars.length; |
| 38 | + password += allowedChars[randomIndex]; |
| 39 | + } |
| 40 | + |
| 41 | + return password; |
| 42 | +} |
| 43 | + |
| 44 | +function generateSimplePassword(length: number): string { |
| 45 | + const ALL_CHARS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + NUMBERS + SPECIAL_CHARACTERS; |
| 46 | + |
| 47 | + if (length < 1) { |
| 48 | + throw new Error('The password length must be a positive number'); |
| 49 | + } |
| 50 | + |
| 51 | + let password = ''; |
| 52 | + const randomValues = new Uint32Array(length); |
| 53 | + |
| 54 | + crypto.getRandomValues(randomValues); |
| 55 | + |
| 56 | + for (let i = 0; i < length; i++) { |
| 57 | + const randomIndex = randomValues[i] % ALL_CHARS.length; |
| 58 | + password += ALL_CHARS[randomIndex]; |
| 59 | + } |
| 60 | + |
| 61 | + return password; |
| 62 | +} |
| 63 | + |
| 64 | +function generateSecurePassword(length: number): string { |
| 65 | + if (length < 4) { |
| 66 | + throw new Error('The minimum length for a secure password is 4 characters'); |
| 67 | + } |
| 68 | + |
| 69 | + const charSets = [UPPERCASE_LETTERS, LOWERCASE_LETTERS, NUMBERS, SPECIAL_CHARACTERS]; |
| 70 | + |
| 71 | + let password = ''; |
| 72 | + password += UPPERCASE_LETTERS[Math.floor(Math.random() * UPPERCASE_LETTERS.length)]; |
| 73 | + password += LOWERCASE_LETTERS[Math.floor(Math.random() * LOWERCASE_LETTERS.length)]; |
| 74 | + password += NUMBERS[Math.floor(Math.random() * NUMBERS.length)]; |
| 75 | + password += SPECIAL_CHARACTERS[Math.floor(Math.random() * SPECIAL_CHARACTERS.length)]; |
| 76 | + |
| 77 | + const ALL_CHARS = charSets.join(''); |
| 78 | + const remainingLength = length - 4; |
| 79 | + |
| 80 | + if (remainingLength > 0) { |
| 81 | + const randomValues = new Uint32Array(remainingLength); |
| 82 | + crypto.getRandomValues(randomValues); |
| 83 | + |
| 84 | + for (let i = 0; i < remainingLength; i++) { |
| 85 | + const randomIndex = randomValues[i] % ALL_CHARS.length; |
| 86 | + password += ALL_CHARS[randomIndex]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return password |
| 91 | + .split('') |
| 92 | + .sort(() => Math.random() - 0.5) |
| 93 | + .join(''); |
| 94 | +} |
| 95 | + |
| 96 | +export { |
| 97 | + generatePassword, |
| 98 | + generateSimplePassword, |
| 99 | + generateSecurePassword, |
| 100 | + UPPERCASE_LETTERS, |
| 101 | + LOWERCASE_LETTERS, |
| 102 | + NUMBERS, |
| 103 | + SPECIAL_CHARACTERS, |
| 104 | +}; |
| 105 | + |
| 106 | +export type { PasswordOptions }; |
0 commit comments