-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypter.py
50 lines (34 loc) · 1.5 KB
/
decrypter.py
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
# SPDX-License-Identifier: MIT
# From https://github.com/nextcord/nextcord/blob/a00b3c6aa7d4ed0e9c01d238a26fda6c9f10367f/nextcord/voice_recording/decrypter.py
from struct import unpack_from
try:
from nacl.secret import SecretBox
def strip_header_ext(data):
if data[0] == 0xBE and data[1] == 0xDE and len(data) > 4:
_, length = unpack_from(">HH", data)
offset = 4 + length * 4
data = data[offset:]
return data
def decrypt_xsalsa20_poly1305(secret_key, header, data):
box = SecretBox(bytes(secret_key))
nonce = bytearray(24)
nonce[:12] = header
return strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))
def decrypt_xsalsa20_poly1305_suffix(secret_key, _, data):
box = SecretBox(bytes(secret_key))
nonce_size = SecretBox.NONCE_SIZE
nonce = data[-nonce_size:]
return strip_header_ext(box.decrypt(bytes(data[:-nonce_size]), nonce))
def decrypt_xsalsa20_poly1305_lite(secret_key, _, data):
box = SecretBox(bytes(secret_key))
nonce = bytearray(24)
nonce[:4] = data[-4:]
data = data[:-4]
return strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))
except ImportError:
def no_nacl(*_args, **_kwargs):
raise RuntimeError("PyNaCl library needed in order to use voice")
strip_header_ext = no_nacl
decrypt_xsalsa20_poly1305 = no_nacl
decrypt_xsalsa20_poly1305_suffix = no_nacl
decrypt_xsalsa20_poly1305_lite = no_nacl