samlibs-generate-password
v1.0.0
Published
A lightweight, fast, and optimized password generator library
Maintainers
Readme
samlibs-generate-password
A lightweight, fast, and optimized password generator library for Node.js. Unlike other libraries that require complex setup, this package provides a simple function that accepts a configuration object to generate secure passwords.
🚀 Features
- Ultra Lightweight: Minimal dependencies and optimized code
- Fast Performance: Efficient character pool generation and selection
- TypeScript Support: Full TypeScript definitions included
- Universal Compatibility: Works with CommonJS and ES modules
- Flexible Configuration: Comprehensive options for password customization
- Secure Random Generation: Uses crypto.getRandomValues when available
- Character Exclusion: Exclude specific characters from password generation
📦 Installation
npm install samlibs-generate-password🔧 Usage
CommonJS
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"ES Modules
import generatePassword from 'samlibs-generate-password';
// Generate multiple passwords
const passwords = generatePassword({
length: 12,
quantity: 5,
special: true
});
console.log(passwords); // Array of 5 passwordsTypeScript
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
Basic Usage
const generatePassword = require('samlibs-generate-password');
// Default password (12 characters, all character types)
const password = generatePassword();Custom Length and Character Types
// 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
});Multiple Passwords
// Generate 10 passwords at once
const passwords = generatePassword({
length: 15,
quantity: 10
});Exclude Similar Characters
// Exclude confusing characters like 0, O, l, 1
const password = generatePassword({
length: 12,
exclude: ['0', 'O', 'l', '1', 'I']
});Strong Password for Databases
// 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:
// Other libraries
const generator = new PasswordGenerator({...});
const password = generator.generate();
// samlibs-generate-password (simpler)
const password = generatePassword({...});🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
MIT License - see LICENSE file for details.
🔗 Links
Made with ❤️ for developers who value simplicity and performance.
