mirror of
				https://github.com/yt-dlp/yt-dlp.git
				synced 2025-10-31 06:35:12 +00:00 
			
		
		
		
	[aes] Add aes_gcm_decrypt_and_verify (#1020)
				
					
				
			Authored by: sulyi, pukkandan
This commit is contained in:
		| @@ -7,7 +7,19 @@ import sys | ||||
| import unittest | ||||
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||||
|  | ||||
| from yt_dlp.aes import aes_decrypt, aes_encrypt, aes_cbc_decrypt, aes_cbc_encrypt, aes_decrypt_text | ||||
| from yt_dlp.aes import ( | ||||
|     aes_decrypt, | ||||
|     aes_encrypt, | ||||
|     aes_cbc_decrypt, | ||||
|     aes_cbc_decrypt_bytes, | ||||
|     aes_cbc_encrypt, | ||||
|     aes_ctr_decrypt, | ||||
|     aes_ctr_encrypt, | ||||
|     aes_gcm_decrypt_and_verify, | ||||
|     aes_gcm_decrypt_and_verify_bytes, | ||||
|     aes_decrypt_text | ||||
| ) | ||||
| from yt_dlp.compat import compat_pycrypto_AES | ||||
| from yt_dlp.utils import bytes_to_intlist, intlist_to_bytes | ||||
| import base64 | ||||
|  | ||||
| @@ -27,18 +39,43 @@ class TestAES(unittest.TestCase): | ||||
|         self.assertEqual(decrypted, msg) | ||||
|  | ||||
|     def test_cbc_decrypt(self): | ||||
|         data = bytes_to_intlist( | ||||
|             b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd" | ||||
|         ) | ||||
|         decrypted = intlist_to_bytes(aes_cbc_decrypt(data, self.key, self.iv)) | ||||
|         data = b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\x27\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd' | ||||
|         decrypted = intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(data), self.key, self.iv)) | ||||
|         self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | ||||
|         if compat_pycrypto_AES: | ||||
|             decrypted = aes_cbc_decrypt_bytes(data, intlist_to_bytes(self.key), intlist_to_bytes(self.iv)) | ||||
|             self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | ||||
|  | ||||
|     def test_cbc_encrypt(self): | ||||
|         data = bytes_to_intlist(self.secret_msg) | ||||
|         encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv)) | ||||
|         self.assertEqual( | ||||
|             encrypted, | ||||
|             b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd") | ||||
|             b'\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6\'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd') | ||||
|  | ||||
|     def test_ctr_decrypt(self): | ||||
|         data = bytes_to_intlist(b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08') | ||||
|         decrypted = intlist_to_bytes(aes_ctr_decrypt(data, self.key, self.iv)) | ||||
|         self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | ||||
|  | ||||
|     def test_ctr_encrypt(self): | ||||
|         data = bytes_to_intlist(self.secret_msg) | ||||
|         encrypted = intlist_to_bytes(aes_ctr_encrypt(data, self.key, self.iv)) | ||||
|         self.assertEqual( | ||||
|             encrypted, | ||||
|             b'\x03\xc7\xdd\xd4\x8e\xb3\xbc\x1a*O\xdc1\x12+8Aio\xd1z\xb5#\xaf\x08') | ||||
|  | ||||
|     def test_gcm_decrypt(self): | ||||
|         data = b'\x159Y\xcf5eud\x90\x9c\x85&]\x14\x1d\x0f.\x08\xb4T\xe4/\x17\xbd' | ||||
|         authentication_tag = b'\xe8&I\x80rI\x07\x9d}YWuU@:e' | ||||
|  | ||||
|         decrypted = intlist_to_bytes(aes_gcm_decrypt_and_verify( | ||||
|             bytes_to_intlist(data), self.key, bytes_to_intlist(authentication_tag), self.iv[:12])) | ||||
|         self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | ||||
|         if compat_pycrypto_AES: | ||||
|             decrypted = aes_gcm_decrypt_and_verify_bytes( | ||||
|                 data, intlist_to_bytes(self.key), authentication_tag, intlist_to_bytes(self.iv[:12])) | ||||
|             self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg) | ||||
|  | ||||
|     def test_decrypt_text(self): | ||||
|         password = intlist_to_bytes(self.key).decode('utf-8') | ||||
|   | ||||
| @@ -2,7 +2,6 @@ import unittest | ||||
| from datetime import datetime, timezone | ||||
|  | ||||
| from yt_dlp import cookies | ||||
| from yt_dlp.compat import compat_pycrypto_AES | ||||
| from yt_dlp.cookies import ( | ||||
|     LinuxChromeCookieDecryptor, | ||||
|     MacChromeCookieDecryptor, | ||||
| @@ -53,7 +52,6 @@ class TestCookies(unittest.TestCase): | ||||
|             decryptor = LinuxChromeCookieDecryptor('Chrome', YDLLogger()) | ||||
|             self.assertEqual(decryptor.decrypt(encrypted_value), value) | ||||
|  | ||||
|     @unittest.skipIf(not compat_pycrypto_AES, 'cryptography library not available') | ||||
|     def test_chrome_cookie_decryptor_windows_v10(self): | ||||
|         with MonkeyPatch(cookies, { | ||||
|             '_get_windows_v10_key': lambda *args, **kwargs: b'Y\xef\xad\xad\xeerp\xf0Y\xe6\x9b\x12\xc2<z\x16]\n\xbb\xb8\xcb\xd7\x9bA\xc3\x14e\x99{\xd6\xf4&' | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Ákos Sülyi
					Ákos Sülyi