-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
413 lines (366 loc) · 14.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* Password Generator
* https://github.com/phillipsdata/unicode-passgen
* Copyright (c) 2018 Phillips Data, Inc.
* MIT License
*/
;(function() {
'use strict';
// Setup the root object--'window', 'global', or 'this'
var root = typeof self == 'object' && self.self === self && self ||
typeof global == 'object' && global.global === global && global ||
this ||
{};
var previousUnicodePassgen = root.unicodePassgen;
var unicodePassgen = {};
// Be sure our dependencies exist
var regenerate = root.regenerate || (typeof require !== undefined) && require('regenerate');
if (!regenerate) {
throw new Error('unicodePassgen requires regenerate, see https://github.com/mathiasbynens/regenerate');
}
/**
* Generates a string of the given length that include characters as defined
* by the given options.
*
* @param {Integer} length The length of the string to generate
* @param {Object} options An Object containing:
* - include (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to include, or the beginning of a
* range of characters to include
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to include.
* - min (optional) - An Integer representing the minimum number of
* characters from the 'chars' set that *must* be included in the generated
* string
* - exclude (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to exclude, or the beginning of a
* range of characters to exclude
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to exclude.
* @returns {String} A random generated string
*/
unicodePassgen.generate = function(length, options) {
if (!isInt(length)) {
throw new TypeError('Non-integer length type');
} else if (length < 0) {
throw new RangeError('Length must be a positive integer');
}
var opts = getOptions(options);
var allCharacters = getCharacterList(opts);
// Return no password if we have no data set to generate it from
var value = '';
if (allCharacters.length <= 0) {
return value;
}
// Generate random characters for the required minimum number of values
var characterSets = getCharacterSets(opts);
for (var i in characterSets) {
value += generateString(characterSets[i].set, characterSets[i].min);
}
// Generate random characters for the remainder of the length
value += generateString(allCharacters, (length - value.length));
return shuffle(value);
};
/**
* Creates a set of options for the generator from the given options
*
* @param {Object} options An Object containing:
* - include (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to include, or the beginning of a
* range of characters to include
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to include.
* - min (optional) - An Integer representing the minimum number of
* characters from the 'chars' set that *must* be included in the generated
* string
* - exclude (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to exclude, or the beginning of a
* range of characters to exclude
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to exclude.
* @returns {Object} An Object representing the given options
*/
function getOptions(options) {
var opts = {
include: [
{
chars: [[0x0000, 0xFFFF]],
min: 0
}
],
exclude: []
};
// Merge the properties of our options
for (var property in options) {
// Ignore options that are not in our opts set or are not an array
if (!opts.hasOwnProperty(property) || !Array.isArray(options[property])) {
continue;
}
// Reset the default value for this property since it's being overridden
if (property === 'include') {
opts.include = [];
}
// Merge the options given
for (var set in options[property]) {
if (typeof options[property][set] === 'object' &&
options[property][set].hasOwnProperty('chars') &&
Array.isArray(options[property][set].chars)
) {
// Add the character set to the options
var charSet = {
chars: translate(options[property][set].chars),
min: 0
};
// Set the minimum value if given
if (options[property][set].hasOwnProperty('min') &&
isInt(options[property][set].min) &&
options[property][set].min > 0
) {
charSet.min = options[property][set].min;
}
opts[property].push(charSet);
}
}
}
return opts;
}
/**
* Translates a pre-defined set of words into their character groups, if set
*
* @param {Array} characters An Array of character sets
* @returns {Array} An array of character sets with predefined sets translated
*/
function translate(characters)
{
var newCharacters = [];
var alpha_lower = ['a', 'z'];
var alpha_upper = ['A', 'Z'];
var numeric = ['0', '9'];
var sets = {
alpha: [alpha_lower, alpha_upper],
alpha_lower: [alpha_lower],
alpha_upper: [alpha_upper],
numeric: [numeric],
alpha_numeric: [alpha_lower, alpha_upper, numeric],
alpha_numeric_lower: [alpha_lower, numeric],
alpha_numeric_upper: [alpha_upper, numeric],
// ASCII printable symbols
symbols: [[33, 47], [58, 64], [91, 96], [123, 126]]
};
for (var set in characters) {
// Only a single set element should be given, e.g. ['alpha']
// otherwise the entire set should not be translated
if (characters[set].length !== 1 ||
characters[set][0] === undefined ||
!sets.hasOwnProperty(characters[set][0])
) {
newCharacters.push(characters[set]);
continue;
}
sets[characters[set][0]].map(function(val) {
return this.chars.push(val);
}, {chars: newCharacters});
}
return newCharacters;
}
/**
* Generates a string of the given length from the given set of characters
*
* @param {Array} set An Array of decimal characters
* @param {Integer} length The length of the string to generate
* @returns {String} The random generated string
*/
function generateString(set, length) {
var content = '';
// Cannot generate a string from an empty set
if (set.length <= 0) {
return content;
}
for (var i = 0; i < length; i++) {
content += String.fromCharCode(
set[Math.floor(Math.random() * set.length)]
);
}
return content;
}
/**
* Retrieves a complete list of all characters from the given options
*
* @param {Object} options An Object containing:
* - include (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to include, or the beginning of a
* range of characters to include
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to include.
* - min (optional) - An Integer representing the minimum number of
* characters from the 'chars' set that *must* be included in the generated
* string
* - exclude (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to exclude, or the beginning of a
* range of characters to exclude
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to exclude.
* @returns {Array} an array of characters
*/
function getCharacterList(options) {
var regen = regenerate();
var include = options.include;
for (var i in include) {
for (var j in include[i].chars) {
// There should always be a 0th-index element
if (include[i].chars[j][0] === undefined) {
continue;
}
regen = addCharacters(regen, include[i].chars[j]);
}
}
removeCharacters(regen, options.exclude);
return regen.valueOf();
}
/**
* Retrieves a complete list of all character sets from the given options
*
* @param {Object} options An Object containing:
* - include (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to include, or the beginning of a
* range of characters to include
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to include.
* - min (optional) - An Integer representing the minimum number of
* characters from the 'chars' set that *must* be included in the generated
* string
* - exclude (optional) - An Object containing an Array of Objects, each of which contain:
* - chars - An Array containing an Array of 1 character per index:
* - 0 - A single character, hex character, or unicode character.
* This may be a single character to exclude, or the beginning of a
* range of characters to exclude
* - 1 - A single character, hex character, or unicode character.
* This must be the end of the range of the characters to exclude.
* @returns {Array} An Array of Objects, each of which include:
* - set - An Array of decimal characters
* - min - An Integer representing the minimum number of characters from the
* set that must be included in the generated string
*/
function getCharacterSets(options) {
var regen;
var include = options.include;
var sets = [];
for (var i in include) {
regen = regenerate();
for (var j in include[i].chars) {
// There should always be a 0th-index element
if (include[i].chars[j][0] === undefined) {
continue;
}
// Add the characters from the set
regen = addCharacters(regen, include[i].chars[j]);
}
// Remove the characters from the exclusion set
regen = removeCharacters(regen, options.exclude);
sets.push(
{
set: regen.valueOf(),
min: include[i].min
}
);
}
return sets;
}
/**
* Adds the given character/range to the set
*
* @param {Regenerate} regen An instance of the Regenerate object
* @param {Array} set An Array containing a 0th and (optionally) a 1st index
* representing a character or character range
* @returns {Regenerate} An updated instance of the Regenerate object
*/
function addCharacters(regen, set) {
// Add the character range, or a single character itself, to the set
if (set[1] !== undefined) {
regen.addRange(set[0], set[1]);
} else {
regen.add(set[0]);
}
return regen;
}
/**
* Removes the given character/range from the set
*
* @param {Regenerate} regen An instance of the Regenerate object
* @param {Array} set An Array containing a 0th and (optionally) a 1st index
* representing a character or character range
* @returns {Regenerate} An updated instance of the Regenerate object
*/
function removeCharacters(regen, set) {
// Remove characters provided
for (var i in set) {
for (var j in set[i].chars) {
// There should always be a 0th-index element
if (set[i].chars[j][0] === undefined) {
continue;
}
// Remove the character range, or a single character itself, from the set
if (set[i].chars[j][1] !== undefined) {
regen.removeRange(set[i].chars[j][0], set[i].chars[j][1]);
} else {
regen.remove(set[i].chars[j][0]);
}
}
}
return regen;
}
/**
* Everyone do the Fisher-Yates shuffle now -_(*-*)_-
*
* @param {Array|String} data An array of characters or a character string
* whose index values to shuffle
* @returns {String} The shuffled data
*/
function shuffle(data) {
// Convert the string to an array
var content = (typeof data === 'string' ? data.split('') : data);
// Perform the shuffle on the array
for (var length = content.length - 1; length > 0; length--) {
var index = Math.floor(Math.random() * (length + 1));
var temp = content[length];
content[length] = content[index];
content[index] = temp;
}
return content.join('');
}
/**
* Checks whether the given number is an Integer
*
* @param {Mixed} number The value to check
* @returns {Boolean} Whether the number is an Integer
*/
function isInt(number) {
if (isNaN(number)) {
return false;
}
return (parseFloat(number) | 0) === number;
}
// Setup module for node/browser
if (typeof exports !== undefined) {
if (typeof module !== undefined && module.exports) {
module.exports = unicodePassgen;
}
exports.unicodePassgen = unicodePassgen;
} else {
root.unicodePassgen = unicodePassgen;
}
}).call(this);