generate-pw 1.3.1 → 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 +64 -27
- 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,16 +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
|
-
|
|
72
|
-
/* sample output:
|
|
73
|
-
|
|
74
|
-
generatePassword() » Initializing character set...
|
|
75
|
-
generatePassword() » Generating password...
|
|
76
|
-
generatePassword() » Password generated!
|
|
77
|
-
generatePassword() » Check returned string.
|
|
78
|
-
'bAsZm3mq6Qn'
|
|
79
|
-
*/
|
|
121
|
+
console.log(password); // sample output: 'bAsZm3mq6Qn'
|
|
80
122
|
```
|
|
81
123
|
|
|
82
124
|
...or **multiple** passwords if `qty` option is given, returning an array of strings:
|
|
@@ -129,15 +171,7 @@ Modifies `password` given to use at least one character of each `requiredCharTyp
|
|
|
129
171
|
|
|
130
172
|
```js
|
|
131
173
|
const strictPW = pw.strictify('abcdef', ['numbers', 'symbols']);
|
|
132
|
-
console.log(strictPW);
|
|
133
|
-
|
|
134
|
-
/* sample output:
|
|
135
|
-
|
|
136
|
-
strictify() » Strictifying password...
|
|
137
|
-
strictify() » Password is now strict!
|
|
138
|
-
strictify() » Check returned string.
|
|
139
|
-
a!c2ef
|
|
140
|
-
*/
|
|
174
|
+
console.log(strictPW); // sample output: 'a!c2ef'
|
|
141
175
|
```
|
|
142
176
|
|
|
143
177
|
**💡 Note:** If no `requiredCharTypes` array is passed, all available types will be required.
|
|
@@ -195,7 +229,7 @@ Any of these can be passed into the options object for each `generate*()` functi
|
|
|
195
229
|
|
|
196
230
|
Name | Type | Description | Default Value
|
|
197
231
|
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
198
|
-
`verbose` | Boolean |
|
|
232
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
199
233
|
`length` | Integer | Length of password(s). | `8`
|
|
200
234
|
`qty`* | Integer | Number of passwords to generate. | `1`
|
|
201
235
|
`charset` | String | Characters to include in password(s). | `''`
|
|
@@ -238,6 +272,9 @@ Boolean options:
|
|
|
238
272
|
-s, --include-symbols Allow symbols in password(s).
|
|
239
273
|
-L, --no-lowercase Disallow lowercase letters in password(s).
|
|
240
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.
|
|
241
278
|
|
|
242
279
|
Info commands:
|
|
243
280
|
-h, --help Display help screen.
|
|
@@ -250,7 +287,7 @@ Info commands:
|
|
|
250
287
|
|
|
251
288
|
## 🏛️ MIT License
|
|
252
289
|
|
|
253
|
-
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
290
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
254
291
|
|
|
255
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:
|
|
256
293
|
|
|
@@ -262,6 +299,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
262
299
|
|
|
263
300
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
264
301
|
|
|
265
|
-
<a href="https://github.com/adamlui/js-utils">**
|
|
302
|
+
<a href="https://github.com/adamlui/js-utils">**JavaScript utilities**</a> /
|
|
266
303
|
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
|
|
267
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 g=require("crypto")["randomInt"],r=require("child_process")["execSync"],m={lower:"abcdefghijklmnopqrstuvwxyz",upper:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers:"0123456789",symbols:"!@#$%^&*()-_=+[]{}/\\|;:'\",.<>?"};function p(o={}){var e={verbose:!0,length:8,qty:1,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};o={...e,...o};for(const r of Object.keys(o))if(!Object.prototype.hasOwnProperty.call(e,r))return console.error(`generatePassword() » ERROR: \`${r}\` is an invalid option.
|
|
3
|
-
`+`generatePassword() » Valid options:
|
|
4
|
-
[ ${Object.keys(e).join(", ")} ]`);for(const s of["length","qty"])if(o[s]=parseInt(o[s],10),isNaN(o[s])||o[s]<1)return console.error(`generatePassword() » ERROR: [${s}] option can only be an integer > 0.`);for(const i of["numbers","symbols","lowercase","uppercase","strict"])if("boolean"!=typeof o[i])return console.error(`generatePassword() » ERROR: [${i}] option can only be \`true\` or \`false\`.`);if(1<o.qty){const{qty:l,...c}=o;return d(l,c)}{var t,n="generatePasswords"===p.caller.name;o.verbose&&!n&&console.info("generatePassword() » Initializing character set...");let r=o.charset||(o.numbers?m.numbers:"")+(o.symbols?m.symbols:"")+(o.lowercase?m.lower:"")+(o.uppercase?m.upper:""),s=(""===r&&(r=m.lower+m.upper),o.exclude&&(o.verbose&&!n&&console.info("generatePassword() » Removing excluded characters..."),r=r.replace(new RegExp(`[${o.exclude}]`,"g"),"")),o.verbose&&!n&&console.info("generatePassword() » Generating password..."),"");for(let e=0;e<o.length;e++){var a=g(0,r.length);s+=r.charAt(a)}return o.strict&&(o.verbose&&!n&&console.info("generatePassword() » Enforcing strict mode..."),t=["number","symbol","lower","upper"].filter(e=>o[e+"s"]||o[e+"case"]),s=f(s,t)),o.verbose&&!n&&console.info("generatePassword() » Password generated!"+(require.main!==module?"\ngeneratePassword() » Check returned string.":"")),s}}function d(r,s={}){var e={verbose:!0,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\`.`);s.verbose&&console.info(`generatePasswords() » Generating password${1<r?"s":""}...`);var o=[];for(let e=0;e<r;e++)o.push(p(s));return s.verbose&&console.info(`generatePasswords() » Password${1<r?"s":""} generated!`+(require.main!==module?"\ngeneratePasswords() » Check returned array.":"")),o}function f(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 d of Object.keys(e)){if(!Object.prototype.hasOwnProperty.call(o,d))return console.error(`strictify() » ERROR: \`${d}\` is an invalid option.
|
|
8
|
-
`+`strictify() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[d])return console.error(`strictify() » ERROR: [${d}] option can only be set to \`true\` or \`false\`.`)}for(const f of s)global["has"+f.charAt(0).toUpperCase()+f.slice(1)]=!1;for(let e=0;e<r.length;e++)for(const u of s)(m[u]||m[u+"s"]).includes(r.charAt(e))&&(global["has"+u.charAt(0).toUpperCase()+u.slice(1)]=!0);e.verbose&&console.info("strictify() » Strictifying password...");var n=Math.min(r.length,s.length),a=[];let i=0,l=r;for(const h of s)if(i<n&&!global["has"+h.charAt(0).toUpperCase()+h.slice(1)]){let e;for(;e=g(0,r.length),a.includes(e););a.push(e);var c=m[h]||m[h+"s"];l=l.substring(0,e)+c.charAt(g(0,c.length))+l.substring(e+1),i++}return e.verbose&&(0<i?console.info("strictify() » Password is now strict!\nstrictify() » Check returned string."):console.info(`strictify() » Password already includes ${s.join(" + ")} characters!
|
|
9
|
-
`+"strictify() » No modifications made.")),l}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 i of Object.keys(r)){if(!Object.prototype.hasOwnProperty.call(o,i))return console.error(`validateStrength() » ERROR: \`${i}\` is an invalid option.
|
|
10
|
-
`+`validateStrength() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof r[i])return console.error(`validateStrength() » ERROR: [${i}] 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 l of e)for(const c of Object.keys(t))(m[c]||m[c+"s"]).includes(l)&&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 d of Object.keys(t))a+=t[d]>=s["min"+d.charAt(0).toUpperCase()+d.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:p,generatePasswords:d,strictify:f,validateStrength:e};else{const n="[0m",a="[1;91m",i="[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)?$/},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(c.paramOptions).find(e=>c.paramOptions[e].test(r)),o=Object.keys(c.flags).find(e=>c.flags[e].test(r)),s=Object.keys(c.infoCmds).find(e=>c.infoCmds[e].test(r)),o?l[o]=!0:e?(o=r.split("=")[1],l[e]=parseInt(o)||o):s||(console.error(`
|
|
11
|
-
${a}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=>c.infoCmds.help.test(e)))t();else if(process.argv.some(e=>c.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{for(const h of["length","qty"])l[h]&&(isNaN(l[h])||l[h]<1)&&(console.error(`
|
|
13
|
-
${a}Error: [${h}] 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},u=p(o);s(Array.isArray(u)?u.join("\n"):u)}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
|
}
|