-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (56 loc) · 2.03 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
'use strict';
/**
* @author Thiago Brezinski
*
* light-random <https://github.com/thiagobrez/light-random>
*/
const
SORT_FACTOR = 0.5,
DEFAULT_LENGTH = 8,
ASCII_DIGIT_START = 48,
ASCII_DIGIT_END = 57,
ASCII_UPPERCASE_START = 65,
ASCII_UPPERCASE_END = 90,
FAKE_ASCII_LOWERCASE_START = 1,
FAKE_ASCII_LOWERCASE_END = 26,
ASCII_LOWERCASE_START = 97;
/**
* Generates a cryptographically weak alphanumerical random string based on the desired length.
*
* @param {number} length: generated string length
* @returns {String}: random string
*/
function lightRandom(length = DEFAULT_LENGTH) {
return getChars(length, []);
}
/**
* Generates a random number based on user current time and iterates over pairs checking if the combination
* matches digits, uppercase or lowercase letters in the ASCII table. When matched, pushes the char to a return
* array, checking if the array length is the desired user-parametrized length or the method must be called again
* recursively to reach the desired length.
*
* @param {number} length: desired length
* @param {string[]} chars: array of matched chars
* @returns {String}: random string
*/
function getChars(length, chars) {
let shuffledRandom = String(new Date().getTime() * (Math.floor(Math.random() * 10) || 1))
.split('')
.sort(() => SORT_FACTOR - Math.random())
.join('');
for (let i = 0; i < shuffledRandom.length - 1; i++) {
if (chars.length === length) break;
const charCode = Number(shuffledRandom[i] + shuffledRandom[i + 1]);
if ((charCode >= ASCII_UPPERCASE_START && charCode <= ASCII_UPPERCASE_END) ||
(charCode >= ASCII_DIGIT_START && charCode <= ASCII_DIGIT_END)) {
chars.push(String.fromCharCode(charCode));
} else if (charCode >= FAKE_ASCII_LOWERCASE_START && charCode <= FAKE_ASCII_LOWERCASE_END) {
chars.push(String.fromCharCode(charCode + ASCII_LOWERCASE_START - 1));
}
}
return chars.length === length ?
chars.join('') :
getChars(length, chars);
}
module.exports = lightRandom;
module.exports.lightRandom = lightRandom;