]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_aes.py
Update changelog.
[youtubedl] / test / test_aes.py
1 #!/usr/bin/env python
2
3 from __future__ import unicode_literals
4
5 # Allow direct execution
6 import os
7 import sys
8 import unittest
9 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
11 from youtube_dl.aes import aes_decrypt, aes_encrypt, aes_cbc_decrypt, aes_decrypt_text
12 from youtube_dl.utils import bytes_to_intlist, intlist_to_bytes
13 import base64
14
15 # the encrypted data can be generate with 'devscripts/generate_aes_testdata.py'
16
17
18 class TestAES(unittest.TestCase):
19 def setUp(self):
20 self.key = self.iv = [0x20, 0x15] + 14 * [0]
21 self.secret_msg = b'Secret message goes here'
22
23 def test_encrypt(self):
24 msg = b'message'
25 key = list(range(16))
26 encrypted = aes_encrypt(bytes_to_intlist(msg), key)
27 decrypted = intlist_to_bytes(aes_decrypt(encrypted, key))
28 self.assertEqual(decrypted, msg)
29
30 def test_cbc_decrypt(self):
31 data = bytes_to_intlist(
32 b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd"
33 )
34 decrypted = intlist_to_bytes(aes_cbc_decrypt(data, self.key, self.iv))
35 self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
36
37 def test_decrypt_text(self):
38 password = intlist_to_bytes(self.key).decode('utf-8')
39 encrypted = base64.b64encode(
40 intlist_to_bytes(self.iv[:8]) +
41 b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
42 )
43 decrypted = (aes_decrypt_text(encrypted, password, 16))
44 self.assertEqual(decrypted, self.secret_msg)
45
46 password = intlist_to_bytes(self.key).decode('utf-8')
47 encrypted = base64.b64encode(
48 intlist_to_bytes(self.iv[:8]) +
49 b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
50 )
51 decrypted = (aes_decrypt_text(encrypted, password, 32))
52 self.assertEqual(decrypted, self.secret_msg)
53
54 if __name__ == '__main__':
55 unittest.main()