Cryptbase tests

This commit is contained in:
2021-10-09 11:00:46 +02:00
parent 8928a0c516
commit 46fdda871f
2 changed files with 116 additions and 2 deletions

View File

@@ -9,6 +9,10 @@ from cryptography.hazmat.primitives.ciphers import modes
class AES256CTREncryptor: class AES256CTREncryptor:
def __init__(self, key): def __init__(self, key):
if not isinstance(key, bytes):
raise ValueError()
if not len(key) == 32:
raise ValueError()
self.__key = key self.__key = key
def encrypt(self, plaintext): def encrypt(self, plaintext):
@@ -36,13 +40,13 @@ class CryptoContainer:
elif 'ciphertext' in kwargs and 'iv' in kwargs: elif 'ciphertext' in kwargs and 'iv' in kwargs:
self.__ciphertext = kwargs['ciphertext'] self.__ciphertext = kwargs['ciphertext']
self.__iv = kwargs['iv'] self.__iv = kwargs['iv']
self.__plaintext = kwargs['encryptor'].decrypt(self.__ciphertext, self.__iv) self.__plaintext = kwargs['encryptor'].decrypt(self.__ciphertext, self.__iv).decode('utf-8')
else: else:
raise ValueError() raise ValueError()
@property @property
def plaintext(self): def plaintext(self):
return self.__plaintext.decode('utf-8') return self.__plaintext
@property @property
def ciphertext(self): def ciphertext(self):

View File

@@ -1,6 +1,116 @@
import base64
import random
import secrets
import string
import cryptbase import cryptbase
def test_main(): def test_main():
assert cryptbase.__version__ == '0.0.0' assert cryptbase.__version__ == '0.0.0'
def test_crypto():
aes_n = 256
key_bytes = int(aes_n / 8)
population = string.ascii_letters + string.digits + string.punctuation
length = 2048
for _ in range(16):
key = secrets.token_bytes(key_bytes)
encryptor = cryptbase.cryptbase.AES256CTREncryptor(key)
for _ in range(32):
plaintext = ''.join(random.choices(population, k=length)).encode('ascii')
assert plaintext == encryptor.decrypt(*encryptor.encrypt(plaintext))
def test_encryptor():
try:
cryptbase.cryptbase.AES256CTREncryptor(None)
assert False
except ValueError:
pass
try:
cryptbase.cryptbase.AES256CTREncryptor(bytes([]))
assert False
except ValueError:
pass
try:
key = secrets.token_bytes(1)
cryptbase.cryptbase.AES256CTREncryptor(key)
assert False
except ValueError:
pass
def test_containter_encrypt():
aes_n = 256
key_bytes = int(aes_n / 8)
population = string.ascii_letters + string.digits + string.punctuation
length = 2048
for _ in range(16):
key = secrets.token_bytes(key_bytes)
encryptor = cryptbase.cryptbase.AES256CTREncryptor(key)
for _ in range(32):
plaintext = ''.join(random.choices(population, k=length))
container = cryptbase.cryptbase.CryptoContainer(encryptor=encryptor, plaintext=plaintext)
assert container.plaintext == plaintext
assert str(container) == '@'.join([container.ciphertext, container.iv])
cipher_b64 = container.ciphertext
iv_b64 = container.iv
ciphertext = base64.b64decode(cipher_b64)
iv = base64.b64decode(iv_b64)
decrypted = encryptor.decrypt(ciphertext, iv).decode('utf-8')
assert type(plaintext) == type(decrypted)
assert len(plaintext) == len(decrypted)
assert plaintext == decrypted
decontainer = cryptbase.cryptbase.CryptoContainer.from_encrypted(str(container), encryptor)
assert decontainer.plaintext == plaintext
assert decontainer.iv == iv_b64
assert decontainer.ciphertext == cipher_b64
def test_container_init():
try:
cryptbase.cryptbase.CryptoContainer()
assert False
except ValueError:
pass
try:
cryptbase.cryptbase.CryptoContainer(encryptor=None)
assert False
except ValueError:
pass
try:
cryptbase.cryptbase.CryptoContainer(encryptor=None, plaintext=None)
assert False
except AttributeError:
pass
try:
cryptbase.cryptbase.CryptoContainer(encryptor=None, ciphertext=None)
assert False
except ValueError:
pass
try:
cryptbase.cryptbase.CryptoContainer(encryptor=None, iv=None)
assert False
except ValueError:
pass
try:
cryptbase.cryptbase.CryptoContainer(encryptor=None, ciphertext=None, iv=None)
assert False
except AttributeError:
pass