generate-pw 1.3.0 → 1.4.0
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/README.md +81 -22
- package/dist/cli.min.js +6 -0
- package/dist/generate-pw.min.js +1 -14
- package/docs/LICENSE.md +1 -1
- package/package.json +10 -6
package/README.md
CHANGED
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
### Randomly generate cryptographically-secure passwords.
|
|
18
18
|
|
|
19
19
|
<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>
|
|
20
|
-
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-pw-1.
|
|
20
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-pw-1.4.0"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.0-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
21
21
|
<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>
|
|
22
|
-
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-pw/generate-pw.js"><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%2Fgenerate-pw.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
22
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-pw/src/generate-pw.js"><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=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
23
23
|
|
|
24
24
|
<br>
|
|
25
25
|
|
|
@@ -43,9 +43,9 @@ $ npm install generate-pw
|
|
|
43
43
|
|
|
44
44
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
45
45
|
|
|
46
|
-
## 🔌 API
|
|
46
|
+
## 🔌 Importing the API
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
49
49
|
|
|
50
50
|
#### ESM:
|
|
51
51
|
|
|
@@ -59,7 +59,58 @@ import * as pw from 'generate-pw';
|
|
|
59
59
|
const pw = require('generate-pw');
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/JSEb19A.png"><img width=16 src="https://i.imgur.com/5VPxf9y.png"></picture> Web
|
|
63
|
+
|
|
64
|
+
#### <> HTML script tag:
|
|
65
|
+
|
|
66
|
+
```html
|
|
67
|
+
<script src="https://cdn.jsdelivr.net/npm/generate-pw@1.4.0/dist/generate-pw.min.js"></script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### ES6:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
(async () => {
|
|
74
|
+
await import('https://cdn.jsdelivr.net/npm/generate-pw@1.4.0/dist/generate-pw.min.js');
|
|
75
|
+
// Your code here...
|
|
76
|
+
})();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### ES5:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
var xhr = new XMLHttpRequest();
|
|
83
|
+
xhr.open('GET', 'https://cdn.jsdelivr.net/npm/generate-pw@1.4.0/dist/generate-pw.min.js');
|
|
84
|
+
xhr.onload = function() {
|
|
85
|
+
if (xhr.status === 200) {
|
|
86
|
+
var generateIP = document.createElement('script');
|
|
87
|
+
generateIP.textContent = xhr.responseText;
|
|
88
|
+
document.head.append(generateIP);
|
|
89
|
+
// Your code here...
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
xhr.send();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### <img height=17 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/tampermonkey-icon28.png"><img height=17.5 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/violentmonkey-icon100.png"> Greasemonkey
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
...
|
|
99
|
+
// @require https://cdn.jsdelivr.net/npm/generate-pw@1.4.0/dist/generate-pw.min.js
|
|
100
|
+
// ==/UserScript==
|
|
101
|
+
|
|
102
|
+
// Your code here...
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
<br>
|
|
106
|
+
|
|
107
|
+
**💡 Note:** To always import the latest version (not recommended in production!) remove the `@1.3.1` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-pw/dist/generate-pw.min.js`
|
|
108
|
+
|
|
109
|
+
<br>
|
|
110
|
+
|
|
111
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
112
|
+
|
|
113
|
+
## 📋 API usage
|
|
63
114
|
|
|
64
115
|
### `generatePassword([options])`
|
|
65
116
|
|
|
@@ -67,8 +118,7 @@ Generates **one** password if `qty` option is not given, returning a string:
|
|
|
67
118
|
|
|
68
119
|
```js
|
|
69
120
|
const password = pw.generatePassword({ length: 11, numbers: true });
|
|
70
|
-
console.log(password);
|
|
71
|
-
// sample output: 'bAsZm3mq6Qn'
|
|
121
|
+
console.log(password); // sample output: 'bAsZm3mq6Qn'
|
|
72
122
|
```
|
|
73
123
|
|
|
74
124
|
...or **multiple** passwords if `qty` option is given, returning an array of strings:
|
|
@@ -76,7 +126,14 @@ console.log(password);
|
|
|
76
126
|
```js
|
|
77
127
|
const passwords = pw.generatePassword({ qty: 5, length: 8, symbols: true });
|
|
78
128
|
console.log(passwords);
|
|
79
|
-
|
|
129
|
+
|
|
130
|
+
/* sample output:
|
|
131
|
+
|
|
132
|
+
generatePassword() » Generating passwords...
|
|
133
|
+
generatePassword() » Passwords generated!
|
|
134
|
+
generatePassword() » Check returned array.
|
|
135
|
+
[ '!zSf@Q.s', '!,HT\\;m=', '?Lq&FV>^', 'gf}Y;}Ne', 'Stsx(GqE' ]
|
|
136
|
+
*/
|
|
80
137
|
```
|
|
81
138
|
|
|
82
139
|
**💡 Note:** If no options are passed, passwords will be 8-chars long, consisting of upper/lower cased letters.
|
|
@@ -92,7 +149,14 @@ Generates **multiple** passwords based on `qty` given, returning an array of str
|
|
|
92
149
|
```js
|
|
93
150
|
const passwords = pw.generatePasswords(5, { length: 3, uppercase: false });
|
|
94
151
|
console.log(passwords);
|
|
95
|
-
|
|
152
|
+
|
|
153
|
+
/* sample output:
|
|
154
|
+
|
|
155
|
+
generatePasswords() » Generating passwords...
|
|
156
|
+
generatePasswords() » Passwords generated!
|
|
157
|
+
generatePasswords() » Check returned array.
|
|
158
|
+
[ 'yilppxru', 'ckvkyjfp', 'zolcpyfb' ]
|
|
159
|
+
*/
|
|
96
160
|
```
|
|
97
161
|
|
|
98
162
|
**💡 Note:** If no `qty` arg is passed, just one password will be generated, returned as a string.
|
|
@@ -107,14 +171,7 @@ Modifies `password` given to use at least one character of each `requiredCharTyp
|
|
|
107
171
|
|
|
108
172
|
```js
|
|
109
173
|
const strictPW = pw.strictify('abcdef', ['numbers', 'symbols']);
|
|
110
|
-
console.log(strictPW);
|
|
111
|
-
|
|
112
|
-
/* sample output:
|
|
113
|
-
|
|
114
|
-
strictify() » Strictifying password...
|
|
115
|
-
strictify() » Strictification complete!
|
|
116
|
-
a!c2ef
|
|
117
|
-
*/
|
|
174
|
+
console.log(strictPW); // sample output: 'a!c2ef'
|
|
118
175
|
```
|
|
119
176
|
|
|
120
177
|
**💡 Note:** If no `requiredCharTypes` array is passed, all available types will be required.
|
|
@@ -139,9 +196,7 @@ Validates the strength of a password, returning an object containing:
|
|
|
139
196
|
Example:
|
|
140
197
|
|
|
141
198
|
```js
|
|
142
|
-
const
|
|
143
|
-
pwStrength = pw.validateStrength(password);
|
|
144
|
-
|
|
199
|
+
const pwStrength = pw.validateStrength('Aa?idsE');
|
|
145
200
|
console.log(pwStrength);
|
|
146
201
|
|
|
147
202
|
/* outputs:
|
|
@@ -174,6 +229,7 @@ Any of these can be passed into the options object for each `generate*()` functi
|
|
|
174
229
|
|
|
175
230
|
Name | Type | Description | Default Value
|
|
176
231
|
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
232
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
177
233
|
`length` | Integer | Length of password(s). | `8`
|
|
178
234
|
`qty`* | Integer | Number of passwords to generate. | `1`
|
|
179
235
|
`charset` | String | Characters to include in password(s). | `''`
|
|
@@ -216,6 +272,9 @@ Boolean options:
|
|
|
216
272
|
-s, --include-symbols Allow symbols in password(s).
|
|
217
273
|
-L, --no-lowercase Disallow lowercase letters in password(s).
|
|
218
274
|
-U, --no-uppercase Disallow uppercase letters in password(s).
|
|
275
|
+
-s, --strict Require at least one character from each
|
|
276
|
+
allowed character set in password(s).
|
|
277
|
+
-q, --quiet Suppress all logging except errors.
|
|
219
278
|
|
|
220
279
|
Info commands:
|
|
221
280
|
-h, --help Display help screen.
|
|
@@ -228,7 +287,7 @@ Info commands:
|
|
|
228
287
|
|
|
229
288
|
## 🏛️ MIT License
|
|
230
289
|
|
|
231
|
-
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
290
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
232
291
|
|
|
233
292
|
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:
|
|
234
293
|
|
|
@@ -240,6 +299,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
240
299
|
|
|
241
300
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
242
301
|
|
|
243
|
-
<a href="https://github.com/adamlui/js-utils">**
|
|
302
|
+
<a href="https://github.com/adamlui/js-utils">**JavaScript utilities**</a> /
|
|
244
303
|
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
|
|
245
304
|
<a href="#-generate-pw">Back to top ↑</a>
|
package/dist/cli.min.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const e=require(__dirname.match(/src/)?"./generate-pw":"./generate-pw.min")["generatePassword"],s=require("child_process")["execSync"],n="[0m",a="[1;91m",t="[1;33m",l={},c={paramOptions:{length:/^--?length/,qty:/^--?qu?a?n?ti?t?y=.*$/,charset:/^--?chars/,excludeChars:/^--?exclude=/},flags:{includeNums:/^--?(?:n|(?:include-?)?num(?:ber)?s?=?(?:true|1)?)$/,includeSymbols:/^--?(?:s|(?:include-?)?symbols?=?(?:true|1)?)$/,excludeLowerChars:/^--?(?:L|(?:exclude|disable|no)-?lower-?(?:case)?|lower-?(?:case)?=(?:false|0))$/,excludeUpperChars:/^--?(?:U|(?:exclude|disable|no)-?upper-?(?:case)?|upper-?(?:case)?=(?:false|0))$/,strictMode:/^--?s(?:trict)?(?:-?mode)?$/,quietMode:/^--?q(?:uiet)?(?:-?mode)?$/},infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};if(process.argv.forEach(s=>{var e,r,o;s.startsWith("-")&&(e=Object.keys(c.paramOptions).find(e=>c.paramOptions[e].test(s)),o=Object.keys(c.flags).find(e=>c.flags[e].test(s)),r=Object.keys(c.infoCmds).find(e=>c.infoCmds[e].test(s)),o?l[o]=!0:e?(o=s.split("=")[1],l[e]=parseInt(o)||o):r||(console.error(`
|
|
3
|
+
${a}ERROR: Arg [${s}] not recognized.`+n),console.info(`
|
|
4
|
+
${t}Valid arguments are below.`+n),i(["paramOptions","flags","infoCmds"]),process.exit(1)))}),process.argv.some(e=>c.infoCmds.help.test(e)))i();else if(process.argv.some(e=>c.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{for(const d of["length","qty"])l[d]&&(isNaN(l[d])||l[d]<1)&&(console.error(`
|
|
5
|
+
${a}Error: [${d}] argument can only be > 0.`+n),process.exit(1));const o={length:l.length||8,qty:l.qty||1,charset:l.charset,exclude:l.excludeChars,numbers:!!l.includeNums,symbols:!!l.includeSymbols,lowercase:!l.excludeLowerChars,uppercase:!l.excludeUpperChars,strict:!!l.strictMode,verbose:!l.quietMode},p=e(o);l.quietMode||console.info("\nCopying to clipboard..."),r(Array.isArray(p)?p.join("\n"):p)}function i(e=["cmdFormat","paramOptions","flags","infoCmds"]){const s={cmdFormat:[`
|
|
6
|
+
${t}generate-pw [options|commands]`+n],paramOptions:["\nParameter 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)."],flags:["\nBoolean options:"," -n, --include-numbers Allow numbers in password(s)."," -s, --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, --strict Require at least one character from each allowed character set in password(s)."," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{s[e]?.forEach(e=>{{const o=process.stdout.columns||80,n=[],s=e.match(/\S+|\s+/g);let r="";s.forEach(e=>{var s=o-(0===n.length?0:29);r.length+e.length>s&&(n.push(0===n.length?r:r.trimStart()),r=""),r+=e}),n.push(0===n.length?r:r.trimStart()),n.forEach((e,s)=>console.info(0===s?e:" ".repeat(29)+e))}})})}function r(e){e=e.replace(/\s+$/m,"").replace(/"/g,'""'),"darwin"===process.platform?s(`printf "${e}" | pbcopy`):"linux"===process.platform?s(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&s(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}
|
package/dist/generate-pw.min.js
CHANGED
|
@@ -1,14 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const m=require("crypto")["randomInt"],r=require("child_process")["execSync"],g={lower:"abcdefghijklmnopqrstuvwxyz",upper:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers:"0123456789",symbols:"!@#$%^&*()-_=+[]{}/\\|;:'\",.<>?"};function a(o={}){var e,r={length:8,qty:1,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};o={...r,...o};for(const s of Object.keys(o))if(!Object.prototype.hasOwnProperty.call(r,s))return console.error(`generatePassword() » ERROR: \`${s}\` is an invalid option.
|
|
3
|
-
`+`generatePassword() » Valid options:
|
|
4
|
-
[ ${Object.keys(r).join(", ")} ]`);for(const n of["length","qty"])if(o[n]=parseInt(o[n],10),isNaN(o[n])||o[n]<1)return console.error(`generatePassword() » ERROR: [${n}] option can only be an integer > 0.`);for(const a of["numbers","symbols","lowercase","uppercase","strict"])if("boolean"!=typeof o[a])return console.error(`generatePassword() » ERROR: [${a}] option can only be \`true\` or \`false\`.`);if(1<o.qty){const{qty:l,...i}=o;return c(l,i)}{let r=o.charset||(o.numbers?g.numbers:"")+(o.symbols?g.symbols:"")+(o.lowercase?g.lower:"")+(o.uppercase?g.upper:""),s=(r=(r=""===r?g.lower+g.upper:r).replace(new RegExp(`[${o.exclude}]`,"g"),""),"");for(let e=0;e<o.length;e++){var t=m(0,r.length);s+=r.charAt(t)}return o.strict&&(e=["number","symbol","lower","upper"].filter(e=>o[e+"s"]||o[e+"case"]),s=p(s,e)),s}}function c(r,s={}){var e={length:8,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};if(s={...e,...s},r=parseInt(r,10),isNaN(r)||r<1)return console.error("generatePasswords() » ERROR: 1st arg [qty] can only be an integer > 0.");for(const t of Object.keys(s))if(!Object.prototype.hasOwnProperty.call(e,t))return console.error(`generatePasswords() » ERROR: \`${t}\` is an invalid option.
|
|
5
|
-
`+`generatePasswords() » Valid options:
|
|
6
|
-
[ ${Object.keys(e).join(", ")} ]`);if(s.length=parseInt(s.length),isNaN(s.length)||s.length<1)return console.error("generatePasswords() » ERROR: [length] option can only be an integer > 0.");for(const n of["numbers","symbols","lowercase","uppercase","strict"])if("boolean"!=typeof s[n])return console.error(`generatePasswords() » ERROR: [${n}] option can only be \`true\` or \`false\`.`);var o=[];for(let e=0;e<r;e++)o.push(a(s));return o}function p(r,s=["number","symbol","lower","upper"],e={}){var o={verbose:!0};if(e={...o,...e},"string"!=typeof r)return console.error("strictify() » ERROR: 1st arg <password> must be a string.");var t=["number","symbol","lower","upper"];for(const p of s)if(!t.includes(p))return console.error(`strictify() » ERROR: \`${p}\` is an invalid character type.
|
|
7
|
-
`+`strictify() » Valid character types: [ ${t.join(", ")} ]`);for(const f of Object.keys(e)){if(!Object.prototype.hasOwnProperty.call(o,f))return console.error(`strictify() » ERROR: \`${f}\` is an invalid option.
|
|
8
|
-
`+`strictify() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[f])return console.error(`strictify() » ERROR: \`${f}\` option can only be set to \`true\` or \`false\`.`)}for(const u of s)global["has"+u.charAt(0).toUpperCase()+u.slice(1)]=!1;for(let e=0;e<r.length;e++)for(const d of s)(g[d]||g[d+"s"]).includes(r.charAt(e))&&(global["has"+d.charAt(0).toUpperCase()+d.slice(1)]=!0);e.verbose&&console.info("strictify() » Strictifying password...");var n=Math.min(r.length,s.length),a=[];let l=0,i=r;for(const h of s)if(l<n&&!global["has"+h.charAt(0).toUpperCase()+h.slice(1)]){let e;for(;e=m(0,r.length),a.includes(e););a.push(e);var c=g[h]||g[h+"s"];i=i.substring(0,e)+c.charAt(m(0,c.length))+i.substring(e+1),l++}return e.verbose&&(0<l?console.info("strictify() » Strictification complete!"):console.info(`strictify() » Password already includes ${s.join(" + ")} characters!
|
|
9
|
-
`+"strictify() » No modifications made.")),i}function e(e,r={}){var s={minLength:8,minLower:1,minUpper:1,minNumber:1,minSymbol:1},o={verbose:!0};if(r={...o,...r},"string"!=typeof e)return console.error("validateStrength() » ERROR: 1st arg <password> must be a string.");for(const l of Object.keys(r)){if(!Object.prototype.hasOwnProperty.call(o,l))return console.error(`validateStrength() » ERROR: \`${l}\` is an invalid option.
|
|
10
|
-
`+`validateStrength() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof r[l])return console.error(`validateStrength() » ERROR: \`${l}\` option can only be set to \`true\` or \`false\`.`)}r.verbose&&console.info("validateStrength() » Validating password strength...");var t={lower:0,upper:0,number:0,symbol:0};for(const i of e)for(const c of Object.keys(t))(g[c]||g[c+"s"]).includes(i)&&t[c]++;var n=[];e.length<s.minLength&&n.push(`Make it at least ${s.minLength} characters long.`);for(const p of Object.keys(t))t[p]<s["min"+p.charAt(0).toUpperCase()+p.slice(1)]&&n.push("Include at least one "+p+`${["upper","lower"].includes(p)?"case letter":""}.`);let a=0;a+=e.length>=s.minLength?20:0;for(const f of Object.keys(t))a+=t[f]>=s["min"+f.charAt(0).toUpperCase()+f.slice(1)]?20:0;return r.verbose&&console.info("validateStrength() » Password strength validated!\n"+(require.main!==module?"validateStrength() » Check returned object for score/recommendations.":"")),{strengthScore:a,recommendations:n,isGood:80<=a}}if(require.main!==module)module.exports={generatePassword:a,generatePasswords:c,strictify:p,validateStrength:e};else{const n="[0m",l="[1;91m",i="[1;33m",f={},u={paramOptions:{length:/^--?length/,qty:/^--?qu?a?n?ti?t?y=.*$/,charset:/^--?chars/,excludeChars:/^--?exclude=/},flags:{includeNums:/^--?(?:n|(?:include-?)?num(?:ber)?s?=?(?:true|1)?)$/,includeSymbols:/^--?(?:s|(?:include-?)?symbols?=?(?:true|1)?)$/,excludeLowerChars:/^--?(?:L|(?:exclude|disable|no)-?lower-?(?:case)?|lower-?(?:case)?=(?:false|0))$/,excludeUpperChars:/^--?(?:U|(?:exclude|disable|no)-?upper-?(?:case)?|upper-?(?:case)?=(?:false|0))$/,strictMode:/^--?s(?:trict)?(?:-?mode)?$/},infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};if(process.argv.forEach(r=>{var e,s,o;r.startsWith("-")&&(e=Object.keys(u.paramOptions).find(e=>u.paramOptions[e].test(r)),o=Object.keys(u.flags).find(e=>u.flags[e].test(r)),s=Object.keys(u.infoCmds).find(e=>u.infoCmds[e].test(r)),o?f[o]=!0:e?(o=r.split("=")[1],f[e]=parseInt(o)||o):s||(console.error(`
|
|
11
|
-
${l}ERROR: Arg [${r}] not recognized.`+n),console.info(`
|
|
12
|
-
${i}Valid arguments are below.`+n),t(["paramOptions","flags","infoCmds"]),process.exit(1)))}),process.argv.some(e=>u.infoCmds.help.test(e)))t();else if(process.argv.some(e=>u.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{for(const h of["length","qty"])f[h]&&(isNaN(f[h])||f[h]<1)&&(console.error(`
|
|
13
|
-
${l}Error: [${h}] argument can only be > 0.`+n),process.exit(1));const o={length:f.length||8,qty:f.qty||1,charset:f.charset,exclude:f.excludeChars,numbers:!!f.includeNums,symbols:!!f.includeSymbols,lowercase:!f.excludeLowerChars,uppercase:!f.excludeUpperChars,strict:!!f.strictMode},d=a(o);s(Array.isArray(d)?d.join("\n"):d)}function t(e=["cmdFormat","paramOptions","flags","infoCmds"]){const r={cmdFormat:[`
|
|
14
|
-
${i}generate-pw [options|commands]`+n],paramOptions:["\nParameter options:"," --length=n Generate password(s) of n length."," --qty=n Generate n password(s)."," --charset=chars Only include chars in password(s)."," --charset=chars Only include chars in password(s)."," --exclude=chars Exclude chars from password(s)."],flags:["\nBoolean options:"," -n, --include-numbers Allow numbers in password(s)."," -s, --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)."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{r[e]?.forEach(e=>{{const o=process.stdout.columns||80,t=[],r=e.match(/\S+|\s+/g);let s="";r.forEach(e=>{var r=o-(0===t.length?0:29);s.length+e.length>r&&(t.push(0===t.length?s:s.trimStart()),s=""),s+=e}),t.push(0===t.length?s:s.trimStart()),t.forEach((e,r)=>console.info(0===r?e:" ".repeat(29)+e))}})})}function s(e){e=e.replace(/\s+$/m,"").replace(/"/g,'""'),"darwin"===process.platform?r(`printf "${e}" | pbcopy`):"linux"===process.platform?r(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&r(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}}
|
|
1
|
+
let w;try{w=require("crypto").randomInt}catch(e){const t=window.crypto||window.msCrypto;w=(e,r)=>{var o=t?.getRandomValues(new Uint32Array(1))[0]/4294967295||Math.random();return Math.floor(o*(r-e))+e}}const m={lower:"abcdefghijklmnopqrstuvwxyz",upper:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers:"0123456789",symbols:"!@#$%^&*()-_=+[]{}/\\|;:'\",.<>?"};function d(t={}){var e={verbose:!0,length:8,qty:1,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};t={...e,...t};const r=JSON.stringify(e),o=Object.keys(e).join(", ");var s=()=>{console.info(`generatePassword() » Valid options: [ ${o} ]`),console.info("generatePassword() » If omitted, default settings are: "+r)};for(const l in t){if("0"==l)return console.error("generatePassword() » ERROR: 1st arg [options] can only be an object of key/values."),console.info("generatePassword() » Example valid call: generatePassword({ verbose: false, numbers: true })"),void s();if(!Object.prototype.hasOwnProperty.call(e,l))return console.error(`generatePassword() » ERROR: \`${l}\` is an invalid option.`),void s();if(["length","qty"].includes(l)){if(t[l]=parseInt(t[l],10),isNaN(t[l])||t[l]<1)return console.error(`generatePassword() » ERROR: [${l}] option can only be \`true\` or \`false\`.`)}else if(["numbers","symbols","lowercase","uppercase","strict"].includes(l)&&"boolean"!=typeof t[l])return console.error(`generatePassword() » ERROR: [${l}] option can only be \`true\` or \`false\`.`)}if(1<t.qty){const{qty:c,...f}=t;return g(c,f)}{var n,a="generatePasswords"===d.caller?.name;t.verbose&&!a&&console.info("generatePassword() » Initializing character set...");let r=t.charset||(t.numbers?m.numbers:"")+(t.symbols?m.symbols:"")+(t.lowercase?m.lower:"")+(t.uppercase?m.upper:""),o=(""===r&&(r=m.lower+m.upper),t.exclude&&(t.verbose&&!a&&console.info("generatePassword() » Removing excluded characters..."),r=r.replace(new RegExp(`[${t.exclude}]`,"g"),"")),t.verbose&&!a&&console.info("generatePassword() » Generating password..."),"");for(let e=0;e<t.length;e++){var i=w(0,r.length);o+=r.charAt(i)}return t.strict&&(t.verbose&&!a&&console.info("generatePassword() » Enforcing strict mode..."),n=["number","symbol","lower","upper"].filter(e=>t[e+"s"]||t[e+"case"]),o=p(o,n)),t.verbose&&!a&&(console.info("generatePassword() » Password generated!"),"undefined"==typeof require||require.main.filename.endsWith("cli.js")||console.info("generatePassword() » Check returned string.")),o}}function g(r,o={}){var e={verbose:!0,length:8,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};if(o={...e,...o},r=parseInt(r,10),isNaN(r)||r<1)return console.error("generatePasswords() » ERROR: 1st arg <qty> can only be an integer > 0.");const t=JSON.stringify(e),s=Object.keys(e).join(", ");var n=()=>{console.info(`generatePasswords() » Valid options: [ ${s} ]`),console.info("generatePasswords() » If omitted, default settings are: "+t)};for(const i in o){if("0"==i)return console.error("generatePasswords() » ERROR: 2nd arg [options] can only be an object of key/values."),console.info("generatePasswords() » Example valid call: generatePasswords(3, { verbose: false, symbols: true })"),void n();if(!Object.prototype.hasOwnProperty.call(e,i))return console.error(`generatePasswords() » ERROR: \`${i}\` is an invalid option.`),void n()}if(o.length=parseInt(o.length),isNaN(o.length)||o.length<1)return console.error("generatePasswords() » ERROR: [length] option can only be an integer > 0.");for(const l of["numbers","symbols","lowercase","uppercase","strict"])if("boolean"!=typeof o[l])return console.error(`generatePasswords() » ERROR: [${l}] option can only be \`true\` or \`false\`.`);o.verbose&&console.info(`generatePasswords() » Generating password${1<r?"s":""}...`);var a=[];for(let e=0;e<r;e++)a.push(d(o));return o.verbose&&console.info(`generatePasswords() » Password${1<r?"s":""} generated!`),"undefined"==typeof require||require.main.filename.endsWith("cli.js")||console.info("generatePasswords() » Check returned array."),a}function p(r,o=["number","symbol","lower","upper"],e={}){var t={verbose:!0};if(e={...t,...e},"string"!=typeof r)return console.error("strictify() » ERROR: 1st arg <password> must be a string.");var s=["number","symbol","lower","upper"];for(const u of o=Array.isArray(o)?o:[o])if(!s.includes(u))return console.error(`strictify() » ERROR: 2nd arg \`${u}\` is an invalid character type.`),console.info(`strictify() » Valid character types: [ ${s.join(", ")} ]`),void console.error("strictify() » Note: If omitted, all character types will be required.");const n=JSON.stringify(t),a=Object.keys(t).join(", ");var i=()=>{console.info(`strictify() » Valid options: [ ${a} ]`),console.info("strictify() » If omitted, default settings are: "+n)};for(const y in e){if("0"==y)return console.error("strictify() » ERROR: 3nd arg [options] can only be an object of key/values."),console.info("strictify() » Example valid call: strictify('pa55word', ['symbol', 'upper'], { verbose: false })"),void i();if(!Object.prototype.hasOwnProperty.call(t,y))return console.error(`strictify() » ERROR: \`${y}\` is an invalid option.`),void i();if("boolean"!=typeof e[y])return console.error(`strictify() » ERROR: [${y}] option can only be set to \`true\` or \`false\`.`)}var l={};for(const b of o)l["has"+b.charAt(0).toUpperCase()+b.slice(1)]=!1;for(let e=0;e<r.length;e++)for(const h of o)(m[h]||m[h+"s"]).includes(r.charAt(e))&&(l["has"+h.charAt(0).toUpperCase()+h.slice(1)]=!0);e.verbose&&console.info("strictify() » Strictifying password...");var c=Math.min(r.length,o.length),f=[];let d=0,g=r;for(const v of o)if(d<c&&!l["has"+v.charAt(0).toUpperCase()+v.slice(1)]){let e;for(;e=w(0,r.length),f.includes(e););f.push(e);var p=m[v]||m[v+"s"];g=g.substring(0,e)+p.charAt(w(0,p.length))+g.substring(e+1),d++}return e.verbose&&(0<d?(console.info("strictify() » Password is now strict!"),console.info("strictify() » Check returned string.")):(console.info(`strictify() » Password already includes ${o.join(" + ")} characters!`),console.info("strictify() » No modifications made."))),g}function e(e,r={}){var o={minLength:8,minLower:1,minUpper:1,minNumber:1,minSymbol:1},t={verbose:!0};if(r={...t,...r},"string"!=typeof e)return console.error("validateStrength() » ERROR: 1st arg <password> must be a string.");const s=JSON.stringify(t),n=Object.keys(t).join(", ");var a=()=>{console.info(`validateStrength() » Valid options: [ ${n} ]`),console.info("validateStrength() » If omitted, default settings are: "+s)};for(const f in r){if("0"==f)return console.error("validateStrength() » ERROR: 2nd arg [options] can only be an object of key/values."),console.info("validateStrength() » Example valid call: validateStrength('pa55word', { verbose: false })"),void a();if(!Object.prototype.hasOwnProperty.call(t,f))return console.error(`validateStrength() » ERROR: \`${f}\` is an invalid option.`),void a();if("boolean"!=typeof r[f])return console.error(`validateStrength() » ERROR: [${f}] option can only be set to \`true\` or \`false\`.`)}r.verbose&&console.info("validateStrength() » Validating password strength...");var i={lower:0,upper:0,number:0,symbol:0};for(const d of e)for(const g of Object.keys(i))(m[g]||m[g+"s"]).includes(d)&&i[g]++;var l=[];e.length<o.minLength&&l.push(`Make it at least ${o.minLength} characters long.`);for(const p of Object.keys(i))i[p]<o["min"+p.charAt(0).toUpperCase()+p.slice(1)]&&l.push("Include at least one "+p+`${["upper","lower"].includes(p)?"case letter":""}.`);let c=0;c+=e.length>=o.minLength?20:0;for(const u of Object.keys(i))c+=i[u]>=o["min"+u.charAt(0).toUpperCase()+u.slice(1)]?20:0;return r.verbose&&(console.info("validateStrength() » Password strength validated!"),console.info("validateStrength() » Check returned object for score/recommendations.")),{strengthScore:c,recommendations:l,isGood:80<=c}}try{module.exports={generatePassword:d,generatePasswords:g,strictify:p,validateStrength:e}}catch(e){}try{window.pw={generatePassword:d,generatePasswords:g,strictify:p,validateStrength:e}}catch(e){}
|
package/docs/LICENSE.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
# 🏛️ MIT License
|
|
16
16
|
|
|
17
|
-
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
17
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
18
18
|
|
|
19
19
|
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:
|
|
20
20
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generate-pw",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Randomly generate cryptographically-secure passwords.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
7
7
|
"email": "adam@kudoai.com",
|
|
8
8
|
"url": "https://github.com/adamlui"
|
|
9
9
|
},
|
|
10
|
-
"homepage": "https://github.com/adamlui/js-utils",
|
|
10
|
+
"homepage": "https://github.com/adamlui/js-utils/tree/main/generate-pw#readme",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"main": "dist/generate-pw.min.js",
|
|
13
13
|
"files": [
|
|
@@ -16,12 +16,16 @@
|
|
|
16
16
|
"!docs/*/"
|
|
17
17
|
],
|
|
18
18
|
"bin": {
|
|
19
|
-
"generatepw": "dist/
|
|
20
|
-
"generate-pw": "dist/
|
|
19
|
+
"generatepw": "dist/cli.min.js",
|
|
20
|
+
"generate-pw": "dist/cli.min.js"
|
|
21
|
+
},
|
|
22
|
+
"directories": {
|
|
23
|
+
"lib": ".",
|
|
24
|
+
"doc": "./docs"
|
|
21
25
|
},
|
|
22
26
|
"scripts": {
|
|
23
27
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
24
|
-
"build": "minify-js
|
|
28
|
+
"build": "minify-js src dist",
|
|
25
29
|
"bump:patch": "bash utils/bump.sh patch",
|
|
26
30
|
"bump:minor": "bash utils/bump.sh minor",
|
|
27
31
|
"bump:major": "bash utils/bump.sh major",
|
|
@@ -49,6 +53,6 @@
|
|
|
49
53
|
"url": "https://github.com/sponsors/adamlui"
|
|
50
54
|
},
|
|
51
55
|
"devDependencies": {
|
|
52
|
-
"@adamlui/minify.js": "^1.4.
|
|
56
|
+
"@adamlui/minify.js": "^1.4.8"
|
|
53
57
|
}
|
|
54
58
|
}
|