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.

55 lines
2.0 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. password = intlist_to_bytes(self.key).decode('utf-8')
  37. encrypted = base64.b64encode(
  38. intlist_to_bytes(self.iv[:8]) +
  39. b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
  40. )
  41. decrypted = (aes_decrypt_text(encrypted, password, 32))
  42. self.assertEqual(decrypted, self.secret_msg)
  43. if __name__ == '__main__':
  44. unittest.main()