Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

> generate-pw

Randomly generate, strengthen, and validate cryptographically-secure passwords.


💡 About

generate-pw is a lightweight, easy-to-use library that allows you to randomly generate, strengthen & validate cryptographically-secure password(s).

  • No external dependencies — Only built-in crypto methods used for secure randomization
  • Highly customizable — Specify length, quantity, charsets to use, etc.
  • Multi-environment support — Use in Node.js or the web browser
  • Command line usable — Just type generate-pw, that's it

⚡ Installation

As a global utility:

$ npm install -g generate-pw

As a runtime dependency, from your project root:

$ npm install generate-pw

🔌 Importing the API

Node.js

ECMAScript*:

import * as pw from 'generate-pw';

CommonJS:

const pw = require('generate-pw');
*Node.js version 14 or higher required

Web

<> HTML script tag:

<script src="https://cdn.jsdelivr.net/npm/generate-pw@1.5.10/dist/generate-pw.min.js"></script>

ES6:

(async () => {
    await import('https://cdn.jsdelivr.net/npm/generate-pw@1.5.10/dist/generate-pw.min.js');
    // Your code here...
})();

Greasemonkey

...
// @require https://cdn.jsdelivr.net/npm/generate-pw@1.5.10/dist/generate-pw.min.js
// ==/UserScript==

// Your code here...

💡 Note: To always import the latest version (not recommended in production!) remove the @1.5.10 version tag from the jsDelivr URL: https://cdn.jsdelivr.net/npm/generate-pw/dist/generate-pw.min.js


📋 API usage

generatePassword([options])

Generates one password if qty option is not given, returning a string:

const password = pw.generatePassword({ length: 11, numbers: true });
console.log(password); // sample output: 'bAsZm3mq6Qn'

...or multiple passwords if qty option is given, returning an array of strings:

const passwords = pw.generatePassword({ qty: 5, length: 8, symbols: true });
console.log(passwords);

/* sample output:

generatePassword() » Generating passwords...
generatePassword() » Passwords generated!
generatePassword() » Check returned array.
[ '!zSf@Q.s', '!,HT\\;m=', '?Lq&FV>^', 'gf}Y;}Ne', 'Stsx(GqE' ]
*/

💡 Note: If no options are passed, passwords will be 8-chars long, consisting of upper/lower cased letters.

See: Available options

generatePasswords(qty[, options])

Generates multiple passwords based on qty given, returning an array of strings:

const passwords = pw.generatePasswords(5, { length: 3, uppercase: false });
console.log(passwords);

/* sample output:

generatePasswords() » Generating passwords...
generatePasswords() » Passwords generated!
generatePasswords() » Check returned array.
[ 'yilppxru', 'ckvkyjfp', 'zolcpyfb' ]
*/

💡 Note: If no qty arg is passed, just one password will be generated, returned as a string.

See: Available options

strictify(password[, requiredCharTypes, options])

Modifies password given to use at least one character of each requiredCharTypes element passed, returning a string:

const strictPW = pw.strictify('abcdef', ['numbers', 'symbols']);
console.log(strictPW); // sample output: 'a!c2ef'

💡 Note: If no requiredCharTypes array is passed, all available types will be required.

Available requiredCharTypes are: ['number', 'symbol', 'lower', 'upper']

Available options (passed as object properties):

Name Type Description Default Value
verbose Boolean Show logging in console/terminal. true

validateStrength(password[, options])

Validates the strength of a password, returning an object containing:

  • strengthScore (0–100)
  • recommendations array
  • isGood boolean (true if strengthScore >= 80)

Example:

const pwStrength = pw.validateStrength('Aa?idsE');
console.log(pwStrength);

/* outputs:

validateStrength() » Validating password strength...
validateStrength() » Password strength validated!
validateStrength() » Check returned object for score/recommendations.
{
  strengthScore: 60,
  recommendations: [
    'Make it at least 8 characters long.',
    'Include at least one number.'
  ],
  isGood: false
}
*/

Available options (passed as object properties):

Name Type Description Default Value
verbose Boolean Show logging in console/terminal. true

Available options for generate*() functions

Any of these can be passed into the options object for each generate*() function:

Name Type Description Default Value
verbose Boolean Show logging in console/terminal. true
length Integer Length of password(s). 8
qty* Integer Number of passwords to generate. 1
charset String Characters to include in password(s). ''
exclude String Characters to exclude from password(s). ''
numbers Boolean Allow numbers in password(s). false
symbols Boolean Allow symbols in password(s). false
lowercase Boolean Allow lowercase letters in password(s). true
uppercase Boolean Allow uppercase letters in password(s). true
excludeSimilarChars Boolean Exclude similar characters (e.g. o,0,O,i,l,1,|) in password(s). false
strict Boolean Require at least one character from each allowed character set in password(s). false
*Only available in generatePassword([options]) since generatePasswords(qty[, options]) takes a qty argument

💻 Command line usage

When installed globally, generate-pw can also be used from the command line. The basic command is:

$ generate-pw

Command line options

Parameter options:
 --length=n                  Generate password(s) of n length.
 --qty=n                     Generate n password(s).
 --charset=chars             Only include chars in password(s).
 --exclude=chars             Exclude chars from password(s).

Boolean options:
 -n, --include-numbers       Allow numbers in password(s).
 -y, --include-symbols       Allow symbols in password(s).
 -L, --no-lowercase          Disallow lowercase letters in password(s).
 -U, --no-uppercase          Disallow uppercase letters in password(s).
 -S, --no-similar            Exclude similar characters in password(s).
 -s, --strict                Require at least one character from each
                             allowed character set in password(s).
 -q, --quiet                 Suppress all logging except errors.

Info commands:
 -h, --help                  Display help screen.
 -v, --version               Show version number.

🏛️ MIT License

Copyright © 2024 Adam Lui & contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


🛠️ Related utilities

Randomly generate, format, and validate IPv4 + IPv6 + MAC addresses.
Install / Readme / API usage / CLI usage / Discuss

Fetch IP geolocation data from the CLI.
Install / Readme / CLI usage / API usage / Discuss


More JavaScript utilities / Discuss / Back to top ↑