def test_cbc256_encrypt(self): key = bytes.fromhex(""" 603DEB10 15CA71BE 2B73AEF0 857D7781 1F352C07 3B6108D7 2D9810A3 0914DFF4 """.replace(" ", "").replace("\n", "")) iv = bytes.fromhex(""" 00010203 04050607 08090A0B 0C0D0E0F """.replace(" ", "").replace("\n", "")) plaintext = bytes.fromhex(""" 6BC1BEE2 2E409F96 E93D7E11 7393172A AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51 30C81C46 A35CE411 E5FBC119 1A0A52EF F69F2445 DF4F9B17 AD2B417B E66C3710 """.replace(" ", "").replace("\n", "")) ciphertext = bytes.fromhex(""" F58C4C04 D6E5F1BA 779EABFB 5F7BFBD6 9CFC4E96 7EDB808D 679F777B C6702C7D 39F23369 A9D9BACF A530E263 04231461 B2EB05E2 C39BE9FC DA6C1907 8C6A9D1B """.replace(" ", "").replace("\n", "")) self.assertEqual(tgcrypto.cbc256_encrypt(plaintext, key, iv), ciphertext)
def test_cbc256_encrypt_invalid_iv_size(self): with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"): tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(32), os.urandom(15))
def test_cbc256_encrypt_invalid_key_size(self): with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"): tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(31), os.urandom(16))
def test_cbc256_encrypt_empty_data(self): with self.assertRaisesRegex(ValueError, r"Data must not be empty"): tgcrypto.cbc256_encrypt(b"", os.urandom(32), os.urandom(16))
def test_cbc256_encrypt_invalid_args_type(self): with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN): tgcrypto.cbc256_encrypt(1, 2, 3)
def test_cbc256_encrypt_invalid_args_count(self): with self.assertRaisesRegex( TypeError, r"function takes exactly \d arguments \(\d given\)"): tgcrypto.cbc256_encrypt(os.urandom(16), os.urandom(32))
def encrypt(self, data): return tgcrypto.cbc256_encrypt(data, self.key, self.iv)