Cryptbase tests
This commit is contained in:
@@ -1,6 +1,116 @@
|
||||
|
||||
import base64
|
||||
import random
|
||||
import secrets
|
||||
import string
|
||||
|
||||
import cryptbase
|
||||
|
||||
|
||||
def test_main():
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user