<a id="top"></a>

<div align="right">
    <h6>
        <picture>
            <source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/icons/earth/white/icon32.svg">
            <img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/icons/earth/black/icon32.svg">
        </picture>
        &nbsp;English |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/zh-cn/#readme">简体中文</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/zh-tw/#readme">繁體中文</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/hi/#readme">हिंदी</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/bn/#readme">বাংলা</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/de/#readme">Deutsch</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/es/#readme">Español</a> |
        <a href="https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/fr/#readme">Français</a>
    </h6>
</div>

# > generate-pw

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

<a href="https://npm-compare.com/generate-pw/#timeRange=ALL">
    <img height=31 src="https://img.shields.io/npm/dm/generate-pw?logo=npm&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-pw-2.3.4">
    <img height=31 src="https://img.shields.io/badge/Latest_Build-2.3.4-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
<a href="#%EF%B8%8F-mit-license">
    <img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
<a href="https://www.npmjs.com/package/generate-pw?activeTab=code">
    <img height=31 src="https://img.shields.io/npm/unpacked-size/generate-pw?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
<a href="https://sonarcloud.io/component_measures?metric=vulnerabilities&selected=adamlui_js-utils%3Agenerate-pw&id=adamlui_js-utils">
    <img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-pw%2Fsrc%2Fgenerate-pw.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonar&logoColor=white&labelColor=464646&label=Vulnerabilities&color=ffef00"></a>
<a href="https://github.com/toolleeo/cli-apps/#password-managers">
    <img height=31 src="https://img.shields.io/badge/Mentioned_in-Awesome-ff69b4?logo=awesomelists&logoColor=white&labelColor=464646&style=for-the-badge"></a>

<br>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 💡 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

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## ⚡ Installation

As a **global utility**:

```
$ npm install -g generate-pw
```

As a **runtime dependency**, from your project root:

```
$ npm install generate-pw
```

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 🔌 Importing the API

#### <a href="#-es-modules-esm"><img height=13 width="auto" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@dbdea4b/assets/images/icons/module-systems/esm/icon32.png"></a> ES Modules (ESM):

```js
import pw from 'generate-pw'
```

#### <a href="#-commonjs-cjs"><img height=13 width="auto" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@dbdea4b/assets/images/icons/module-systems/cjs/icon32.png"></a> CommonJS (CJS):

```js
const pw = require('generate-pw')
```

#### [<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/chrome/icon16.png" title="Chrome">][web-usage][<img height=13.5 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/edge/icon16.png" title="Edge">][web-usage][<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/firefox/icon16.png" title="Firefox">][web-usage][<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/safari/icon16.png" title="Safari">][web-usage][<img height=13 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/qq/3d/icon-32x33.png" title="QQ Browser">][web-usage] Web:

[web-usage]: #-web

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

#### [<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/tampermonkey/icon28.png" title="Tampermonkey">][greasemonkey-usage][<img height="15" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/violentmonkey/icon25.png" title="Violentmonkey">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/scriptcat/icon32.png" title="ScriptCat">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/orangemonkey/icon16.png" title="OrangeMonkey">][greasemonkey-usage][<img height="14" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/stay/icon32.png" title="Stay">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/userscripts/icon32.png" title="Userscripts">][greasemonkey-usage] Greasemonkey:

[greasemonkey-usage]: #-greasemonkey-1

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

<br><a href="https://github.com/sponsors/KudoAI"><img src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@e53b001/assets/images/banners/sponsor/$10/banner1660x260.png"></a>

<hr>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 📋 API usage

### `generatePassword([options])`

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

```js
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:

```js
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](#available-options-for-generate-functions)

#

### `generatePasswords(qty[, options])`

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

```js
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](#available-options-for-generate-functions)

#

### `strictify(password[, requiredCharTypes, options])`

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

```js
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: `['numbers', 'symbols', '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:

```js
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`
`strength`            | String  | `<'weak'\|'basic'\|'strong'>` Apply strength preset.                           | `''`
`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`
`similarChars`        | Boolean | Include 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). | `true`
`entropy`             | Boolean | Calculate/log estimated entropy.                                               | `true`

##### _*Only available in [`generatePassword([options])`](#generatepasswordoptions) since [`generatePasswords(qty[, options])`](#generatepasswordsqty-options) takes a `qty` argument_

<br>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 💻 Command line usage

**generate-pw** can also be used directly from the command line. The basic command is:

```
$ generate-pw
```

<img src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-pw/assets/images/screenshots/cli/generate-pw-cmd-output.png">

#

### 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).
 --ui-lang="code"            ISO 639-1 code of language to display UI in.
 --config="path/to/file"     Load custom config file.

Boolean options:
 -w, --weak                  Generate weak password(s).
 -b, --basic                 Generate basic strength password(s).
 -t, --strong                Generate strong password(s).
 -N, --no-numbers            Disallow numbers in password(s).
 -Y, --no-symbols            Disallow symbols in password(s).
 -L, --no-lowercase          Disallow lowercase letters in password(s).
 -U, --no-uppercase          Disallow uppercase letters in password(s).
 -S, --similar-chars         Include similar characters in password(s).
 -S, --unstrict              Don't require at least one character from
                             each allowed character set in password(s).
 -E, --no-entropy            Don't calculate/log estimated entropy.
 -q, --quiet                 Suppress all logging except errors.

Commands:
 -i, --init                  Create config file (in project root).
 -h, --help                  Display help screen.
 -v, --version               Show version number.
     --stats                 Show npm stats.
     --debug [targetKey]     Show debug logs.
```

#

### Configuration file

**generate-pw** can be customized using a `generate-pw.config.mjs` or `generate-pw.config.js` placed in your project root.

Example defaults:

```js
export default {
    length: 12,                 // length of passwords to generate
    qty: 1,                     // # of passwords to generate
    strength: '',               // <'weak'|'basic'|'strong'> apply strength preset
    charset: '',                // only include chars in password(s)
    exclude: '',                // exclude chars from password(s)
    excludeNums: false,         // disallow numbers in password(s)
    excludeSymbols: false,      // disallow symbols in password(s)
    excludeLowerChars: false,   // disallow lowercase letters in password(s)
    excludeUpperChars: false,   // disallow uppercase letters in password(s)
    similarChars: false,        // include similar chars in password(s)
    unstrict: false,            // don't require 1+ char from each allowed charset in password(s)
    noEntropy: false,           // don't calculate/log estimated entropy
    quietMode: false            // suppress all logging except errors
}
```

💡 Run `generate-pw init` to generate a template `generate-pw.config.mjs` in your project root.

<br>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 🏛️ MIT License

**Copyright © 2024–2026 [Adam Lui](https://github.com/adamlui) & 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.

<br>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

## 🛠️ Related utilities

### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/icons/node-graph/white/icon55x49.png"><img height=21 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/icons/node-graph/black/icon55x49.png"></picture> [generate-ip](https://github.com/adamlui/js-utils/tree/main/generate-ip/#readme) &nbsp;<a href="https://github.com/toolleeo/cli-apps/#networking"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/badges/awesome/badge.svg"></a>

> Randomly generate, format, and validate IPv4 + IPv6 + MAC addresses.
<br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-ip/docs/#-installation) /
[Readme](https://github.com/adamlui/js-utils/tree/main/generate-ip/docs/#readme) /
[API usage](https://github.com/adamlui/js-utils/tree/main/generate-ip/docs/#-api-usage) /
[CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-ip/docs/#-command-line-usage) /
[Discuss](https://github.com/adamlui/js-utils/discussions)

### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/geolocate/assets/images/icons/wire-globe/white/icon32.png"><img height=22 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/geolocate/assets/images/icons/wire-globe/black/icon32.png"></picture> [geolocate](https://github.com/adamlui/js-utils/tree/main/geolocate/#readme) &nbsp;<a href="https://github.com/toolleeo/cli-apps/#networking"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/badges/awesome/badge.svg"></a>

> Fetch IP geolocation data from the CLI.
<br>[Install](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-installation) /
[Readme](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#readme) /
[CLI usage](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-command-line-usage) /
[API usage](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-api-usage) /
[Discuss](https://github.com/adamlui/js-utils/discussions)

<br>

<img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png">

[<picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@21bf981/assets/images/icons/home/white/icon32x27.png"><img height=13 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@21bf981/assets/images/icons/home/dark-gray/icon32x27.png"></picture>][js-utils]
[**More JavaScript utilities**][js-utils] /
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
<a href="https://github.com/adamlui/js-utils/issues">Report bug</a> /
<a href="mailto:security@tidelift.com">Report vulnerability</a> /
<a href="#top">Back to top ↑</a>

[js-utils]: https://github.com/adamlui/js-utils/#readme
