Created
June 30, 2026 12:41
-
-
Save xgqfrms/0815f60442d9db297bdae91db3196d12 to your computer and use it in GitHub Desktop.
Strong Password Generator js xgqfrms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Strong Password Generator | |
| * @author xgqfrms Reference Implementation | |
| * @license MIT | |
| */ | |
| class StrongPasswordGenerator { | |
| constructor() { | |
| this.charset = { | |
| uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
| lowercase: 'abcdefghijklmnopqrstuvwxyz', | |
| numbers: '0123456789', | |
| symbols: '!@#$%^&*()_+-=[]{}|;:,.<>?' | |
| }; | |
| } | |
| /** | |
| * Generates a secure random integer between 0 and max - 1 | |
| * @param {number} max | |
| * @returns {number} | |
| */ | |
| getSecureRandomInt(max) { | |
| const array = new Uint32Array(1); | |
| // Cryptographically secure pseudorandom number generator (CSPRNG) | |
| window.crypto.getRandomValues(array); | |
| return array[0] % max; | |
| } | |
| /** | |
| * Generates a strong password based on configurations | |
| * @param {Object} options | |
| * @returns {string} | |
| */ | |
| generate(options = {}) { | |
| const { | |
| length = 16, | |
| uppercase = true, | |
| lowercase = true, | |
| numbers = true, | |
| symbols = true | |
| } = options; | |
| let availableChars = ''; | |
| let passwordPool = []; | |
| // Ensure at least one character from each selected pool is guaranteed | |
| if (uppercase) { | |
| availableChars += this.charset.uppercase; | |
| passwordPool.push(this.charset.uppercase[this.getSecureRandomInt(this.charset.uppercase.length)]); | |
| } | |
| if (lowercase) { | |
| availableChars += this.charset.lowercase; | |
| passwordPool.push(this.charset.lowercase[this.getSecureRandomInt(this.charset.lowercase.length)]); | |
| } | |
| if (numbers) { | |
| availableChars += this.charset.numbers; | |
| passwordPool.push(this.charset.numbers[this.getSecureRandomInt(this.charset.numbers.length)]); | |
| } | |
| if (symbols) { | |
| availableChars += this.charset.symbols; | |
| passwordPool.push(this.charset.symbols[this.getSecureRandomInt(this.charset.symbols.length)]); | |
| } | |
| if (availableChars.length === 0) { | |
| throw new Error('You must select at least one character type.'); | |
| } | |
| // Fill the remaining length of the password | |
| while (passwordPool.length < length) { | |
| const randomIndex = this.getSecureRandomInt(availableChars.length); | |
| passwordPool.push(availableChars[randomIndex]); | |
| } | |
| // Shuffle the final array securely using Fisher-Yates variant | |
| for (let i = passwordPool.length - 1; i > 0; i--) { | |
| const j = this.getSecureRandomInt(i + 1); | |
| [passwordPool[i], passwordPool[j]] = [passwordPool[j], passwordPool[i]]; | |
| } | |
| return passwordPool.join(''); | |
| } | |
| } | |
| // === Basic Usage Example === | |
| const generator = new StrongPasswordGenerator(); | |
| // Generate a 16-character secure password | |
| const securePassword = generator.generate({ length: 16 }); | |
| console.log('Generated Secure Password:', securePassword); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/xgqfrms/cdn/actions/runs/19885478585
xgqfrms/cdn@eecbce9#diff-94caa076b8828d47702e229b1e748640f5394d20fdc5cfb5abf8a6089d29c269
https://github.com/xgqfrms/cdn/tree/gh-pages/sdk/strong-password-generator.js