-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
67 lines (52 loc) · 1.94 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
const { Binary } = require('mongodb')
module.exports = {
binToJUUIDString (binary) {
let hex = binary.buffer.toString('hex')
let msb = hex.substr(0, 16)
let lsb = hex.substr(16, 16)
msb = significantBit(msb)
lsb = significantBit(lsb)
hex = msb + lsb
const uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) +
'-' + hex.substr(16, 4) + '-' + hex.substr(20, 12)
return uuid
},
juuidStringToBin (juuid) {
let hex = juuid.replace(/[{}-]/g, '') // remove extra characters
let msb = hex.substr(0, 16)
let lsb = hex.substr(16, 16)
msb = significantBit(msb)
lsb = significantBit(lsb)
hex = msb + lsb
return new Binary(Buffer.from(hexToBase64(hex), 'base64'), 3)
},
binToCSUUIDString (binary) {
let hex = binary.buffer.toString('hex')
const a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2)
const b = hex.substr(10, 2) + hex.substr(8, 2)
const c = hex.substr(14, 2) + hex.substr(12, 2)
const d = hex.substr(16, 16)
hex = a + b + c + d
const uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) +
'-' + hex.substr(16, 4) + '-' + hex.substr(20, 12)
return uuid
},
csuuidStringToBin (csuuid) {
let hex = csuuid.replace(/[{}-]/g, '') // remove extra characters
const a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2)
const b = hex.substr(10, 2) + hex.substr(8, 2)
const c = hex.substr(14, 2) + hex.substr(12, 2)
const d = hex.substr(16, 16)
hex = a + b + c + d
return new Binary(Buffer.from(hexToBase64(hex), 'base64'), 3)
},
hexToBase64
}
function hexToBase64 (hex) {
return Buffer.from(hex, 'hex').toString('base64')
}
function significantBit (msb) {
msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) +
msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2)
return msb
}