mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-30 12:01:28 +00:00
[utils] Add generic caesar cipher and rot47
This commit is contained in:
parent
6ddd4bf6ac
commit
1ced222120
|
@ -19,6 +19,7 @@
|
||||||
age_restricted,
|
age_restricted,
|
||||||
args_to_str,
|
args_to_str,
|
||||||
encode_base_n,
|
encode_base_n,
|
||||||
|
caesar,
|
||||||
clean_html,
|
clean_html,
|
||||||
date_from_str,
|
date_from_str,
|
||||||
DateRange,
|
DateRange,
|
||||||
|
@ -69,6 +70,7 @@
|
||||||
remove_start,
|
remove_start,
|
||||||
remove_end,
|
remove_end,
|
||||||
remove_quotes,
|
remove_quotes,
|
||||||
|
rot47,
|
||||||
shell_quote,
|
shell_quote,
|
||||||
smuggle_url,
|
smuggle_url,
|
||||||
str_to_int,
|
str_to_int,
|
||||||
|
@ -1369,6 +1371,20 @@ def test_encode_base_n(self):
|
||||||
self.assertRaises(ValueError, encode_base_n, 0, 70)
|
self.assertRaises(ValueError, encode_base_n, 0, 70)
|
||||||
self.assertRaises(ValueError, encode_base_n, 0, 60, custom_table)
|
self.assertRaises(ValueError, encode_base_n, 0, 60, custom_table)
|
||||||
|
|
||||||
|
def test_caesar(self):
|
||||||
|
self.assertEqual(caesar('ace', 'abcdef', 2), 'cea')
|
||||||
|
self.assertEqual(caesar('cea', 'abcdef', -2), 'ace')
|
||||||
|
self.assertEqual(caesar('ace', 'abcdef', -2), 'eac')
|
||||||
|
self.assertEqual(caesar('eac', 'abcdef', 2), 'ace')
|
||||||
|
self.assertEqual(caesar('ace', 'abcdef', 0), 'ace')
|
||||||
|
self.assertEqual(caesar('xyz', 'abcdef', 2), 'xyz')
|
||||||
|
self.assertEqual(caesar('abc', 'acegik', 2), 'ebg')
|
||||||
|
self.assertEqual(caesar('ebg', 'acegik', -2), 'abc')
|
||||||
|
|
||||||
|
def test_rot47(self):
|
||||||
|
self.assertEqual(rot47('youtube-dl'), r'J@FEF36\5=')
|
||||||
|
self.assertEqual(rot47('YOUTUBE-DL'), r'*~&%&qt\s{')
|
||||||
|
|
||||||
def test_urshift(self):
|
def test_urshift(self):
|
||||||
self.assertEqual(urshift(3, 1), 1)
|
self.assertEqual(urshift(3, 1), 1)
|
||||||
self.assertEqual(urshift(-3, 1), 2147483646)
|
self.assertEqual(urshift(-3, 1), 2147483646)
|
||||||
|
|
|
@ -5383,6 +5383,19 @@ def decode_packed_codes(code):
|
||||||
obfucasted_code)
|
obfucasted_code)
|
||||||
|
|
||||||
|
|
||||||
|
def caesar(s, alphabet, shift):
|
||||||
|
if shift == 0:
|
||||||
|
return s
|
||||||
|
l = len(alphabet)
|
||||||
|
return ''.join(
|
||||||
|
alphabet[(alphabet.index(c) + shift) % l] if c in alphabet else c
|
||||||
|
for c in s)
|
||||||
|
|
||||||
|
|
||||||
|
def rot47(s):
|
||||||
|
return caesar(s, r'''!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~''', 47)
|
||||||
|
|
||||||
|
|
||||||
def parse_m3u8_attributes(attrib):
|
def parse_m3u8_attributes(attrib):
|
||||||
info = {}
|
info = {}
|
||||||
for (key, val) in re.findall(r'(?P<key>[A-Z0-9-]+)=(?P<val>"[^"]+"|[^",]+)(?:,|$)', attrib):
|
for (key, val) in re.findall(r'(?P<key>[A-Z0-9-]+)=(?P<val>"[^"]+"|[^",]+)(?:,|$)', attrib):
|
||||||
|
|
Loading…
Reference in a new issue