You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from youtube_dl.aes import aes_decrypt, aes_encrypt, aes_cbc_decrypt, aes_decrypt_text
  9. from youtube_dl.utils import bytes_to_intlist, intlist_to_bytes
  10. import base64
  11. # the encrypted data can be generate with 'devscripts/generate_aes_testdata.py'
  12. class TestAES(unittest.TestCase):
  13. def setUp(self):
  14. self.key = self.iv = [0x20, 0x15] + 14 * [0]
  15. self.secret_msg = b'Secret message goes here'
  16. def test_encrypt(self):
  17. msg = b'message'
  18. key = list(range(16))
  19. encrypted = aes_encrypt(bytes_to_intlist(msg), key)
  20. decrypted = intlist_to_bytes(aes_decrypt(encrypted, key))
  21. self.assertEqual(decrypted, msg)
  22. def test_cbc_decrypt(self):
  23. data = bytes_to_intlist(
  24. b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd"
  25. )
  26. decrypted = intlist_to_bytes(aes_cbc_decrypt(data, self.key, self.iv))
  27. self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
  28. def test_decrypt_text(self):
  29. password = intlist_to_bytes(self.key).decode('utf-8')
  30. encrypted = base64.b64encode(
  31. intlist_to_bytes(self.iv[:8]) +
  32. b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
  33. )
  34. decrypted = (aes_decrypt_text(encrypted, password, 16))
  35. self.assertEqual(decrypted, self.secret_msg)
  36. if __name__ == '__main__':
  37. unittest.main()