Ejemplo n.º 1
0
def test_encrypted_binary_field_can_rotate():
    key_1 = aes_generate_key()
    token = EncryptedBinaryField(key_1)
    encrypted_data = token.to_mongo(b'\x00\x01')

    key_2 = aes_generate_key()
    token = EncryptedBinaryField([key_2, key_1])
    assert token.to_python(encrypted_data) == b'\x00\x01'
Ejemplo n.º 2
0
def test_data_encrypted_twice_is_different():
    data = b'test'
    key = aes_generate_key()

    first_encryption = aes_encrypt(key, data)
    second_encryption = aes_encrypt(key, data)
    assert first_encryption != second_encryption
Ejemplo n.º 3
0
def test_encrypted_binary_field_will_fail_on_corrupted_data():
    key_1 = aes_generate_key()
    token = EncryptedBinaryField(key_1)
    corrupted_encrypted_data = token.to_mongo(b'\x00\x01')[:3]
    with pytest.raises(AuthenticationError) as excinfo:
        token.to_python(corrupted_encrypted_data)
    assert str(excinfo.value) == 'message authentication failed'
Ejemplo n.º 4
0
def test_with_v1_corrupted_data():
    data = b'test'
    key = aes_generate_key()
    encrypted_data = aes_encrypt(key, data)
    assert encrypted_data[0:1] == b'\x01'
    corrupted_encrypted_data = encrypted_data[:-3]
    with pytest.raises(AuthenticationError) as excinfo:
        aes_decrypt(key, corrupted_encrypted_data)
    assert str(excinfo.value) == "message authentication failed"
Ejemplo n.º 5
0
def test_with_data_longer_than_aes_block():
    data = b'a' * 130
    key = aes_generate_key()
    assert aes_decrypt(key, aes_encrypt(key, data)) == data
Ejemplo n.º 6
0
def test_with_data_exactly_as_long_as_aes_block():
    data = b'a' * 128
    key = aes_generate_key()
    assert aes_decrypt(key, aes_encrypt(key, data)) == data
Ejemplo n.º 7
0
def test_with_v1_data():
    data = b'test'
    key = aes_generate_key()
    encrypted_data = aes_encrypt(key, data)
    assert encrypted_data[0:1] == b'\x01'
    assert aes_decrypt(key, encrypted_data) == data
Ejemplo n.º 8
0
def test_encrypted_string_field_works_with_unicode_data():
    token = EncryptedStringField(aes_generate_key())
    assert token.to_python(token.to_mongo(u'ãé')) == u'ãé'
Ejemplo n.º 9
0
def test_encrypted_binary_field_with_none():
    token = EncryptedBinaryField(aes_generate_key())
    assert token.to_python(token.to_mongo(None)) is None
Ejemplo n.º 10
0
def test_encrypted_binary_field_can_encrypt_and_decrypt():
    token = EncryptedBinaryField(aes_generate_key())
    assert token.to_python(token.to_mongo(b'\x00\x01')) == b'\x00\x01'