doc: add readthedocs config
[rarfile.git] / test / test_crypto.py
blob5cb576cc9b2f6a5536e13c09b681eae1896139d9
1 """Crypto tests.
2 """
4 from __future__ import division, print_function
6 from binascii import unhexlify
8 import pytest
10 import rarfile
12 try:
13 from cryptography.hazmat.backends import default_backend
14 from cryptography.hazmat.primitives.ciphers import (
15 Cipher, algorithms, modes,
17 def aes_encrypt(key, iv, data):
18 ciph = Cipher(algorithms.AES(key), modes.CBC(iv), default_backend())
19 enc = ciph.encryptor()
20 return enc.update(data)
21 except ImportError:
22 pass
25 @pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
26 def test_aes128_cbc():
27 data = b"0123456789abcdef" * 2
28 key = b"\x02" * 16
29 iv = b"\x80" * 16
31 #encdata = aes_encrypt(key, iv, data)
32 encdata = unhexlify("4b0d438b4a1b972bd4ab81cd64674dcce4b0158090fbe616f455354284d53502")
34 ctx = rarfile.AES_CBC_Decrypt(key, iv)
35 assert ctx.decrypt(encdata) == data
38 @pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
39 def test_aes256_cbc():
40 data = b"0123456789abcdef" * 2
41 key = b"\x52" * 32
42 iv = b"\x70" * 16
44 #encdata = aes_encrypt(key, iv, data)
45 encdata = unhexlify("24988f387592e4d95b6eaab013137a221f81b25aa7ecde0ef4f4d7a95f92c250")
47 ctx = rarfile.AES_CBC_Decrypt(key, iv)
48 assert ctx.decrypt(encdata) == data