Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import hashlib
2import base64
3import struct
4from lib import sslcrypto
5from Crypt import Crypt
6
7
8curve = sslcrypto.ecc.get_curve("secp256k1")
9
10
11def eciesEncrypt(data, pubkey, ciphername="aes-256-cbc"):
12 ciphertext, key_e = curve.encrypt(
13 data,
14 base64.b64decode(pubkey),
15 algo=ciphername,
16 derivation="sha512",
17 return_aes_key=True
18 )
19 return key_e, ciphertext
20
21
22@Crypt.thread_pool_crypt.wrap
23def eciesDecryptMulti(encrypted_datas, privatekey):
24 texts = [] # Decoded texts
25 for encrypted_data in encrypted_datas:
26 try:
27 text = eciesDecrypt(encrypted_data, privatekey).decode("utf8")
28 texts.append(text)
29 except Exception:
30 texts.append(None)
31 return texts
32
33
34def eciesDecrypt(ciphertext, privatekey):
35 return curve.decrypt(base64.b64decode(ciphertext), curve.wif_to_private(privatekey.encode()), derivation="sha512")
36
37
38def decodePubkey(pubkey):
39 i = 0
40 curve = struct.unpack('!H', pubkey[i:i + 2])[0]
41 i += 2
42 tmplen = struct.unpack('!H', pubkey[i:i + 2])[0]
43 i += 2
44 pubkey_x = pubkey[i:i + tmplen]
45 i += tmplen
46 tmplen = struct.unpack('!H', pubkey[i:i + 2])[0]
47 i += 2
48 pubkey_y = pubkey[i:i + tmplen]
49 i += tmplen
50 return curve, pubkey_x, pubkey_y, i
51
52
53def split(encrypted):
54 iv = encrypted[0:16]
55 curve, pubkey_x, pubkey_y, i = decodePubkey(encrypted[16:])
56 ciphertext = encrypted[16 + i:-32]
57
58 return iv, ciphertext