2018-01-23 09:40:13 +00:00
|
|
|
from nacl.secret import SecretBox
|
2018-01-23 07:40:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UmbralDEM(object):
|
2018-01-23 09:40:13 +00:00
|
|
|
def __init__(self, symm_key: bytes):
|
|
|
|
"""
|
|
|
|
Initializes an UmbralDEM object. Requires a key to perform
|
|
|
|
Salsa20-Poly1305.
|
|
|
|
"""
|
2018-01-23 11:28:51 +00:00
|
|
|
if len(symm_key) != SecretBox.KEY_SIZE:
|
2018-01-23 09:40:13 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Invalid key size, must be {} bytes".format(SecretBox.KEY_SIZE)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.cipher = SecretBox(symm_key)
|
|
|
|
|
|
|
|
def encrypt(self, data: bytes):
|
|
|
|
"""
|
|
|
|
Encrypts data using NaCl's Salsa20-Poly1305 secret box symmetric cipher.
|
|
|
|
"""
|
|
|
|
enc_data = self.cipher.encrypt(data)
|
|
|
|
return enc_data
|
|
|
|
|
|
|
|
def decrypt(self, enc_data: bytes):
|
|
|
|
"""
|
|
|
|
Decrypts data using NaCl's Salsa20-Poly1305 secret box symmetric cipher.
|
|
|
|
"""
|
|
|
|
plaintext = self.cipher.decrypt(enc_data)
|
|
|
|
return plaintext
|