A lightweight, fast, and optimized password generator library
npm install samlibs-generate-passwordbash
npm install samlibs-generate-password
`
🔧 Usage
$3
`javascript
const generatePassword = require('samlibs-generate-password');
// Generate a simple password with default settings
const password = generatePassword();
console.log(password); // e.g., "K9$mN7#qR2@x"
// Generate password with custom options
const customPassword = generatePassword({
length: 16,
uppercase: true,
lowercase: true,
numbers: true,
special: false,
exclude: ['0', 'O', 'l', '1']
});
console.log(customPassword); // e.g., "KmN7qR2xPzWcJbVn"
`
$3
`javascript
import generatePassword from 'samlibs-generate-password';
// Generate multiple passwords
const passwords = generatePassword({
length: 12,
quantity: 5,
special: true
});
console.log(passwords); // Array of 5 passwords
`
$3
`typescript
import generatePassword, { PasswordOptions } from 'samlibs-generate-password';
const options: PasswordOptions = {
length: 20,
quantity: 1,
lowercase: true,
uppercase: true,
numbers: true,
special: true,
exclude: ['@', '#', '$']
};
const password: string = generatePassword(options) as string;
`
⚙️ Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| length | number | 12 | Length of each password |
| quantity | number | 1 | Number of passwords to generate |
| lowercase | boolean | true | Include lowercase letters (a-z) |
| uppercase | boolean | true | Include uppercase letters (A-Z) |
| numbers | boolean | true | Include numbers (0-9) |
| special | boolean | true | Include special characters (!@#$%^&*()_+-=[]{}|;:,.<>?) |
| exclude | string[] | [] | Array of characters to exclude |
📋 Examples
$3
`javascript
const generatePassword = require('samlibs-generate-password');
// Default password (12 characters, all character types)
const password = generatePassword();
`
$3
`javascript
// Only letters and numbers, 16 characters
const password = generatePassword({
length: 16,
special: false
});
// Only uppercase letters, 8 characters
const password = generatePassword({
length: 8,
lowercase: false,
numbers: false,
special: false
});
`
$3
`javascript
// Generate 10 passwords at once
const passwords = generatePassword({
length: 15,
quantity: 10
});
`
$3
`javascript
// Exclude confusing characters like 0, O, l, 1
const password = generatePassword({
length: 12,
exclude: ['0', 'O', 'l', '1', 'I']
});
`
$3
`javascript
// Database-safe password (no special characters that might cause issues)
const dbPassword = generatePassword({
length: 32,
special: false,
exclude: ['\\', '/', '"', "'", '']
🔒 Security Features
- Uses crypto.getRandomValues() for cryptographically secure randomness when available
- Falls back to Math.random() in environments where crypto is not available
- No predictable patterns in password generation
- Efficient character pool filtering to prevent bias
🚀 Performance
This library is optimized for:
- Minimal memory footprint
- Fast character pool generation
- Efficient exclusion filtering using Set data structure
- No unnecessary dependencies
📊 Comparison
Unlike other password libraries that require instantiating classes or complex configurations:
`javascript
// Other libraries
const generator = new PasswordGenerator({...});
const password = generator.generate();
// samlibs-generate-password (simpler)
const password = generatePassword({...});
``