forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid4.js
39 lines (36 loc) · 840 Bytes
/
uuid4.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
var Node = {
crypto: require('crypto')
};
const TABLE = [];
for (var index = 0; index < 256; index++) {
TABLE[index] = (index + 0x100).toString(16).substr(1);
}
function UUID4() {
// TO DO: Optimize by replacing randomBytes() with AES stream cipher.
var buffer = Node.crypto.randomBytes(16);
buffer[6] = (buffer[6] & 0x0f) | 0x40;
buffer[8] = (buffer[8] & 0x3f) | 0x80;
return [
TABLE[buffer[ 0]],
TABLE[buffer[ 1]],
TABLE[buffer[ 2]],
TABLE[buffer[ 3]],
'-',
TABLE[buffer[ 4]],
TABLE[buffer[ 5]],
'-',
TABLE[buffer[ 6]],
TABLE[buffer[ 7]],
'-',
TABLE[buffer[ 8]],
TABLE[buffer[ 9]],
'-',
TABLE[buffer[10]],
TABLE[buffer[11]],
TABLE[buffer[12]],
TABLE[buffer[13]],
TABLE[buffer[14]],
TABLE[buffer[15]],
].join('');
}
module.exports = UUID4;