forked from cypherstack/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
22 lines (18 loc) · 750 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Utility functions
from chacha20poly1305 import ChaCha20Poly1305, TagInvalidException
from hashlib import blake2s
from siphash import SipHash_2_4
def aead_encrypt(prekey,header,plaintext):
key = blake2s(repr(prekey).encode('utf-8')).digest()
nonce = bytearray(12)
cipher = ChaCha20Poly1305(key)
return cipher.encrypt(nonce,plaintext,header.encode('utf-8'))
def aead_decrypt(prekey,header,ciphertext):
key = blake2s(repr(prekey).encode('utf-8')).digest()
nonce = bytearray(12)
cipher = ChaCha20Poly1305(key)
plaintext = cipher.decrypt(nonce,ciphertext,header.encode('utf-8'))
if plaintext is not None:
return plaintext
def view_tag(key):
return SipHash_2_4(bytes.fromhex(repr(key))[:16],'Spark view tag'.encode('utf-8')).digest()[:1]