Simple NodeJS password generator

Lndr01

Lndr01

Golden Member
Joined
August 29, 2025
Messages
116
Reaction score
1,245
Points
93
  • Thread Author
  • #1
No additional packages needed

Code:
// Password generator function
function generatePassword(length = 12, options = { lowercase: true, uppercase: true, numbers: true, symbols: true }) {
// Define character sets
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const symbols = '!@#$%^&*()_+[]{}|;:,.<>?';

// Create a pool of characters based on the selected options
let characters = '';
if (options.lowercase) characters += lowercase;
if (options.uppercase) characters += uppercase;
if (options.numbers) characters += numbers;
if (options.symbols) characters += symbols;

// Generate the password
let password = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters[randomIndex];
}

return password;
}

// Example usage: Generate a 16-character password with all character types
const password = generatePassword(16, { lowercase: true, uppercase: true, numbers: true, symbols: true });
console.log('Generated Password:', password);
 
  • Tags
    generator nodejs password password generation password generator security tools simple
  • Top