-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsodium.js
49 lines (41 loc) · 1.89 KB
/
sodium.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
const libraries = {
'sodium-native': (sodium) => ({
crypto_aead_xchacha20poly1305_ietf_encrypt: (voice, packetBuffer, nonce, key) => {
const buffer = Buffer.alloc(voice.length + sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES)
return sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(buffer, null, voice, packetBuffer, nonce, key)
},
crypto_aead_xchacha20poly1305_ietf_decrypt: (cipherVoice, packetBuffer, nonce, key) => {
const buffer = Buffer.alloc(cipherVoice.length - sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES)
return sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(buffer, null, null, cipherVoice, packetBuffer, nonce, key)
}
}),
'libsodium-wrappers': (sodium) => ({
crypto_aead_xchacha20poly1305_ietf_encrypt: (voice, packetBuffer, nonce, key) => {
return sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(voice, packetBuffer, null, nonce, key)
},
crypto_aead_xchacha20poly1305_ietf_decrypt: (cipherVoice, packetBuffer, nonce, key) => {
return sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, cipherVoice, packetBuffer, nonce, key)
}
})
}
const functions = {
crypto_aead_xchacha20poly1305_ietf_encrypt: null,
crypto_aead_xchacha20poly1305_ietf_decrypt: null,
}
void (async () => {
let index = 0
Object.keys(libraries).forEach(async (name) => {
try {
const lib = await import(name)
if (functions.open) return;
if (name == 'libsodium-wrappers') await lib.default.ready
functions.crypto_aead_xchacha20poly1305_ietf_encrypt = libraries[name](lib.default).crypto_aead_xchacha20poly1305_ietf_encrypt
functions.crypto_aead_xchacha20poly1305_ietf_decrypt = libraries[name](lib.default).crypto_aead_xchacha20poly1305_ietf_decrypt
} catch {}
if (index == 2 && !functions.open) {
throw new Error('Could not load any sodium library')
}
index++
})
})()
export default functions