-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgsid.js
More file actions
104 lines (94 loc) · 2.7 KB
/
gsid.js
File metadata and controls
104 lines (94 loc) · 2.7 KB
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
'use strict';
const crypto = require('crypto');
const { performance } = require('perf_hooks');
const os = require('os');
class GSID {
constructor() {
this.counter = 0;
this.lastTimestamp = 0;
this.machineId = GSID.generateMachineId();
this.processId = process.pid;
this.entropyPool = crypto.randomBytes(32);
this.entropyIndex = 0;
}
static generateMachineId() {
const networkInterfaces = os.networkInterfaces();
const macAddresses = [];
for (const interfaceName in networkInterfaces) {
const interfaces = networkInterfaces[interfaceName];
for (const iface of interfaces) {
if (iface.mac && iface.mac !== '00:00:00:00:00:00') {
macAddresses.push(iface.mac);
}
}
}
const hostname = os.hostname();
const combined = hostname + macAddresses.join('');
return crypto
.createHash('sha256')
.update(combined)
.digest('hex')
.substring(0, 8);
}
getTimestamp() {
const now = Date.now();
if (now === this.lastTimestamp) {
this.counter++;
} else {
this.counter = 0;
this.lastTimestamp = now;
}
return now;
}
getEntropyBytes(count) {
if (this.entropyIndex + count > this.entropyPool.length) {
this.entropyPool = crypto.randomBytes(32);
this.entropyIndex = 0;
}
const bytes = this.entropyPool.slice(
this.entropyIndex,
this.entropyIndex + count,
);
this.entropyIndex += count;
return bytes;
}
generate() {
const entropy = this.getEntropyBytes(8);
const buffer = Buffer.allocUnsafe(20);
buffer.writeUInt32BE(this.counter, 0);
buffer.writeUInt32BE(this.processId, 4);
buffer.set(Buffer.from(this.machineId, 'hex'), 8);
buffer.set(entropy, 12);
return buffer.toString('base64url').replace(/=/g, '');
}
static generateUUID() {
return crypto.randomUUID();
}
generateHybrid() {
const entropy = this.getEntropyBytes(6);
const buffer = Buffer.allocUnsafe(14);
buffer.writeUInt16BE(this.counter % 65536, 0);
buffer.writeUInt32BE(this.processId, 2);
buffer.set(Buffer.from(this.machineId.substring(0, 4), 'hex'), 6);
buffer.set(entropy, 8);
return buffer.toString('base64url').replace(/=/g, '');
}
benchmark(iterations = 100000) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
this.generate();
}
const gsidTime = performance.now() - start;
const uuidStart = performance.now();
for (let i = 0; i < iterations; i++) {
GSID.generateUUID();
}
const uuidTime = performance.now() - uuidStart;
return {
gsid: gsidTime,
uuid: uuidTime,
ratio: uuidTime / gsidTime,
};
}
}
module.exports = GSID;