pyUmbral/umbral/dem.py

30 lines
866 B
Python
Raw Normal View History

from nacl.secret import SecretBox
2018-01-23 07:40:36 +00:00
class UmbralDEM(object):
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:
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