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.

63 lines
2.3 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_cbc_encrypt, 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_cbc_encrypt(self):
  29. data = bytes_to_intlist(self.secret_msg)
  30. encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv))
  31. self.assertEqual(
  32. encrypted,
  33. b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd")
  34. def test_decrypt_text(self):
  35. password = intlist_to_bytes(self.key).decode('utf-8')
  36. encrypted = base64.b64encode(
  37. intlist_to_bytes(self.iv[:8]) +
  38. b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
  39. ).decode('utf-8')
  40. decrypted = (aes_decrypt_text(encrypted, password, 16))
  41. self.assertEqual(decrypted, self.secret_msg)
  42. password = intlist_to_bytes(self.key).decode('utf-8')
  43. encrypted = base64.b64encode(
  44. intlist_to_bytes(self.iv[:8]) +
  45. b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
  46. ).decode('utf-8')
  47. decrypted = (aes_decrypt_text(encrypted, password, 32))
  48. self.assertEqual(decrypted, self.secret_msg)
  49. if __name__ == '__main__':
  50. unittest.main()