Skip to content

Instantly share code, notes, and snippets.

@xgqfrms
Created June 30, 2026 12:41
Show Gist options
  • Select an option

  • Save xgqfrms/0815f60442d9db297bdae91db3196d12 to your computer and use it in GitHub Desktop.

Select an option

Save xgqfrms/0815f60442d9db297bdae91db3196d12 to your computer and use it in GitHub Desktop.
Strong Password Generator js xgqfrms
/**
* 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment