]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_aes.py
New upstream version 2020.05.08
[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_cbc_encrypt, 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_cbc_encrypt(self):
38 data = bytes_to_intlist(self.secret_msg)
39 encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv))
40 self.assertEqual(
41 encrypted,
42 b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd")
43
44 def test_decrypt_text(self):
45 password = intlist_to_bytes(self.key).decode('utf-8')
46 encrypted = base64.b64encode(
47 intlist_to_bytes(self.iv[:8])
48 + b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
49 ).decode('utf-8')
50 decrypted = (aes_decrypt_text(encrypted, password, 16))
51 self.assertEqual(decrypted, self.secret_msg)
52
53 password = intlist_to_bytes(self.key).decode('utf-8')
54 encrypted = base64.b64encode(
55 intlist_to_bytes(self.iv[:8])
56 + b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
57 ).decode('utf-8')
58 decrypted = (aes_decrypt_text(encrypted, password, 32))
59 self.assertEqual(decrypted, self.secret_msg)
60
61
62 if __name__ == '__main__':
63 unittest.main()