generate-pw 1.3.1 → 1.4.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/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.3.1"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.3.1-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
20
+ <a href="https://github.com/adamlui/js-utils/releases/tag/generate-pw-1.4.1"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.1-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,23 +43,60 @@ $ 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 usage
46
+ ## 🔌 Importing the API
47
47
 
48
- **generate-pw** can be imported into your app as an ECMAScript module or a CommonJS module.
48
+ ### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
49
49
 
50
- #### ESM:
50
+ #### ES module*:
51
51
 
52
52
  ```js
53
53
  import * as pw from 'generate-pw';
54
54
  ```
55
55
 
56
- #### CJS:
56
+ #### CommonJS:
57
57
 
58
58
  ```js
59
59
  const pw = require('generate-pw');
60
60
  ```
61
61
 
62
- #
62
+ ###### _*Node.js version 14 or higher required_
63
+
64
+ ### <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
65
+
66
+ #### <> HTML script tag:
67
+
68
+ ```html
69
+ <script src="https://cdn.jsdelivr.net/npm/generate-pw@1.4.1/dist/generate-pw.min.js"></script>
70
+ ```
71
+
72
+ #### ES6:
73
+
74
+ ```js
75
+ (async () => {
76
+ await import('https://cdn.jsdelivr.net/npm/generate-pw@1.4.1/dist/generate-pw.min.js');
77
+ // Your code here...
78
+ })();
79
+ ```
80
+
81
+ ### <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
82
+
83
+ ```js
84
+ ...
85
+ // @require https://cdn.jsdelivr.net/npm/generate-pw@1.4.1/dist/generate-pw.min.js
86
+ // ==/UserScript==
87
+
88
+ // Your code here...
89
+ ```
90
+
91
+ <br>
92
+
93
+ **💡 Note:** To always import the latest version (not recommended in production!) remove the `@1.4.1` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-pw/dist/generate-pw.min.js`
94
+
95
+ <br>
96
+
97
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
98
+
99
+ ## 📋 API usage
63
100
 
64
101
  ### `generatePassword([options])`
65
102
 
@@ -67,16 +104,7 @@ Generates **one** password if `qty` option is not given, returning a string:
67
104
 
68
105
  ```js
69
106
  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
- */
107
+ console.log(password); // sample output: 'bAsZm3mq6Qn'
80
108
  ```
81
109
 
82
110
  ...or **multiple** passwords if `qty` option is given, returning an array of strings:
@@ -129,15 +157,7 @@ Modifies `password` given to use at least one character of each `requiredCharTyp
129
157
 
130
158
  ```js
131
159
  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
- */
160
+ console.log(strictPW); // sample output: 'a!c2ef'
141
161
  ```
142
162
 
143
163
  **💡 Note:** If no `requiredCharTypes` array is passed, all available types will be required.
@@ -195,7 +215,7 @@ Any of these can be passed into the options object for each `generate*()` functi
195
215
 
196
216
  Name | Type | Description | Default Value
197
217
  ------------|---------|--------------------------------------------------------------------------------|---------------
198
- `verbose` | Boolean | Enable logging. | `true`
218
+ `verbose` | Boolean | Show logging in console/terminal. | `true`
199
219
  `length` | Integer | Length of password(s). | `8`
200
220
  `qty`* | Integer | Number of passwords to generate. | `1`
201
221
  `charset` | String | Characters to include in password(s). | `''`
@@ -238,6 +258,9 @@ Boolean options:
238
258
  -s, --include-symbols Allow symbols in password(s).
239
259
  -L, --no-lowercase Disallow lowercase letters in password(s).
240
260
  -U, --no-uppercase Disallow uppercase letters in password(s).
261
+ -s, --strict Require at least one character from each
262
+ allowed character set in password(s).
263
+ -q, --quiet Suppress all logging except errors.
241
264
 
242
265
  Info commands:
243
266
  -h, --help Display help screen.
@@ -250,7 +273,7 @@ Info commands:
250
273
 
251
274
  ## 🏛️ MIT License
252
275
 
253
- **Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
276
+ **Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
254
277
 
255
278
  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
279
 
@@ -262,6 +285,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
262
285
 
263
286
  <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
264
287
 
265
- <a href="https://github.com/adamlui/js-utils">**Home**</a> /
288
+ <a href="https://github.com/adamlui/js-utils">**JavaScript utilities**</a> /
266
289
  <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
267
290
  <a href="#-generate-pw">Back to top ↑</a>
@@ -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="",a="",t="",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"})}
@@ -1,14 +1 @@
1
- #!/usr/bin/env node
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="",a="",i="",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 h;try{h=require("crypto").randomInt}catch(e){const t=window.crypto||window.msCrypto;h=(e,r)=>{var o=t?.getRandomValues(new Uint32Array(1))[0]/4294967295||Math.random();return Math.floor(o*(r-e))+e}}const w={lower:"abcdefghijklmnopqrstuvwxyz",upper:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers:"0123456789",symbols:"!@#$%^&*()-_=+[]{}/\\|;:'\",.<>?"};function g(t={}){const r={verbose:!0,length:8,qty:1,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1},e=JSON.stringify(r,null,2).replace(/"([^"]+)":/g,"$1:").replace(/"/g,"'").replace(/\n\s*/g," "),o=Object.keys(r).join(", "),s=Object.keys(r).filter(e=>"boolean"==typeof r[e]),n=Object.keys(r).filter(e=>Number.isInteger(r[e]));var a=()=>{console.info(`generatePassword() » Valid options: [ ${o} ]`),console.info("generatePassword() » If omitted, default settings are: "+e)};if("object"==typeof t){for(const f in t){if(!Object.prototype.hasOwnProperty.call(r,f))return console.error(`generatePassword() » ERROR: \`${f}\` is an invalid option.`),void a();if(s.includes(f)&&"boolean"!=typeof t[f])return console.error(`generatePassword() » ERROR: [${f}] option can only be \`true\` or \`false\`.`);if(n.includes(f)&&(t[f]=parseInt(t[f],10),isNaN(t[f])||t[f]<1))return console.error(`generatePassword() » ERROR: [${f}] option can only be \`true\` or \`false\`.`)}if(1<(t={...r,...t}).qty){const{qty:d,...p}=t;return u(d,p)}{var i,l="generatePasswords"===g.caller?.name;t.verbose&&!l&&console.info("generatePassword() » Initializing character set...");let r=t.charset||(t.numbers?w.numbers:"")+(t.symbols?w.symbols:"")+(t.lowercase?w.lower:"")+(t.uppercase?w.upper:""),o=(""===r&&(r=w.lower+w.upper),t.exclude&&(t.verbose&&!l&&console.info("generatePassword() » Removing excluded characters..."),r=r.replace(new RegExp(`[${t.exclude}]`,"g"),"")),t.verbose&&!l&&console.info("generatePassword() » Generating password..."),"");for(let e=0;e<t.length;e++){var c=h(0,r.length);o+=r.charAt(c)}return t.strict&&(t.verbose&&!l&&console.info("generatePassword() » Enforcing strict mode..."),i=["number","symbol","lower","upper"].filter(e=>t[e+"s"]||t[e+"case"]),o=y(o,i)),t.verbose&&!l&&(console.info("generatePassword() » Password generated!"),"undefined"==typeof require||require.main.filename.endsWith("cli.js")||console.info("generatePassword() » Check returned string.")),o}}console.error("generatePassword() » ERROR: [options] can only be an object of key/values."),console.info("generatePassword() » Example valid call: generatePassword({ verbose: false, numbers: true })"),a()}function u(r,o={}){const t={verbose:!0,length:8,charset:"",exclude:"",numbers:!1,symbols:!1,lowercase:!0,uppercase:!0,strict:!1};if(r=parseInt(r,10),isNaN(r)||r<1)return console.error("generatePasswords() » ERROR: 1st arg <qty> can only be an integer > 0.");const e=JSON.stringify(t,null,2).replace(/"([^"]+)":/g,"$1:").replace(/"/g,"'").replace(/\n\s*/g," "),s=Object.keys(t).join(", "),n=Object.keys(t).filter(e=>"boolean"==typeof t[e]),a=Object.keys(t).filter(e=>Number.isInteger(t[e]));var i=()=>{console.info(`generatePasswords() » Valid options: [ ${s} ]`),console.info("generatePasswords() » If omitted, default settings are: "+e)};if("object"==typeof o){for(const c in o){if(!Object.prototype.hasOwnProperty.call(t,c))return console.error(`generatePasswords() » ERROR: \`${c}\` is an invalid option.`),void i();if(n.includes(c)&&"boolean"!=typeof o[c])return console.error(`generatePassword() » ERROR: [${c}] option can only be \`true\` or \`false\`.`);if(a.includes(c)&&(o[c]=parseInt(o[c],10),isNaN(o[c])||o[c]<1))return console.error(`generatePassword() » ERROR: [${c}] option can only be an integer > 0.`)}(o={...t,...o}).verbose&&console.info(`generatePasswords() » Generating password${1<r?"s":""}...`);var l=[];for(let e=0;e<r;e++)l.push(g(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."),l}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 })"),i()}function y(t,s=["number","symbol","lower","upper"],e={}){var n={verbose:!0};if("string"!=typeof t)return console.error("strictify() » ERROR: 1st arg <password> must be a string.");var r=["number","symbol","lower","upper"];for(const p of s=Array.isArray(s)?s:[s])if(!r.includes(p))return console.error(`strictify() » ERROR: 2nd arg \`${p}\` is an invalid character type.`),console.info(`strictify() » Valid character types: [ ${r.map(e=>`'${e}'`).join(", ")} ]`),void console.info("strictify() » Pass one as a string or more as an array, or all types will be required.");const o=JSON.stringify(n,null,2).replace(/"([^"]+)":/g,"$1:").replace(/"/g,"'").replace(/\n\s*/g," "),a=Object.keys(n).join(", ");var i=()=>{console.info(`strictify() » Valid options: [ ${a} ]`),console.info("strictify() » If omitted, default settings are: "+o)};if("object"==typeof e){for(const g in e){if(!Object.prototype.hasOwnProperty.call(n,g))return console.error(`strictify() » ERROR: \`${g}\` is an invalid option.`),void i();if("boolean"!=typeof e[g])return console.error(`strictify() » ERROR: [${g}] option can only be set to \`true\` or \`false\`.`)}e={...n,...e};var l={};for(const u of s)l["has"+u.charAt(0).toUpperCase()+u.slice(1)]=!1;for(let e=0;e<t.length;e++)for(const y of s)(w[y]||w[y+"s"]).includes(t.charAt(e))&&(l["has"+y.charAt(0).toUpperCase()+y.slice(1)]=!0);e.verbose&&console.info("strictify() » Strictifying password...");var c=Math.min(t.length,s.length),f=[];let r=0,o=t;for(const b of s)if(r<c&&!l["has"+b.charAt(0).toUpperCase()+b.slice(1)]){let e;for(;e=h(0,t.length),f.includes(e););f.push(e);var d=w[b]||w[b+"s"];o=o.substring(0,e)+d.charAt(h(0,d.length))+o.substring(e+1),r++}return e.verbose&&(0<r?(console.info("strictify() » Password is now strict!"),console.info("strictify() » Check returned string.")):(console.info(`strictify() » Password already includes ${s.join(" + ")} characters!`),console.info("strictify() » No modifications made."))),o}console.error("strictify() » ERROR: 3rd arg [options] can only be an object of key/values."),console.info("strictify() » Example valid call: strictify('pa55word', ['symbol', 'upper'], { verbose: false })"),i()}function e(r,o={}){var t={minLength:8,minLower:1,minUpper:1,minNumber:1,minSymbol:1},s={verbose:!0};if("string"!=typeof r)return console.error("validateStrength() » ERROR: 1st arg <password> must be a string.");const e=JSON.stringify(s,null,2).replace(/"([^"]+)":/g,"$1:").replace(/"/g,"'").replace(/\n\s*/g," "),n=Object.keys(s).join(", ");var a=()=>{console.info(`validateStrength() » Valid options: [ ${n} ]`),console.info("validateStrength() » If omitted, default settings are: "+e)};if("object"==typeof o){for(const c in o){if(!Object.prototype.hasOwnProperty.call(s,c))return console.error(`validateStrength() » ERROR: \`${c}\` is an invalid option.`),void a();if("boolean"!=typeof o[c])return console.error(`validateStrength() » ERROR: [${c}] option can only be set to \`true\` or \`false\`.`)}(o={...s,...o}).verbose&&console.info("validateStrength() » Validating password strength...");var i={lower:0,upper:0,number:0,symbol:0};for(const f of r)for(const d of Object.keys(i))(w[d]||w[d+"s"]).includes(f)&&i[d]++;var l=[];r.length<t.minLength&&l.push(`Make it at least ${t.minLength} characters long.`);for(const p of Object.keys(i))i[p]<t["min"+p.charAt(0).toUpperCase()+p.slice(1)]&&l.push("Include at least one "+p+`${["upper","lower"].includes(p)?"case letter":""}.`);let e=0;e+=r.length>=t.minLength?20:0;for(const g of Object.keys(i))e+=i[g]>=t["min"+g.charAt(0).toUpperCase()+g.slice(1)]?20:0;return o.verbose&&(console.info("validateStrength() » Password strength validated!"),console.info("validateStrength() » Check returned object for score/recommendations.")),{strengthScore:e,recommendations:l,isGood:80<=e}}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 })"),a()}const r={generatePassword:g,generatePasswords:u,strictify:y,validateStrength:e};try{module.exports={...r}}catch(e){}try{window.pw={...r}}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.1",
3
+ "version": "1.4.1",
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/generate-pw.min.js",
20
- "generate-pw": "dist/generate-pw.min.js"
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 generate-pw.js dist",
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.7"
56
+ "@adamlui/minify.js": "^1.4.8"
53
57
  }
54
58
  }