@zohocorporation/vault-cli 1.1.2

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.
Files changed (102) hide show
  1. package/README.md +16 -0
  2. package/out/commands/add/add.const.js +6 -0
  3. package/out/commands/add/add.types.js +2 -0
  4. package/out/commands/add/add.util.js +35 -0
  5. package/out/commands/download/download.const.js +6 -0
  6. package/out/commands/download/download.types.js +2 -0
  7. package/out/commands/download/download.utils.js +35 -0
  8. package/out/commands/generate-password/generate-password.const.js +12 -0
  9. package/out/commands/generate-password/generate-password.types.js +2 -0
  10. package/out/commands/generate-password/generate-password.utils.js +58 -0
  11. package/out/commands/generate-template/generate-template.const.js +7 -0
  12. package/out/commands/generate-template/generate-template.types.js +2 -0
  13. package/out/commands/generate-template/generate-template.util.js +37 -0
  14. package/out/commands/get/get.const.js +8 -0
  15. package/out/commands/get/get.types.js +2 -0
  16. package/out/commands/get/get.utils.js +45 -0
  17. package/out/commands/get-default/get-default.const.js +5 -0
  18. package/out/commands/get-default/get-default.types.js +2 -0
  19. package/out/commands/get-default/get-default.utils.js +33 -0
  20. package/out/commands/list/list.const.js +7 -0
  21. package/out/commands/list/list.types.js +2 -0
  22. package/out/commands/list/list.utils.js +43 -0
  23. package/out/commands/lock/lock.action.js +13 -0
  24. package/out/commands/lock/lock.js +10 -0
  25. package/out/commands/lock/lock.utils.js +52 -0
  26. package/out/commands/login/login.action.js +22 -0
  27. package/out/commands/login/login.const.js +10 -0
  28. package/out/commands/login/login.help.js +12 -0
  29. package/out/commands/login/login.js +17 -0
  30. package/out/commands/login/login.types.js +5 -0
  31. package/out/commands/login/login.utils.js +307 -0
  32. package/out/commands/reset/reset.action.js +28 -0
  33. package/out/commands/reset/reset.help.js +8 -0
  34. package/out/commands/reset/reset.js +14 -0
  35. package/out/commands/reset/reset.utils.js +2 -0
  36. package/out/commands/search/search.action.js +30 -0
  37. package/out/commands/search/search.constants.js +14 -0
  38. package/out/commands/search/search.help.js +6 -0
  39. package/out/commands/search/search.js +26 -0
  40. package/out/commands/search/search.types.js +16 -0
  41. package/out/commands/search/search.utils.js +61 -0
  42. package/out/commands/set-default/set-default.const.js +6 -0
  43. package/out/commands/set-default/set-default.types.js +2 -0
  44. package/out/commands/set-default/set-default.utils.js +39 -0
  45. package/out/commands/unlock/unlock.action.js +23 -0
  46. package/out/commands/unlock/unlock.help.js +2 -0
  47. package/out/commands/unlock/unlock.js +12 -0
  48. package/out/commands/unlock/unlock.type.js +2 -0
  49. package/out/commands/unlock/unlock.utils.js +133 -0
  50. package/out/js/I18n/index.js +80 -0
  51. package/out/js/I18n/resources/CLI_I18N_en.js +173 -0
  52. package/out/js/I18n/resources/CLI_I18N_es.js +3 -0
  53. package/out/js/I18n/resources/CLI_I18N_fr.js +3 -0
  54. package/out/js/I18n/resources/CLI_I18N_nl.js +3 -0
  55. package/out/js/I18n/resources/CLI_I18N_pt.js +3 -0
  56. package/out/js/I18n/resources/CLI_I18N_zh.js +3 -0
  57. package/out/js/crypto/aes.js +225 -0
  58. package/out/js/crypto/index.js +139 -0
  59. package/out/js/crypto/rsa.js +1945 -0
  60. package/out/js/crypto/zohovault.js +671 -0
  61. package/out/js/logger/index.js +10 -0
  62. package/out/old/Add.js +590 -0
  63. package/out/old/Default.js +209 -0
  64. package/out/old/Download.js +69 -0
  65. package/out/old/Generate.js +171 -0
  66. package/out/old/Get.js +225 -0
  67. package/out/old/List.js +197 -0
  68. package/out/old/Login.js +287 -0
  69. package/out/old/Logout.js +21 -0
  70. package/out/old/PasswordGenerator.js +718 -0
  71. package/out/old/PasswordPolicies.js +21 -0
  72. package/out/old/PasswordTypes.js +33 -0
  73. package/out/old/Update.js +682 -0
  74. package/out/old/cmd/addcmd.js +59 -0
  75. package/out/old/cmd/downloadcmd.js +76 -0
  76. package/out/old/cmd/generate-passwordcmd.js +127 -0
  77. package/out/old/cmd/generate-templatecmd.js +59 -0
  78. package/out/old/cmd/get-defaultcmd.js +48 -0
  79. package/out/old/cmd/getcmd.js +57 -0
  80. package/out/old/cmd/listcmd.js +54 -0
  81. package/out/old/cmd/logoutcmd.js +31 -0
  82. package/out/old/cmd/set-default.js +75 -0
  83. package/out/old/cmd/updatecmd.js +44 -0
  84. package/out/old/index.js +580 -0
  85. package/out/util/configuration.util.js +103 -0
  86. package/out/util/consts.util.js +97 -0
  87. package/out/util/dbutils/db.types.js +2 -0
  88. package/out/util/dbutils/db.utils.js +164 -0
  89. package/out/util/dbutils/dbtable.utils.js +19 -0
  90. package/out/util/dev.utils.js +14 -0
  91. package/out/util/encryptionutil/encryption.uitl.js +108 -0
  92. package/out/util/index.util.js +341 -0
  93. package/out/util/keyringutil/keyring.util.js +57 -0
  94. package/out/util/loader/loader.interface.js +2 -0
  95. package/out/util/loader/transfer.loader.js +28 -0
  96. package/out/util/login.util.js +352 -0
  97. package/out/util/passwordtypes.util.js +91 -0
  98. package/out/util/print.util.js +4 -0
  99. package/out/util/search.util.js +359 -0
  100. package/out/util/types.util.js +7 -0
  101. package/out/zv.js +41 -0
  102. package/package.json +96 -0
@@ -0,0 +1,718 @@
1
+ //$Id$
2
+ import chalk from 'chalk';
3
+ //ignorei18n_start
4
+ /*
5
+ export const PasswordGenerator = {
6
+ alphaLower: null,
7
+ alphaUpper: null,
8
+ numeric: null,
9
+ spclChar: null
10
+ };
11
+
12
+ PasswordGenerator.init = function (notReqChars) {
13
+ let aLower = "abcdefghijklmnopqrstuvwxyz";
14
+ let aUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
15
+ let num = "0123456789";
16
+ let sChar = "!#$%&()*+,-.:;<=>?@[]^_{|}~";
17
+
18
+ for (var i = 0; i < notReqChars.length; i++) {
19
+ if (aLower.indexOf(notReqChars[i]) >= 0) {
20
+ aLower = aLower.replace(notReqChars[i], "");
21
+ }
22
+ if (aUpper.indexOf(notReqChars[i]) >= 0) {
23
+ aUpper = aUpper.replace(notReqChars[i], "");
24
+ }
25
+ if (num.indexOf(notReqChars[i]) >= 0) {
26
+ num = num.replace(notReqChars[i], "");
27
+ }
28
+ if (sChar.indexOf(notReqChars[i]) >= 0) {
29
+ sChar = sChar.replace(notReqChars[i], "");
30
+ }
31
+ }
32
+
33
+ PasswordGenerator.alphaLower = aLower.split("");
34
+ PasswordGenerator.alphaUpper = aUpper.split("");
35
+ PasswordGenerator.numeric = num.split("");
36
+ PasswordGenerator.spclChar = sChar.split("");
37
+ };
38
+
39
+ /**
40
+ * To get the password length. It returns a random password length between minLen and maxLen.
41
+ *
42
+ * @param minLen minimum required length of the password
43
+ * @param maxLen maximum required length of the password
44
+ * @return the calculated length which should be >= minLen and <= maxLen.
45
+ *
46
+ PasswordGenerator.getPasswordLength = function (minLen, maxLen) {
47
+ let passLen = getRandomNumber(maxLen);
48
+ while (passLen < minLen) {
49
+ passLen += maxLen - minLen;
50
+ }
51
+ return passLen;
52
+ };
53
+
54
+ /**
55
+ * To generate the password as per the rule defined in specified password rule ID (prId). The rule will be obtained from the server and then
56
+ * the password will be generated at client browser.
57
+ *
58
+ * @param prId Password Rule ID
59
+ * @return Generated password
60
+ *
61
+ PasswordGenerator.generate = function (el) {
62
+ let generatedPasswd;
63
+ let policyid = el
64
+ .parent()
65
+ .closest("form")
66
+ .find("select[name='policyid']")
67
+ .val();
68
+ if (typeof policyid === "undefined") {
69
+ $.ajax({
70
+ url: "/tools/passwdPolicies.do",
71
+ type: "POST",
72
+ data: { opParam: "GetDefaultPasswordPolicy", zvrcsr: getCSRFCookie() },
73
+ async: false,
74
+ cache: false,
75
+ success: function (data) {
76
+ var opResult = data;
77
+ if (opResult.opStatus == "success") {
78
+ var passwdRule = opResult.opMsg;
79
+ var isPasswdOK = false;
80
+ var notReqChar = passwdRule.NOTREQCHARS
81
+ ? passwdRule.NOTREQCHARS.split("")
82
+ : "";
83
+ PasswordGenerator.init(notReqChar);
84
+ //while(!isPasswdOK) {
85
+ generatedPasswd = PasswordGenerator.generatePassword(
86
+ passwdRule.MINLENGTH,
87
+ passwdRule.MAXLENGTH,
88
+ passwdRule.REQMIXEDCASE,
89
+ passwdRule.REQSPCLCHAR,
90
+ passwdRule.NUMOFSPCLCHAR,
91
+ passwdRule.BEGINWITHLETTER,
92
+ passwdRule.REQNUMERALS
93
+ );
94
+ //isPasswdOK = PasswordGenerator.isPasswdNotReqChar(notReqChar, generatedPasswd);
95
+ //}
96
+ } else {
97
+ alert(opResult.opMsg);
98
+ generatedPasswd = null;
99
+ }
100
+ }
101
+ });
102
+ } else {
103
+ var policyObj = policies[policyid];
104
+ var notReqChar = policyObj.NOTREQCHARS
105
+ ? policyObj.NOTREQCHARS.split("")
106
+ : "";
107
+ PasswordGenerator.init(notReqChar);
108
+ generatedPasswd = PasswordGenerator.generatePassword(
109
+ policyObj.MINLENGTH,
110
+ policyObj.MAXLENGTH,
111
+ policyObj.REQMIXEDCASE,
112
+ policyObj.REQSPCLCHAR,
113
+ policyObj.NUMOFSPCLCHAR,
114
+ policyObj.BEGINWITHLETTER,
115
+ policyObj.REQNUMERALS
116
+ );
117
+ }
118
+ return generatedPasswd;
119
+ };
120
+
121
+ PasswordGenerator.isPasswdNotReqChar = function (notReqChar, passwd) {
122
+ for (let i = 0; i < notReqChar.length; i++) {
123
+ if (passwd.indexOf(notReqChar[i]) >= 0) {
124
+ return false;
125
+ }
126
+ }
127
+ return true;
128
+ };
129
+
130
+ /**
131
+ * To generate the password: Same like PMP.
132
+ *
133
+ * @param minLen minimum required length of the password
134
+ * @param maxLen maximum required length of the password
135
+ * @param reqMixedCase boolean value to specify whether the password should contain mixed case characters or not
136
+ * @param reqSpclChar boolean value to specify whether the password should contain special characters or not
137
+ * @param spclCharCount if applicable, minimum number of special characters to be included in the password
138
+ * @param startWithLetter boolean value to specify whether the password should start with a letter or not
139
+ * @param reqNumerals boolean value to specify whether the password should contain numerals or not
140
+ * @return the generated passowrd
141
+ *
142
+
143
+ PasswordGenerator.generatePassword = function (
144
+ minLen,
145
+ maxLen,
146
+ reqMixedCase,
147
+ reqSpclChar,
148
+ spclCharCount,
149
+ startWithLetter,
150
+ reqNumerals
151
+ ) {
152
+ let passMinLength = 0;
153
+ if (reqMixedCase) {
154
+ passMinLength = 2;
155
+ } else if (startWithLetter) {
156
+ passMinLength = 1;
157
+ }
158
+ if (reqNumerals) {
159
+ passMinLength++;
160
+ }
161
+ if (reqSpclChar) {
162
+ passMinLength = passMinLength + spclCharCount;
163
+ }
164
+ if (minLen < passMinLength) {
165
+ minLen = passMinLength;
166
+ }
167
+ if (maxLen < passMinLength) {
168
+ minLen = maxLen;
169
+ }
170
+ const passLen = PasswordGenerator.getPasswordLength(minLen, maxLen);
171
+ let password = "";
172
+ let iCount = 0;
173
+
174
+ let types = reqSpclChar && spclCharCount > 0 ? 3 : 2;
175
+ let spclCharsAdded = 0;
176
+ let toggleCase = false;
177
+ let mixedCount = 0;
178
+ // Generate the password
179
+ for (let i = 0; i < passLen; i++) {
180
+ var arrayId = 0;
181
+ if (i === 0 && startWithLetter) {
182
+ arrayId = 0;
183
+ } else if (reqNumerals) {
184
+ reqNumerals = false;
185
+ arrayId = 1;
186
+ } else if (reqMixedCase && mixedCount === 0) {
187
+ arrayId = 0;
188
+ mixedCount++;
189
+ } else if (reqSpclChar && spclCharCount > 0) {
190
+ if (spclCharsAdded < spclCharCount) {
191
+ if (passLen - i === spclCharCount - spclCharsAdded) {
192
+ arrayId = 2;
193
+ } else {
194
+ arrayId = getArrayIdRandomly(types);
195
+ }
196
+ } else {
197
+ types = 2;
198
+ }
199
+ } else {
200
+ arrayId = getArrayIdRandomly(types);
201
+ }
202
+
203
+ switch (arrayId) {
204
+ case 0: {
205
+ const alphaLowerIdx = getRandomNumber(
206
+ PasswordGenerator.alphaLower.length
207
+ );
208
+ const alphaUpperIdx = getRandomNumber(
209
+ PasswordGenerator.alphaUpper.length
210
+ );
211
+ let character;
212
+ if (reqMixedCase) {
213
+ // This depends on the no. of chars included in the password !
214
+ if (i === 0) {
215
+ password += PasswordGenerator.alphaLower[alphaLowerIdx];
216
+ } else {
217
+ let charCase = getRandomNumber(2);
218
+ if (iCount === 0) {
219
+ charCase = 0;
220
+ } else {
221
+ if (toggleCase === false) {
222
+ charCase = 1;
223
+ toggleCase = true;
224
+ }
225
+ }
226
+ if (charCase === 0) {
227
+ character = PasswordGenerator.alphaUpper[alphaUpperIdx];
228
+ if (
229
+ password.length > 1 &&
230
+ checkCurrentAndPreviousCharacter(
231
+ character,
232
+ password.charAt(password.length - 1)
233
+ )
234
+ ) {
235
+ if (alphaUpperIdx - 3 >= 0) {
236
+ character = PasswordGenerator.alphaUpper[alphaUpperIdx - 3];
237
+ } else if (
238
+ alphaLowerIdx + 3 <
239
+ PasswordGenerator.alphaUpper.length
240
+ ) {
241
+ character = PasswordGenerator.alphaUpper[alphaUpperIdx + 3];
242
+ }
243
+ }
244
+ password += character;
245
+ iCount++;
246
+ } else {
247
+ character = PasswordGenerator.alphaLower[alphaLowerIdx];
248
+ if (
249
+ password.length > 1 &&
250
+ checkCurrentAndPreviousCharacter(
251
+ character,
252
+ password.charAt(password.length - 1)
253
+ )
254
+ ) {
255
+ if (alphaLowerIdx - 3 >= 0) {
256
+ character = PasswordGenerator.alphaLower[alphaLowerIdx - 3];
257
+ } else if (
258
+ alphaLowerIdx + 3 <
259
+ PasswordGenerator.alphaLower.length
260
+ ) {
261
+ character = PasswordGenerator.alphaLower[alphaLowerIdx + 3];
262
+ }
263
+ }
264
+ password += character;
265
+ }
266
+ }
267
+ } else {
268
+ character = PasswordGenerator.alphaLower[alphaLowerIdx];
269
+ if (
270
+ password.length > 1 &&
271
+ checkCurrentAndPreviousCharacter(
272
+ character,
273
+ password.charAt(password.length - 1)
274
+ )
275
+ ) {
276
+ if (alphaLowerIdx - 3 >= 0) {
277
+ character = PasswordGenerator.alphaLower[alphaLowerIdx - 3];
278
+ } else if (
279
+ alphaLowerIdx + 3 <
280
+ PasswordGenerator.alphaLower.length
281
+ ) {
282
+ character = PasswordGenerator.alphaLower[alphaLowerIdx + 3];
283
+ }
284
+ }
285
+ password += character;
286
+ }
287
+ break;
288
+ }
289
+ case 1: {
290
+ let numIdx = getRandomNumber(PasswordGenerator.numeric.length);
291
+ let number = PasswordGenerator.numeric[numIdx];
292
+ //this condition is checked to prevent password from having having continous or repitition number such as 66 or 123.
293
+ if (
294
+ password.length > 1 &&
295
+ checkCurrentAndPreviousCharacter(
296
+ number,
297
+ password.charAt(password.length - 1)
298
+ )
299
+ ) {
300
+ if (numIdx - 3 >= 0) {
301
+ number = PasswordGenerator.numeric[numIdx - 3];
302
+ } else if (numIdx + 3 < PasswordGenerator.numeric.length) {
303
+ number = PasswordGenerator.numeric[numIdx + 3];
304
+ }
305
+ }
306
+ password += number;
307
+ break;
308
+ }
309
+ case 2: {
310
+ var spclCharIdx = getRandomNumber(PasswordGenerator.spclChar.length);
311
+ var special = PasswordGenerator.spclChar[spclCharIdx];
312
+ if (
313
+ password.length > 1 &&
314
+ checkCurrentAndPreviousCharacter(
315
+ special,
316
+ password.charAt(password.length - 1)
317
+ )
318
+ ) {
319
+ if (spclCharIdx - 3 >= 0) {
320
+ special = PasswordGenerator.spclChar[spclCharIdx - 3];
321
+ } else if (spclCharIdx + 3 < PasswordGenerator.spclChar.length) {
322
+ special = PasswordGenerator.spclChar[spclCharIdx + 3];
323
+ }
324
+ }
325
+ password += special;
326
+ spclCharsAdded++;
327
+ break;
328
+ }
329
+ }
330
+ }
331
+ return password;
332
+ };
333
+ //check wether current character and previous character are same or consecutive eg.rr or 123
334
+ function checkCurrentAndPreviousCharacter(currentChar, previousChar) {
335
+ const specialChar = [
336
+ "!",
337
+ "@",
338
+ "#",
339
+ "$",
340
+ "%",
341
+ "^",
342
+ "&",
343
+ "*",
344
+ "(",
345
+ ")",
346
+ "-",
347
+ "="
348
+ ];
349
+ try {
350
+ let prevType;
351
+ let curType;
352
+ if (
353
+ PasswordGenerator.alphaUpper.indexOf(previousChar) > -1 ||
354
+ PasswordGenerator.alphaLower.indexOf(previousChar) > -1
355
+ ) {
356
+ prevType = "char";
357
+ }
358
+ if (
359
+ PasswordGenerator.alphaUpper.indexOf(currentChar) > -1 ||
360
+ PasswordGenerator.alphaLower.indexOf(currentChar) > -1
361
+ ) {
362
+ curType = "char";
363
+ }
364
+ if (PasswordGenerator.numeric.indexOf(previousChar) > -1) {
365
+ prevType = "int";
366
+ }
367
+ if (PasswordGenerator.numeric.indexOf(currentChar) > -1) {
368
+ curType = "int";
369
+ }
370
+ if (PasswordGenerator.spclChar.indexOf(previousChar) > -1) {
371
+ prevType = "special";
372
+ }
373
+ if (PasswordGenerator.spclChar.indexOf(currentChar) > -1) {
374
+ curType = "special";
375
+ }
376
+ if (prevType !== undefined && curType !== undefined) {
377
+ if (prevType === curType) {
378
+ switch (prevType) {
379
+ case "char":
380
+ if (
381
+ previousChar.toLowerCase() === currentChar.toLowerCase() ||
382
+ previousChar.toLowerCase().charCodeAt(0) + 1 ===
383
+ currentChar.toLowerCase().charCodeAt(0) ||
384
+ previousChar.toLowerCase().charCodeAt(0) ===
385
+ currentChar.toLowerCase().charCodeAt(0) + 1
386
+ ) {
387
+ return true;
388
+ }
389
+ break;
390
+ case "int":
391
+ if (
392
+ previousChar === currentChar ||
393
+ previousChar.charCodeAt(0) + 1 === currentChar.charCodeAt(0) ||
394
+ previousChar.charCodeAt(0) === currentChar.charCodeAt(0) + 1
395
+ ) {
396
+ return true;
397
+ }
398
+ break;
399
+ case "special":
400
+ if (
401
+ previousChar === currentChar ||
402
+ (specialChar.indexOf(previousChar) > -1 &&
403
+ specialChar.indexOf(currentChar) > -1 &&
404
+ specialChar.indexOf(previousChar) + 1 ===
405
+ specialChar.indexOf(currentChar)) ||
406
+ (specialChar.indexOf(previousChar) > -1 &&
407
+ specialChar.indexOf(currentChar) > -1 &&
408
+ specialChar.indexOf(previousChar) ===
409
+ specialChar.indexOf(currentChar) + 1)
410
+ ) {
411
+ return true;
412
+ }
413
+ break;
414
+ }
415
+ }
416
+ }
417
+ } catch (er) {}
418
+ return false;
419
+ }
420
+ /* Generic functions related to Password Policy */
421
+ /**
422
+ * To get a random number in JavaScript. It expects a +ve number as parameter and returns any random number between 0 and the limit value.
423
+ * If the generated random number equals to the limit then it returns (generated_value - 1) as it should be lesser than the limit.
424
+ *
425
+ * @param limit A +ve number specifying the limit under which the random number to be generated.
426
+ *
427
+ function getRandomNumber(limit) {
428
+ var rand = Math.floor(Math.random() * limit);
429
+ return rand < limit ? rand : rand - 1;
430
+ }
431
+
432
+ /**
433
+ * To get the random array ID. This ID will help in randomly choosing the array to use.
434
+ *
435
+ * @param type Types of arrays can be used. Expected value: 2 or 3.
436
+ * If 3, the special char array will also be included otherwise only alpha and numeric array will be included
437
+ *
438
+ function getArrayIdRandomly(type) {
439
+ // We do not need to check the validity of special char array as
440
+ // 1. if it is to be used, it will not be empty
441
+ // 2. if it is NOT to be used then the random number will generate only 0 or 1 (type will be 2)
442
+ // Thus the random array ID will be verified only for alpha and numeric arrays, i.e., only for random ID 0 or 1.
443
+ var aryId = getRandomNumber(type);
444
+ if (aryId === 0) {
445
+ if (PasswordGenerator.alphaLower.length === 0) {
446
+ // Alpha array is empty, check and get either for numeric or special char randomly
447
+ if (PasswordGenerator.numeric.length === 0) {
448
+ // Numeric array is also empty so, only special char array which must not be empty in this case, can be used
449
+ return 2; // Special char array
450
+ } else {
451
+ return 1; // Numeric array
452
+ }
453
+ }
454
+ return aryId;
455
+ } else if (aryId === 1) {
456
+ if (PasswordGenerator.numeric.length === 0) {
457
+ // Numeric array is empty, check and get either for alpha or special char randomly
458
+ if (PasswordGenerator.alphaLower.length === 0) {
459
+ // Alpha array is also empty so, only special char array which must not be empty in this case, can be used
460
+ return 2; // Special char array
461
+ } else {
462
+ return 0; // Alpha array
463
+ }
464
+ }
465
+ }
466
+ // aryId can be 2 only if special char needed and hence the array will not empty.
467
+ return aryId;
468
+ }
469
+ */
470
+ // var crypto = require('crypto')
471
+ import * as crypto from "crypto";
472
+ import { I18N } from '../js/I18n/index.js';
473
+ let i18nText = new I18N();
474
+ export class PasswordOptions {
475
+ constructor(len, specialCharacterCount, includeUpperCase, includeLowerCase, excludeCharacter, includeNumbers, startWithLetter) {
476
+ this.len = len;
477
+ this.noOfSpl = specialCharacterCount;
478
+ this.needLower = includeLowerCase;
479
+ this.needUpper = includeUpperCase;
480
+ this.needNum = includeNumbers;
481
+ this.needSpl = specialCharacterCount == 0 ? false : true;
482
+ this.firstLower = startWithLetter === undefined ? ((includeLowerCase || includeUpperCase) ? true : false) : startWithLetter;
483
+ this.excludeChars = excludeCharacter;
484
+ }
485
+ }
486
+ PasswordOptions.LENGTH = "len";
487
+ PasswordOptions.REQ_LOWER = "needLower";
488
+ PasswordOptions.REQ_UPPER = "needUpper";
489
+ PasswordOptions.REQ_NUM = "needNum";
490
+ PasswordOptions.REQ_SPL = "needSpl";
491
+ PasswordOptions.NO_OF_SPL = "noOfSpl";
492
+ PasswordOptions.START_WITH_LETTER = "firstLower";
493
+ PasswordOptions.EXCLUDE_CHARS = "excludeChars";
494
+ class _PasswordGenerator {
495
+ constructor() {
496
+ this.CHARSET_TYPE = {
497
+ LOWER: 0,
498
+ UPPER: 1,
499
+ NUM: 2,
500
+ SPL: 3
501
+ };
502
+ this.CHARSET = {
503
+ [this.CHARSET_TYPE.LOWER]: "abcdefghijklmnopqrstuvwxyz",
504
+ [this.CHARSET_TYPE.UPPER]: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
505
+ [this.CHARSET_TYPE.NUM]: "0123456789",
506
+ [this.CHARSET_TYPE.SPL]: "~!@#$%^&*()-_+=|\\{}[];:'\",.<>/?"
507
+ };
508
+ }
509
+ /**
510
+ * @param {PasswordOptions} options
511
+ * @returns {string}
512
+ */
513
+ generatePassword(options) {
514
+ let uniqueexcludeString = [...new Set(options.excludeChars)].join('');
515
+ let len = '';
516
+ len = options.needLower ? len + this.CHARSET[0] : len;
517
+ len = options.needUpper ? len + this.CHARSET[1] : len;
518
+ len = options.needNum ? len + this.CHARSET[2] : len;
519
+ function check(char) {
520
+ return !(uniqueexcludeString.includes(char));
521
+ }
522
+ if (len.split('').filter(check).length === 0) {
523
+ console.log(chalk.red.bold(i18nText.getI18nMsg("vault.generator.excludeChar")));
524
+ process.exit(1);
525
+ }
526
+ return this.correct(this.shuffleF(this.processTemplate(this.getTemplate(options), options), options.firstLower), options).join("");
527
+ }
528
+ /**
529
+ *
530
+ * @param {PasswordOptions} options
531
+ * @returns {number[]}
532
+ */
533
+ getTemplate(options) {
534
+ const excludeChars = Array.from(options.excludeChars).sort().join("");
535
+ const isAllowed = x => !excludeChars.includes(this.CHARSET[x]);
536
+ const allowNum = isAllowed(this.CHARSET_TYPE.NUM);
537
+ const allowSpl = isAllowed(this.CHARSET_TYPE.SPL);
538
+ const allowLower = isAllowed(this.CHARSET_TYPE.LOWER);
539
+ const allowUpper = isAllowed(this.CHARSET_TYPE.UPPER);
540
+ const numLen = allowNum && options.needNum ? Math.ceil(0.30 * options.len) : 0;
541
+ const splLen = allowSpl && options.needSpl ? (options.noOfSpl || Math.ceil(0.10 * options.len)) : 0;
542
+ const charLen = (options.needLower || options.needUpper) ? (options.len - numLen - splLen) : 0;
543
+ const lowLen = allowLower && options.needLower ?
544
+ (allowUpper && options.needUpper ? Math.ceil(charLen / 2) : charLen) : 0;
545
+ const upLen = (allowUpper && options.needUpper) ? charLen - lowLen : 0;
546
+ const remLen = options.len - (numLen + splLen + lowLen + upLen);
547
+ if (remLen && !numLen && (!splLen || options.noOfSpl || !allowSpl)) {
548
+ throw new Error();
549
+ }
550
+ const numRemLen = remLen && allowNum && options.needNum ?
551
+ (options.noOfSpl || !allowSpl || !options.needSpl ? remLen : Math.ceil(0.75 * remLen)) : 0;
552
+ const spqlRemLen = remLen - numRemLen;
553
+ return this.getFilledArray(lowLen, upLen, numLen + numRemLen, splLen + spqlRemLen);
554
+ }
555
+ getFilledArray(lower = 0, upper = 0, num = 0, spl = 0) {
556
+ const f = (len, x) => Array.from({ length: len }).fill(x);
557
+ return f(lower, this.CHARSET_TYPE.LOWER)
558
+ .concat(f(upper, this.CHARSET_TYPE.UPPER))
559
+ .concat(f(num, this.CHARSET_TYPE.NUM))
560
+ .concat(f(spl, this.CHARSET_TYPE.SPL));
561
+ }
562
+ /**
563
+ * @param {number[]} a
564
+ * @param {PasswordOptions} options
565
+ * @returns {string[]}
566
+ */
567
+ processTemplate(a = [], options) {
568
+ const charSet = this.getExcludedCharSet(options.excludeChars);
569
+ const getRandomChar = x => charSet[x][getRandomNumber(charSet[x].length)];
570
+ return a.map(x => getRandomChar(x));
571
+ }
572
+ /**
573
+ *
574
+ * @param {string[]} password
575
+ * @param {PasswordOptions} options
576
+ * @returns {string[]}
577
+ */
578
+ correct(password = [], options) {
579
+ const getAsciiCode = (x = "") => x.charCodeAt(0) || 0;
580
+ const isSameOrConsecutive = (ch1 = "", ch2 = "") => Math.abs(getAsciiCode(ch1) - getAsciiCode(ch2)) <= 1;
581
+ const getCharFromCode = (ch = "", inc = 0) => String.fromCharCode(ch.charCodeAt(0) + inc);
582
+ const getPreviousChar = (ch = "") => getCharFromCode(ch, -1);
583
+ const getNextChar = (ch = "") => getCharFromCode(ch, 1);
584
+ const getRandomChar = (charSet = "") => charSet[getRandomNumber(charSet.length)];
585
+ const ans = [];
586
+ const getLastAnsChar = () => ans[ans.length - 1] || "";
587
+ //@ts-ignore
588
+ const charSets = Object.values(this.getExcludedCharSet(options.excludeChars));
589
+ const getCharSetOf = (ch = "") => charSets.find(x => x.includes(ch));
590
+ const getReplacement = (ch = "") => {
591
+ const previousChar = getPreviousChar(ch);
592
+ const nextChar = getNextChar(ch);
593
+ const originalCharSet = getCharSetOf(ch);
594
+ const correctedcCharSet = this.replaceEach(originalCharSet, previousChar + ch + nextChar);
595
+ const choosenCharSet = correctedcCharSet.length ? correctedcCharSet : originalCharSet;
596
+ return getRandomChar(choosenCharSet);
597
+ };
598
+ const addAnsChar = (ch = "") => {
599
+ const previousChar = getLastAnsChar();
600
+ const correctedCh = isSameOrConsecutive(previousChar, ch) ? getReplacement(previousChar) : ch;
601
+ ans.push(correctedCh);
602
+ };
603
+ password.forEach(ch => addAnsChar(ch));
604
+ return ans;
605
+ }
606
+ getExcludedCharSet(excludeChars = "") {
607
+ //@ts-ignore
608
+ return Object.fromEntries(Object.entries(this.CHARSET)
609
+ .map(x => [x[0], this.replaceEach(x[1], excludeChars)]));
610
+ }
611
+ replaceEach(s = "", toBeRemoved = "") {
612
+ const removeSet = new Set(toBeRemoved);
613
+ const removeFilter = ch => !removeSet.has(ch);
614
+ return Array.from(s).filter(removeFilter).join("");
615
+ }
616
+ shuffleF(a = [], excludeStart = false) {
617
+ return excludeStart ? [a[0]].concat(this.shuffle(a.slice(1))) : this.shuffle(a);
618
+ }
619
+ //shuffles by Fisher-Yates shuffle algorithm https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
620
+ shuffle(a = []) {
621
+ const swap = (i, j) => { const temp = a[i]; a[i] = a[j]; a[j] = temp; };
622
+ a.forEach((x, i) => swap(i, getRandomNumberMaxExc(i, a.length)));
623
+ return a;
624
+ }
625
+ }
626
+ function getRandomNumber(limit) {
627
+ return crypto.randomFillSync(new Uint32Array(1))[0] % limit;
628
+ }
629
+ function getRandomNumberInRange(min, max) {
630
+ return crypto.randomFillSync(new Uint32Array(1))[0] % (max - min + 1) + min;
631
+ }
632
+ function getRandomNumberMaxExc(min, max) {
633
+ return crypto.randomFillSync(new Uint32Array(1))[0] % (max - min) + min;
634
+ }
635
+ export const PasswordGenerator = {
636
+ x: new _PasswordGenerator(),
637
+ /**
638
+ * @param {PasswordOptions} options
639
+ * @returns {string}
640
+ * @throws exception on invalid constraints => ex:length 100, no lower case , no upper case , no number, 5 spl char
641
+ */
642
+ generatePassword(options) {
643
+ return this.x.generatePassword(options);
644
+ },
645
+ /**
646
+ * @param {PasswordOptions} options
647
+ * @returns {boolean}
648
+ */
649
+ isOk(options) {
650
+ try {
651
+ this.generatePassword(options);
652
+ return true;
653
+ }
654
+ catch (e) {
655
+ return false;
656
+ }
657
+ }
658
+ };
659
+ export function getComplexityOf(password = "") {
660
+ const MAX_LENGTH = 12;
661
+ function getComplexity(password = "") {
662
+ const complexity = Math.min(getLengthComplexity(password), getCharSetComplexity(password));
663
+ return complexity >= 100 ? 100 : complexity;
664
+ }
665
+ function getCharSetComplexity(password) {
666
+ return getPercent(getCharSetLengthFloor(Math.pow(getCharSetBase(password), new Set(password).size)), MAX_LENGTH);
667
+ }
668
+ function getCharSetLengthFloor(complexity) {
669
+ const MAX_BASE = 20 + 20 + 15 + 10;
670
+ let i = 3;
671
+ while (Math.pow(MAX_BASE, i) <= complexity) {
672
+ i++;
673
+ }
674
+ return i - 1;
675
+ }
676
+ function getCharSetBase(password) {
677
+ const a = [[/[a-z]/, 20], [/[A-Z]/, 20], [/[0-9]/, 10], [/[-~!@#$%^&*()_+=|\\{}[\];:'",.<>/?]/, 15]];
678
+ //@ts-ignore
679
+ return a.filter(x => x[0].test(password)).reduce((prev, x) => prev + x[1], 0);
680
+ }
681
+ function getLengthComplexity(password) {
682
+ return getPercent(getFactNValFloor(combinations(Array.from(password))) - 3, MAX_LENGTH - 3);
683
+ }
684
+ function getFactNValFloor(limit) {
685
+ let i = 1;
686
+ while (factorial(i) <= limit) {
687
+ i++;
688
+ }
689
+ return i - 1;
690
+ }
691
+ function combinations(elements) {
692
+ return factorial(elements.length) /
693
+ Array.from(getCountMap(elements).values()).map(factorial).reduce((x, y) => x * y, 1);
694
+ }
695
+ function getCountMap(a) {
696
+ const countMap = new Map();
697
+ a.forEach(x => countMap.set(x, (countMap.get(x) || 0) + 1));
698
+ return countMap;
699
+ }
700
+ function factorial(n) {
701
+ let ans = 1;
702
+ for (let i = 2; i <= n; i++) {
703
+ ans *= i;
704
+ }
705
+ return ans;
706
+ }
707
+ function getPercent(part, total) {
708
+ return roundOf(((part < 0 ? 0 : part) / total) * 100, 100);
709
+ }
710
+ function roundOf(val, limit) {
711
+ return val >= limit ? limit : val;
712
+ }
713
+ function average(...a) {
714
+ return a.reduce((x, y) => x + y, 0) / a.length;
715
+ }
716
+ return getComplexity(password);
717
+ }
718
+ //# sourceMappingURL=PasswordGenerator.js.map