generate-password-lite 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +280 -171
- package/dist/index.cjs +201 -1
- package/dist/index.d.cts +67 -0
- package/dist/index.d.mts +67 -0
- package/dist/index.d.ts +65 -1
- package/dist/index.mjs +175 -1
- package/package.json +71 -57
- package/dist/lib/check-options-error.d.ts +0 -2
- package/dist/lib/generate-random-number.d.ts +0 -1
- package/dist/lib/password-generator.d.ts +0 -2
- package/dist/lib/process-options.d.ts +0 -2
- package/dist/lib/shuffle-string.d.ts +0 -1
- package/dist/types.d.ts +0 -58
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Ahmad Joya
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ahmad Joya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,171 +1,280 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
</
|
|
11
|
-
|
|
12
|
-
<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
</
|
|
26
|
-
|
|
27
|
-
<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
1
|
+
<p align ="center">
|
|
2
|
+
<a href="https://nodei.co/npm/generate-password-lite" target="_blank">
|
|
3
|
+
<img src="https://nodei.co/npm/generate-password-lite.png" alt="npm Info" />
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://npm.im/generate-password-lite" target="_blank">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/generate-password-lite.svg" alt="npm Version" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://npm.im/generate-password-lite" target="_blank">
|
|
12
|
+
<img src="https://img.shields.io/npm/dm/generate-password-lite.svg" alt="npm Downloads" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://bundlephobia.com/package/generate-password-lite" target="_blank">
|
|
15
|
+
<img src="https://img.shields.io/bundlephobia/minzip/generate-password-lite" alt="Bundle Size" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://github.com/ahmadjoya/generate-password-lite/blob/main/LICENSE" target="_blank">
|
|
18
|
+
<img src="https://img.shields.io/npm/l/generate-password-lite.svg" alt="License" />
|
|
19
|
+
</a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
<p align="center">
|
|
23
|
+
<a href="https://github.com/ahmadjoya/generate-password-lite/stargazers" target="_blank">
|
|
24
|
+
<img src="https://img.shields.io/github/stars/ahmadjoya/generate-password-lite" alt="GitHub Stars" />
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://github.com/ahmadjoya/generate-password-lite/network/members" target="_blank">
|
|
27
|
+
<img src="https://img.shields.io/github/forks/ahmadjoya/generate-password-lite" alt="GitHub Forks" />
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://github.com/ahmadjoya/generate-password-lite/issues" target="_blank">
|
|
30
|
+
<img src="https://img.shields.io/github/issues/ahmadjoya/generate-password-lite" alt="GitHub Issues" />
|
|
31
|
+
</a>
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
# generate-password-lite
|
|
35
|
+
|
|
36
|
+
A tiny, dependency-free password generator for JavaScript and TypeScript projects — Node.js, React, Next.js, Vue, Svelte, Deno, Bun, and the browser.
|
|
37
|
+
|
|
38
|
+
Passwords may contain lowercase letters, uppercase letters, numbers and symbols. The `options` parameter lets you enable or disable each group, exclude specific characters, and require a minimum number of characters from each group.
|
|
39
|
+
|
|
40
|
+
- **Secure by default** — every character comes from the Web Crypto CSPRNG (`crypto.getRandomValues`), never `Math.random()`.
|
|
41
|
+
- **Zero dependencies** — nothing else is pulled into your lockfile.
|
|
42
|
+
- **Dual ESM + CommonJS** — `import` and `require` both work, with matching type declarations.
|
|
43
|
+
- **Fully typed** — TypeScript definitions ship with the package.
|
|
44
|
+
|
|
45
|
+
> **Note**
|
|
46
|
+
> This package is also published under the name [`js-generate-password`](https://www.npmjs.com/package/js-generate-password). The two packages are the same library — use whichever name you already depend on.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install generate-password-lite
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
yarn add generate-password-lite
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pnpm add generate-password-lite
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Requirements:** Node.js 18 or newer, or any modern browser. Both provide the Web Crypto API that this package relies on.
|
|
63
|
+
|
|
64
|
+
## Usage
|
|
65
|
+
|
|
66
|
+
### ES modules / TypeScript
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { GeneratePassword } from 'generate-password-lite'
|
|
70
|
+
|
|
71
|
+
const password = GeneratePassword({
|
|
72
|
+
length: 14,
|
|
73
|
+
symbols: true,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
console.log(password) // => "q7#Rk2mV$pLx8w"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
A default export is available too, if you prefer it:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import GeneratePassword from 'generate-password-lite'
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### CommonJS
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
const { GeneratePassword } = require('generate-password-lite')
|
|
89
|
+
|
|
90
|
+
const password = GeneratePassword({ length: 14, symbols: true })
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### React
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
import { useState } from 'react'
|
|
97
|
+
import { GeneratePassword } from 'generate-password-lite'
|
|
98
|
+
|
|
99
|
+
export function PasswordField() {
|
|
100
|
+
const [password, setPassword] = useState(() =>
|
|
101
|
+
GeneratePassword({ length: 16, symbols: true })
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<div>
|
|
106
|
+
<input readOnly value={password} />
|
|
107
|
+
<button onClick={() => setPassword(GeneratePassword({ length: 16, symbols: true }))}>
|
|
108
|
+
Regenerate
|
|
109
|
+
</button>
|
|
110
|
+
</div>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Next.js
|
|
116
|
+
|
|
117
|
+
The package works in both the App Router and the Pages Router, on the server and in the browser.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
// app/api/password/route.ts
|
|
121
|
+
import { GeneratePassword } from 'generate-password-lite'
|
|
122
|
+
|
|
123
|
+
export function GET() {
|
|
124
|
+
return Response.json({ password: GeneratePassword({ length: 20, symbols: true }) })
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Generating a password during render of a **server component** produces a different value on the server and on the client. Generate it in an event handler, in a route handler, or inside `useState`'s initializer in a client component.
|
|
129
|
+
|
|
130
|
+
## Options
|
|
131
|
+
|
|
132
|
+
Every option is optional. Calling `GeneratePassword()` with no argument uses the defaults below.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
GeneratePassword({
|
|
136
|
+
length: 10,
|
|
137
|
+
lowercase: true,
|
|
138
|
+
uppercase: true,
|
|
139
|
+
numbers: true,
|
|
140
|
+
symbols: false,
|
|
141
|
+
exclude: '',
|
|
142
|
+
minLengthLowercase: 1,
|
|
143
|
+
minLengthUppercase: 1,
|
|
144
|
+
minLengthNumbers: 1,
|
|
145
|
+
minLengthSymbols: 0,
|
|
146
|
+
})
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
| Name | Type | Description | Default |
|
|
150
|
+
| -------------------- | --------- | --------------------------------------------------------------------------------- | ------- |
|
|
151
|
+
| `length` | `number` | Length of the generated password. Must be a positive integer. | `10` |
|
|
152
|
+
| `lowercase` | `boolean` | Include lowercase letters (`a–z`). | `true` |
|
|
153
|
+
| `uppercase` | `boolean` | Include uppercase letters (`A–Z`). | `true` |
|
|
154
|
+
| `numbers` | `boolean` | Include digits (`0–9`). | `true` |
|
|
155
|
+
| `symbols` | `boolean` | Include symbols (see [Symbols](#symbols)). | `false` |
|
|
156
|
+
| `exclude` | `string` | Characters to leave out of the password. | `''` |
|
|
157
|
+
| `minLengthLowercase` | `number` | Minimum number of lowercase letters. Forced to `0` when `lowercase` is `false`. | `1` |
|
|
158
|
+
| `minLengthUppercase` | `number` | Minimum number of uppercase letters. Forced to `0` when `uppercase` is `false`. | `1` |
|
|
159
|
+
| `minLengthNumbers` | `number` | Minimum number of digits. Forced to `0` when `numbers` is `false`. | `1` |
|
|
160
|
+
| `minLengthSymbols` | `number` | Minimum number of symbols. Forced to `0` when `symbols` is `false`. | `1`\* |
|
|
161
|
+
|
|
162
|
+
At least one of `lowercase`, `uppercase`, `numbers` or `symbols` must be `true`.
|
|
163
|
+
|
|
164
|
+
\* `minLengthSymbols` defaults to `1` whenever you turn `symbols` on. Because `symbols` is `false` by default, the effective default for an untouched call is `0`.
|
|
165
|
+
|
|
166
|
+
### Symbols
|
|
167
|
+
|
|
168
|
+
When `symbols` is enabled the following characters are used:
|
|
169
|
+
|
|
170
|
+
```text
|
|
171
|
+
!#$%&'()*+,-./:;<=>?@[]^_{|}~
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Examples
|
|
175
|
+
|
|
176
|
+
**No options.** Defaults apply: 10 characters, upper and lowercase letters plus digits, no symbols.
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { GeneratePassword } from 'generate-password-lite'
|
|
180
|
+
|
|
181
|
+
console.log(GeneratePassword())
|
|
182
|
+
// => "xDU6izb3PV"
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**A longer password.**
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
console.log(GeneratePassword({ length: 25 }))
|
|
189
|
+
// => "U4c3KpQP5UrbZgTcrqMgFeI3R"
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
**Excluding characters.** Useful for dropping look-alikes such as `O`/`0` and `l`/`1`.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
console.log(GeneratePassword({ length: 16, exclude: 'Ol01Il' }))
|
|
196
|
+
// => "gT7yqW4nZbK9vRhs"
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Requiring a mix.** Guarantee at least two of each group.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
console.log(
|
|
203
|
+
GeneratePassword({
|
|
204
|
+
length: 20,
|
|
205
|
+
symbols: true,
|
|
206
|
+
minLengthLowercase: 2,
|
|
207
|
+
minLengthUppercase: 2,
|
|
208
|
+
minLengthNumbers: 2,
|
|
209
|
+
minLengthSymbols: 2,
|
|
210
|
+
})
|
|
211
|
+
)
|
|
212
|
+
// => "k#9Wq2mZ$vT7pL4nRj%x"
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**Digits only, for a one-time code.**
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
console.log(
|
|
219
|
+
GeneratePassword({
|
|
220
|
+
length: 6,
|
|
221
|
+
lowercase: false,
|
|
222
|
+
uppercase: false,
|
|
223
|
+
numbers: true,
|
|
224
|
+
symbols: false,
|
|
225
|
+
})
|
|
226
|
+
)
|
|
227
|
+
// => "482915"
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Errors
|
|
231
|
+
|
|
232
|
+
`GeneratePassword` throws an `Error` when the options cannot produce a valid password:
|
|
233
|
+
|
|
234
|
+
| Condition | Example |
|
|
235
|
+
| ---------------------------------------------------------------------------- | -------------------------------------------------------------------- |
|
|
236
|
+
| `length` is not a positive integer | `GeneratePassword({ length: 0 })` |
|
|
237
|
+
| A `minLength*` value is not a non-negative integer | `GeneratePassword({ minLengthNumbers: -1 })` |
|
|
238
|
+
| Every character group is disabled | `GeneratePassword({ lowercase: false, uppercase: false, numbers: false, symbols: false })` |
|
|
239
|
+
| The `minLength*` values add up to more than `length` | `GeneratePassword({ length: 4, symbols: true, minLengthSymbols: 5 })` |
|
|
240
|
+
| `exclude` removes every character of a group that still has a minimum | `GeneratePassword({ exclude: '0123456789' })` — digits are on by default |
|
|
241
|
+
|
|
242
|
+
The last case is worth calling out: excluding all digits while `numbers` is still `true` is a contradiction, so it fails loudly instead of quietly returning a password with no digits.
|
|
243
|
+
|
|
244
|
+
## Security
|
|
245
|
+
|
|
246
|
+
Passwords are generated with `crypto.getRandomValues`, the cryptographically secure random source built into Node.js 18+ and every modern browser. Values are drawn using rejection sampling, so each character in the allowed set is equally likely — a plain modulo would skew the results toward the start of the alphabet.
|
|
247
|
+
|
|
248
|
+
The characters that satisfy your `minLength*` requirements are shuffled through the whole password rather than placed at the front, so the position of each group carries no information.
|
|
249
|
+
|
|
250
|
+
If `globalThis.crypto` is unavailable, the package throws rather than silently falling back to a predictable random source.
|
|
251
|
+
|
|
252
|
+
## TypeScript
|
|
253
|
+
|
|
254
|
+
The `GenerateOptions` type is exported for reuse:
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
import { GeneratePassword, type GenerateOptions } from 'generate-password-lite'
|
|
258
|
+
|
|
259
|
+
const strongDefaults: GenerateOptions = {
|
|
260
|
+
length: 24,
|
|
261
|
+
symbols: true,
|
|
262
|
+
minLengthSymbols: 3,
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const password = GeneratePassword(strongDefaults)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Contributing
|
|
269
|
+
|
|
270
|
+
Issues and pull requests are welcome at [github.com/ahmadjoya/generate-password-lite](https://github.com/ahmadjoya/generate-password-lite).
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
npm install
|
|
274
|
+
npm run build
|
|
275
|
+
npm test
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
[MIT](https://github.com/ahmadjoya/generate-password-lite/blob/main/LICENSE) © Ahmad Joya
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,201 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* generate-password-lite v1.1.1
|
|
3
|
+
* Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var index_exports = {};
|
|
27
|
+
__export(index_exports, {
|
|
28
|
+
GeneratePassword: () => GeneratePassword,
|
|
29
|
+
default: () => index_default
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// src/lib/generate-random-number.ts
|
|
34
|
+
var UINT32_RANGE = 4294967296;
|
|
35
|
+
var getWebCrypto = () => {
|
|
36
|
+
const webCrypto = globalThis.crypto;
|
|
37
|
+
if (!webCrypto || typeof webCrypto.getRandomValues !== "function") {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"generate-password-lite: no Web Crypto implementation found. A secure random source is required to generate passwords. Use Node.js 18+, a modern browser, or polyfill `globalThis.crypto`."
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return webCrypto;
|
|
43
|
+
};
|
|
44
|
+
var generateRandomNumbers = (upperLimit) => {
|
|
45
|
+
if (!Number.isInteger(upperLimit) || upperLimit <= 0) {
|
|
46
|
+
throw new RangeError(
|
|
47
|
+
`generate-password-lite: upperLimit must be a positive integer, received ${upperLimit}.`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const webCrypto = getWebCrypto();
|
|
51
|
+
const buffer = new Uint32Array(1);
|
|
52
|
+
const cutoff = Math.floor(UINT32_RANGE / upperLimit) * upperLimit;
|
|
53
|
+
let value;
|
|
54
|
+
do {
|
|
55
|
+
webCrypto.getRandomValues(buffer);
|
|
56
|
+
value = buffer[0];
|
|
57
|
+
} while (value >= cutoff);
|
|
58
|
+
return value % upperLimit;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/lib/shuffle-string.ts
|
|
62
|
+
var shuffleString = (str) => {
|
|
63
|
+
const chars = [...str];
|
|
64
|
+
for (let i = chars.length - 1; i > 0; i--) {
|
|
65
|
+
const j = generateRandomNumbers(i + 1);
|
|
66
|
+
const tmp = chars[i];
|
|
67
|
+
chars[i] = chars[j];
|
|
68
|
+
chars[j] = tmp;
|
|
69
|
+
}
|
|
70
|
+
return chars.join("");
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/lib/password-generator.ts
|
|
74
|
+
var LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
|
|
75
|
+
var UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
76
|
+
var NUMBER_CHARS = "0123456789";
|
|
77
|
+
var SYMBOL_CHARS = "!#$%&'()*+,-./:;<=>?@[]^_{|}~";
|
|
78
|
+
var removeExceptions = (str, exclude) => {
|
|
79
|
+
if (!exclude) return str;
|
|
80
|
+
const banned = /* @__PURE__ */ new Set([...exclude]);
|
|
81
|
+
return [...str].filter((char) => !banned.has(char)).join("");
|
|
82
|
+
};
|
|
83
|
+
var pickFrom = (pool, count, name) => {
|
|
84
|
+
if (count === 0) return "";
|
|
85
|
+
if (pool.length === 0) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`generate-password-lite: \`exclude\` removed every ${name} character, but \`minLength${name[0].toUpperCase()}${name.slice(1)}\` is ${count}.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
let picked = "";
|
|
91
|
+
for (let i = 0; i < count; i++) {
|
|
92
|
+
picked += pool.charAt(generateRandomNumbers(pool.length));
|
|
93
|
+
}
|
|
94
|
+
return picked;
|
|
95
|
+
};
|
|
96
|
+
var generatePswd = (options) => {
|
|
97
|
+
const {
|
|
98
|
+
length,
|
|
99
|
+
lowercase,
|
|
100
|
+
uppercase,
|
|
101
|
+
numbers,
|
|
102
|
+
symbols,
|
|
103
|
+
minLengthLowercase,
|
|
104
|
+
minLengthUppercase,
|
|
105
|
+
minLengthNumbers,
|
|
106
|
+
minLengthSymbols,
|
|
107
|
+
exclude
|
|
108
|
+
} = options;
|
|
109
|
+
const lowercaseChar = removeExceptions(LOWERCASE_CHARS, exclude);
|
|
110
|
+
const uppercaseChar = removeExceptions(UPPERCASE_CHARS, exclude);
|
|
111
|
+
const numberChar = removeExceptions(NUMBER_CHARS, exclude);
|
|
112
|
+
const symbolChar = removeExceptions(SYMBOL_CHARS, exclude);
|
|
113
|
+
const guaranteed = pickFrom(lowercaseChar, minLengthLowercase, "lowercase") + pickFrom(uppercaseChar, minLengthUppercase, "uppercase") + pickFrom(numberChar, minLengthNumbers, "numbers") + pickFrom(symbolChar, minLengthSymbols, "symbols");
|
|
114
|
+
const characterPool = (lowercase ? lowercaseChar : "") + (uppercase ? uppercaseChar : "") + (numbers ? numberChar : "") + (symbols ? symbolChar : "");
|
|
115
|
+
const remaining = length - guaranteed.length;
|
|
116
|
+
if (remaining > 0 && characterPool.length === 0) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"generate-password-lite: `exclude` removed every character of every enabled class. No valid characters to generate the password from."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
let filler = "";
|
|
122
|
+
for (let i = 0; i < remaining; i++) {
|
|
123
|
+
filler += characterPool.charAt(generateRandomNumbers(characterPool.length));
|
|
124
|
+
}
|
|
125
|
+
return shuffleString(guaranteed + filler);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/lib/check-options-error.ts
|
|
129
|
+
var assertCount = (value, name) => {
|
|
130
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`generate-password-lite: \`${name}\` must be a non-negative integer, received ${value}.`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
var checkError = (options) => {
|
|
137
|
+
const {
|
|
138
|
+
length,
|
|
139
|
+
lowercase,
|
|
140
|
+
uppercase,
|
|
141
|
+
numbers,
|
|
142
|
+
symbols,
|
|
143
|
+
minLengthLowercase,
|
|
144
|
+
minLengthUppercase,
|
|
145
|
+
minLengthNumbers,
|
|
146
|
+
minLengthSymbols
|
|
147
|
+
} = options;
|
|
148
|
+
if (!Number.isInteger(length) || length < 1) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`generate-password-lite: \`length\` must be a positive integer, received ${length}.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
assertCount(minLengthLowercase, "minLengthLowercase");
|
|
154
|
+
assertCount(minLengthUppercase, "minLengthUppercase");
|
|
155
|
+
assertCount(minLengthNumbers, "minLengthNumbers");
|
|
156
|
+
assertCount(minLengthSymbols, "minLengthSymbols");
|
|
157
|
+
if (!lowercase && !uppercase && !numbers && !symbols) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
"generate-password-lite: `lowercase`, `uppercase`, `numbers` and `symbols` are all false. No valid characters to generate the password from."
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
const minimumTotal = minLengthLowercase + minLengthUppercase + minLengthNumbers + minLengthSymbols;
|
|
163
|
+
if (minimumTotal > length) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`generate-password-lite: the sum of the minimum character counts (${minimumTotal}) is greater than \`length\` (${length}).`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/lib/process-options.ts
|
|
171
|
+
var withDefault = (value, fallback) => typeof value === "undefined" ? fallback : value;
|
|
172
|
+
var processOption = (options) => {
|
|
173
|
+
const lowercase = withDefault(options.lowercase, true);
|
|
174
|
+
const uppercase = withDefault(options.uppercase, true);
|
|
175
|
+
const numbers = withDefault(options.numbers, true);
|
|
176
|
+
const symbols = withDefault(options.symbols, false);
|
|
177
|
+
return {
|
|
178
|
+
length: withDefault(options.length, 10),
|
|
179
|
+
lowercase,
|
|
180
|
+
uppercase,
|
|
181
|
+
numbers,
|
|
182
|
+
symbols,
|
|
183
|
+
exclude: withDefault(options.exclude, ""),
|
|
184
|
+
minLengthLowercase: lowercase ? withDefault(options.minLengthLowercase, 1) : 0,
|
|
185
|
+
minLengthUppercase: uppercase ? withDefault(options.minLengthUppercase, 1) : 0,
|
|
186
|
+
minLengthNumbers: numbers ? withDefault(options.minLengthNumbers, 1) : 0,
|
|
187
|
+
minLengthSymbols: symbols ? withDefault(options.minLengthSymbols, 1) : 0
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// src/index.ts
|
|
192
|
+
function GeneratePassword(options = {}) {
|
|
193
|
+
const resolved = processOption(options);
|
|
194
|
+
checkError(resolved);
|
|
195
|
+
return generatePswd(resolved);
|
|
196
|
+
}
|
|
197
|
+
var index_default = GeneratePassword;
|
|
198
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
199
|
+
0 && (module.exports = {
|
|
200
|
+
GeneratePassword
|
|
201
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* generate-password-lite v1.1.1
|
|
3
|
+
* Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
export interface GenerateOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Length of the generated password.
|
|
9
|
+
* @default 10
|
|
10
|
+
*/
|
|
11
|
+
length?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Should the password include lowercase characters.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
lowercase?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Should the password include uppercase characters.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
uppercase?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Should the password include numbers.
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
numbers?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Should the password include symbol characters.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
symbols?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Characters to exclude from the password.
|
|
34
|
+
* @default ""
|
|
35
|
+
*/
|
|
36
|
+
exclude?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
|
|
39
|
+
* @default 1 when `lowercase` is true, otherwise 0
|
|
40
|
+
*/
|
|
41
|
+
minLengthLowercase?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
|
|
44
|
+
* @default 1 when `uppercase` is true, otherwise 0
|
|
45
|
+
*/
|
|
46
|
+
minLengthUppercase?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Minimum number of numeric characters. Forced to 0 when `numbers` is false.
|
|
49
|
+
* @default 1 when `numbers` is true, otherwise 0
|
|
50
|
+
*/
|
|
51
|
+
minLengthNumbers?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Minimum number of symbol characters. Forced to 0 when `symbols` is false.
|
|
54
|
+
* @default 1 when `symbols` is true, otherwise 0
|
|
55
|
+
*/
|
|
56
|
+
minLengthSymbols?: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generates a random password using a cryptographically secure random source.
|
|
60
|
+
*
|
|
61
|
+
* @param options - Optional overrides. Every unset option falls back to its
|
|
62
|
+
* documented default.
|
|
63
|
+
* @returns The generated password.
|
|
64
|
+
*/
|
|
65
|
+
declare function GeneratePassword(options?: GenerateOptions): string;
|
|
66
|
+
export { GeneratePassword, type GenerateOptions };
|
|
67
|
+
export default GeneratePassword;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* generate-password-lite v1.1.1
|
|
3
|
+
* Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
export interface GenerateOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Length of the generated password.
|
|
9
|
+
* @default 10
|
|
10
|
+
*/
|
|
11
|
+
length?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Should the password include lowercase characters.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
lowercase?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Should the password include uppercase characters.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
uppercase?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Should the password include numbers.
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
numbers?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Should the password include symbol characters.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
symbols?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Characters to exclude from the password.
|
|
34
|
+
* @default ""
|
|
35
|
+
*/
|
|
36
|
+
exclude?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
|
|
39
|
+
* @default 1 when `lowercase` is true, otherwise 0
|
|
40
|
+
*/
|
|
41
|
+
minLengthLowercase?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
|
|
44
|
+
* @default 1 when `uppercase` is true, otherwise 0
|
|
45
|
+
*/
|
|
46
|
+
minLengthUppercase?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Minimum number of numeric characters. Forced to 0 when `numbers` is false.
|
|
49
|
+
* @default 1 when `numbers` is true, otherwise 0
|
|
50
|
+
*/
|
|
51
|
+
minLengthNumbers?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Minimum number of symbol characters. Forced to 0 when `symbols` is false.
|
|
54
|
+
* @default 1 when `symbols` is true, otherwise 0
|
|
55
|
+
*/
|
|
56
|
+
minLengthSymbols?: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generates a random password using a cryptographically secure random source.
|
|
60
|
+
*
|
|
61
|
+
* @param options - Optional overrides. Every unset option falls back to its
|
|
62
|
+
* documented default.
|
|
63
|
+
* @returns The generated password.
|
|
64
|
+
*/
|
|
65
|
+
declare function GeneratePassword(options?: GenerateOptions): string;
|
|
66
|
+
export { GeneratePassword, type GenerateOptions };
|
|
67
|
+
export default GeneratePassword;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* generate-password-lite v1.1.1
|
|
3
|
+
* Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
export interface GenerateOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Length of the generated password.
|
|
9
|
+
* @default 10
|
|
10
|
+
*/
|
|
11
|
+
length?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Should the password include lowercase characters.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
lowercase?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Should the password include uppercase characters.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
uppercase?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Should the password include numbers.
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
numbers?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Should the password include symbol characters.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
symbols?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Characters to exclude from the password.
|
|
34
|
+
* @default ""
|
|
35
|
+
*/
|
|
36
|
+
exclude?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
|
|
39
|
+
* @default 1 when `lowercase` is true, otherwise 0
|
|
40
|
+
*/
|
|
41
|
+
minLengthLowercase?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
|
|
44
|
+
* @default 1 when `uppercase` is true, otherwise 0
|
|
45
|
+
*/
|
|
46
|
+
minLengthUppercase?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Minimum number of numeric characters. Forced to 0 when `numbers` is false.
|
|
49
|
+
* @default 1 when `numbers` is true, otherwise 0
|
|
50
|
+
*/
|
|
51
|
+
minLengthNumbers?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Minimum number of symbol characters. Forced to 0 when `symbols` is false.
|
|
54
|
+
* @default 1 when `symbols` is true, otherwise 0
|
|
55
|
+
*/
|
|
56
|
+
minLengthSymbols?: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generates a random password using a cryptographically secure random source.
|
|
60
|
+
*
|
|
61
|
+
* @param options - Optional overrides. Every unset option falls back to its
|
|
62
|
+
* documented default.
|
|
63
|
+
* @returns The generated password.
|
|
64
|
+
*/
|
|
2
65
|
declare function GeneratePassword(options?: GenerateOptions): string;
|
|
3
66
|
export { GeneratePassword, type GenerateOptions };
|
|
67
|
+
export default GeneratePassword;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,175 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* generate-password-lite v1.1.1
|
|
3
|
+
* Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// src/lib/generate-random-number.ts
|
|
8
|
+
var UINT32_RANGE = 4294967296;
|
|
9
|
+
var getWebCrypto = () => {
|
|
10
|
+
const webCrypto = globalThis.crypto;
|
|
11
|
+
if (!webCrypto || typeof webCrypto.getRandomValues !== "function") {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"generate-password-lite: no Web Crypto implementation found. A secure random source is required to generate passwords. Use Node.js 18+, a modern browser, or polyfill `globalThis.crypto`."
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
return webCrypto;
|
|
17
|
+
};
|
|
18
|
+
var generateRandomNumbers = (upperLimit) => {
|
|
19
|
+
if (!Number.isInteger(upperLimit) || upperLimit <= 0) {
|
|
20
|
+
throw new RangeError(
|
|
21
|
+
`generate-password-lite: upperLimit must be a positive integer, received ${upperLimit}.`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const webCrypto = getWebCrypto();
|
|
25
|
+
const buffer = new Uint32Array(1);
|
|
26
|
+
const cutoff = Math.floor(UINT32_RANGE / upperLimit) * upperLimit;
|
|
27
|
+
let value;
|
|
28
|
+
do {
|
|
29
|
+
webCrypto.getRandomValues(buffer);
|
|
30
|
+
value = buffer[0];
|
|
31
|
+
} while (value >= cutoff);
|
|
32
|
+
return value % upperLimit;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/lib/shuffle-string.ts
|
|
36
|
+
var shuffleString = (str) => {
|
|
37
|
+
const chars = [...str];
|
|
38
|
+
for (let i = chars.length - 1; i > 0; i--) {
|
|
39
|
+
const j = generateRandomNumbers(i + 1);
|
|
40
|
+
const tmp = chars[i];
|
|
41
|
+
chars[i] = chars[j];
|
|
42
|
+
chars[j] = tmp;
|
|
43
|
+
}
|
|
44
|
+
return chars.join("");
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/lib/password-generator.ts
|
|
48
|
+
var LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
|
|
49
|
+
var UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
50
|
+
var NUMBER_CHARS = "0123456789";
|
|
51
|
+
var SYMBOL_CHARS = "!#$%&'()*+,-./:;<=>?@[]^_{|}~";
|
|
52
|
+
var removeExceptions = (str, exclude) => {
|
|
53
|
+
if (!exclude) return str;
|
|
54
|
+
const banned = /* @__PURE__ */ new Set([...exclude]);
|
|
55
|
+
return [...str].filter((char) => !banned.has(char)).join("");
|
|
56
|
+
};
|
|
57
|
+
var pickFrom = (pool, count, name) => {
|
|
58
|
+
if (count === 0) return "";
|
|
59
|
+
if (pool.length === 0) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`generate-password-lite: \`exclude\` removed every ${name} character, but \`minLength${name[0].toUpperCase()}${name.slice(1)}\` is ${count}.`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
let picked = "";
|
|
65
|
+
for (let i = 0; i < count; i++) {
|
|
66
|
+
picked += pool.charAt(generateRandomNumbers(pool.length));
|
|
67
|
+
}
|
|
68
|
+
return picked;
|
|
69
|
+
};
|
|
70
|
+
var generatePswd = (options) => {
|
|
71
|
+
const {
|
|
72
|
+
length,
|
|
73
|
+
lowercase,
|
|
74
|
+
uppercase,
|
|
75
|
+
numbers,
|
|
76
|
+
symbols,
|
|
77
|
+
minLengthLowercase,
|
|
78
|
+
minLengthUppercase,
|
|
79
|
+
minLengthNumbers,
|
|
80
|
+
minLengthSymbols,
|
|
81
|
+
exclude
|
|
82
|
+
} = options;
|
|
83
|
+
const lowercaseChar = removeExceptions(LOWERCASE_CHARS, exclude);
|
|
84
|
+
const uppercaseChar = removeExceptions(UPPERCASE_CHARS, exclude);
|
|
85
|
+
const numberChar = removeExceptions(NUMBER_CHARS, exclude);
|
|
86
|
+
const symbolChar = removeExceptions(SYMBOL_CHARS, exclude);
|
|
87
|
+
const guaranteed = pickFrom(lowercaseChar, minLengthLowercase, "lowercase") + pickFrom(uppercaseChar, minLengthUppercase, "uppercase") + pickFrom(numberChar, minLengthNumbers, "numbers") + pickFrom(symbolChar, minLengthSymbols, "symbols");
|
|
88
|
+
const characterPool = (lowercase ? lowercaseChar : "") + (uppercase ? uppercaseChar : "") + (numbers ? numberChar : "") + (symbols ? symbolChar : "");
|
|
89
|
+
const remaining = length - guaranteed.length;
|
|
90
|
+
if (remaining > 0 && characterPool.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"generate-password-lite: `exclude` removed every character of every enabled class. No valid characters to generate the password from."
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
let filler = "";
|
|
96
|
+
for (let i = 0; i < remaining; i++) {
|
|
97
|
+
filler += characterPool.charAt(generateRandomNumbers(characterPool.length));
|
|
98
|
+
}
|
|
99
|
+
return shuffleString(guaranteed + filler);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// src/lib/check-options-error.ts
|
|
103
|
+
var assertCount = (value, name) => {
|
|
104
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`generate-password-lite: \`${name}\` must be a non-negative integer, received ${value}.`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var checkError = (options) => {
|
|
111
|
+
const {
|
|
112
|
+
length,
|
|
113
|
+
lowercase,
|
|
114
|
+
uppercase,
|
|
115
|
+
numbers,
|
|
116
|
+
symbols,
|
|
117
|
+
minLengthLowercase,
|
|
118
|
+
minLengthUppercase,
|
|
119
|
+
minLengthNumbers,
|
|
120
|
+
minLengthSymbols
|
|
121
|
+
} = options;
|
|
122
|
+
if (!Number.isInteger(length) || length < 1) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`generate-password-lite: \`length\` must be a positive integer, received ${length}.`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
assertCount(minLengthLowercase, "minLengthLowercase");
|
|
128
|
+
assertCount(minLengthUppercase, "minLengthUppercase");
|
|
129
|
+
assertCount(minLengthNumbers, "minLengthNumbers");
|
|
130
|
+
assertCount(minLengthSymbols, "minLengthSymbols");
|
|
131
|
+
if (!lowercase && !uppercase && !numbers && !symbols) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
"generate-password-lite: `lowercase`, `uppercase`, `numbers` and `symbols` are all false. No valid characters to generate the password from."
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
const minimumTotal = minLengthLowercase + minLengthUppercase + minLengthNumbers + minLengthSymbols;
|
|
137
|
+
if (minimumTotal > length) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`generate-password-lite: the sum of the minimum character counts (${minimumTotal}) is greater than \`length\` (${length}).`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/lib/process-options.ts
|
|
145
|
+
var withDefault = (value, fallback) => typeof value === "undefined" ? fallback : value;
|
|
146
|
+
var processOption = (options) => {
|
|
147
|
+
const lowercase = withDefault(options.lowercase, true);
|
|
148
|
+
const uppercase = withDefault(options.uppercase, true);
|
|
149
|
+
const numbers = withDefault(options.numbers, true);
|
|
150
|
+
const symbols = withDefault(options.symbols, false);
|
|
151
|
+
return {
|
|
152
|
+
length: withDefault(options.length, 10),
|
|
153
|
+
lowercase,
|
|
154
|
+
uppercase,
|
|
155
|
+
numbers,
|
|
156
|
+
symbols,
|
|
157
|
+
exclude: withDefault(options.exclude, ""),
|
|
158
|
+
minLengthLowercase: lowercase ? withDefault(options.minLengthLowercase, 1) : 0,
|
|
159
|
+
minLengthUppercase: uppercase ? withDefault(options.minLengthUppercase, 1) : 0,
|
|
160
|
+
minLengthNumbers: numbers ? withDefault(options.minLengthNumbers, 1) : 0,
|
|
161
|
+
minLengthSymbols: symbols ? withDefault(options.minLengthSymbols, 1) : 0
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/index.ts
|
|
166
|
+
function GeneratePassword(options = {}) {
|
|
167
|
+
const resolved = processOption(options);
|
|
168
|
+
checkError(resolved);
|
|
169
|
+
return generatePswd(resolved);
|
|
170
|
+
}
|
|
171
|
+
var index_default = GeneratePassword;
|
|
172
|
+
export {
|
|
173
|
+
GeneratePassword,
|
|
174
|
+
index_default as default
|
|
175
|
+
};
|
package/package.json
CHANGED
|
@@ -1,57 +1,71 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "generate-password-lite",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"private": false,
|
|
5
|
-
"description": "
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "generate-password-lite",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.",
|
|
6
|
+
"author": "Ahmad Joya <joya.abn@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.cts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"default": "./dist/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"require": {
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
21
|
+
"default": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node scripts/build.mjs",
|
|
34
|
+
"typecheck": "tsc --noEmit false -p tsconfig.json --emitDeclarationOnly false --noEmit",
|
|
35
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
36
|
+
"check": "npm run build && npm run test && publint",
|
|
37
|
+
"prepublishOnly": "npm run build",
|
|
38
|
+
"build:alias": "node scripts/pack-alias.mjs",
|
|
39
|
+
"publish:alias": "node scripts/pack-alias.mjs --publish"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/ahmadjoya/generate-password-lite.git"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"password",
|
|
47
|
+
"password-generator",
|
|
48
|
+
"generate-password",
|
|
49
|
+
"generate-password-lite",
|
|
50
|
+
"js-generate-password",
|
|
51
|
+
"random-password",
|
|
52
|
+
"secure-password",
|
|
53
|
+
"crypto",
|
|
54
|
+
"csprng",
|
|
55
|
+
"generator",
|
|
56
|
+
"random",
|
|
57
|
+
"typescript",
|
|
58
|
+
"nodejs",
|
|
59
|
+
"react",
|
|
60
|
+
"nextjs",
|
|
61
|
+
"browser"
|
|
62
|
+
],
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/ahmadjoya/generate-password-lite/issues"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/ahmadjoya/generate-password-lite#readme",
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
69
|
+
"typescript": "^7.0.2"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const generateRandomNumbers: (upperLimit: number) => number;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const shuffleString: (str: string) => string;
|
package/dist/types.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
interface GenerateOptions {
|
|
2
|
-
/**
|
|
3
|
-
* lenght of the password
|
|
4
|
-
* @default 10
|
|
5
|
-
*/
|
|
6
|
-
length?: number
|
|
7
|
-
/**
|
|
8
|
-
* Should the password include lowercase characters
|
|
9
|
-
* @default true
|
|
10
|
-
*/
|
|
11
|
-
lowercase?: boolean
|
|
12
|
-
/**
|
|
13
|
-
* Should the password include uppercase characters
|
|
14
|
-
* @default true
|
|
15
|
-
*/
|
|
16
|
-
uppercase?: boolean
|
|
17
|
-
/**
|
|
18
|
-
* Should the password include numbers
|
|
19
|
-
* @default true
|
|
20
|
-
*/
|
|
21
|
-
numbers?: boolean
|
|
22
|
-
/**
|
|
23
|
-
* Should the password include symbol characters
|
|
24
|
-
* @default false
|
|
25
|
-
*/
|
|
26
|
-
symbols?: boolean
|
|
27
|
-
/**
|
|
28
|
-
* List of characters to be excluded from the password
|
|
29
|
-
* @default ""
|
|
30
|
-
*/
|
|
31
|
-
exclude?: string
|
|
32
|
-
/**
|
|
33
|
-
* min lentgh of lowercase character
|
|
34
|
-
* @default 1
|
|
35
|
-
*/
|
|
36
|
-
minLengthLowercase?: number
|
|
37
|
-
/**
|
|
38
|
-
* min lentgh of uppercase character
|
|
39
|
-
* @default 1
|
|
40
|
-
*/
|
|
41
|
-
minLengthUppercase?: number
|
|
42
|
-
/**
|
|
43
|
-
* min length of numbers
|
|
44
|
-
* @default 1
|
|
45
|
-
*/
|
|
46
|
-
minLengthNumbers?: number
|
|
47
|
-
/**
|
|
48
|
-
* min length of symbols
|
|
49
|
-
* @default 0
|
|
50
|
-
*/
|
|
51
|
-
minLengthSymbols?: number
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
declare namespace GeneratePassword {}
|
|
55
|
-
|
|
56
|
-
declare function GeneratePassword(options?: GenerateOptions): string
|
|
57
|
-
|
|
58
|
-
export { type GenerateOptions, GeneratePassword };
|