-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrandom-chars.js
executable file
·73 lines (64 loc) · 1.59 KB
/
random-chars.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
/**
* Encode using these characters.
*
* The array length must be 16, to map one byte to two chars.
*/
const chars = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'];
/**
* Byte to string
*
* Map each byte to a two-character string, by permuting valid characters.
* The array length must be 256, such as ['AA','AB','AC',..,'PN','PO','PP'].
*
* Example: byteToStringArr[0] => "AA"
*/
const byteToStringArr = [].concat.apply(
[],
chars.map(function(a){
return chars.map(function(b){
return a + b;
})
})
);
/**
* Convert a byte to its string.
*
* Example: byteToString(0) => "AA"
*/
function byteToString(byte){
return byteToStringArr[byte];
}
/**
* Convert a byte array to its string.
*
* Example: byteToString([0, 255]) => "AAPP"
*/
function bytesToString(bytes){
return bytes.map(byteToString).join("");
}
/**
* Get random bytes using the browser's typical crypto package.
* We isolate the randomness in just this one function, so we
* can chanage this later as we want, and also to ease auditing.
*/
function randomBytes(length) {
var a = new Uint8Array(length);
window.crypto.getRandomValues(a);
return [].slice.call(a);
}
/**
* Generate a random string of a given length.
*
* Example: generate(4) => "ABCD"
*/
function generate(length) {
return bytesToString(randomBytes(length/2))
}
/**
* Separate a string every _length_ characters using a _separator_ string.
*
* Example: separate("ABCDEFGHIJKL", 3, "-") => "ABC-DEF-GHI-JKL"
*/
function separate(s, length, separator) {
return st.match(/.{1,length}/g).join(separator);
}