[utils] Deprecate long_to_bytes and bytes_to_long (#17630)

Authored by: doe1080
This commit is contained in:
doe1080
2026-12-01 04:50:57 +00:00
committed by GitHub
parent e53630e9d0
commit c07411aa75
4 changed files with 23 additions and 56 deletions
+2
View File
@@ -393,8 +393,10 @@ banned-from = [
"yt_dlp.compat.compat_shlex_quote".msg = "Use `yt_dlp.utils.shell_quote` instead."
"yt_dlp.utils.error_to_compat_str".msg = "Use `str` instead."
"yt_dlp.utils.bytes_to_intlist".msg = "Use `list` instead."
"yt_dlp.utils.bytes_to_long".msg = "Use `int.from_bytes(..., 'big')` instead."
"yt_dlp.utils.intlist_to_bytes".msg = "Use `bytes` instead."
"yt_dlp.utils.jwt_encode_hs256".msg = "Use `yt_dlp.utils.jwt_encode` instead."
"yt_dlp.utils.long_to_bytes".msg = "Do not use"
"yt_dlp.utils.make_dir".msg = "Use `yt_dlp.utils.make_parent_dirs` instead."
"yt_dlp.utils.number_of_digits".msg = "Do not use"
"yt_dlp.utils.random_uuidv4".msg = "Use `str(uuid.uuid4())` instead."
+6 -5
View File
@@ -11,11 +11,9 @@ from ..networking.exceptions import HTTPError
from ..utils import (
ExtractorError,
ass_subtitles_timecode,
bytes_to_long,
float_or_none,
int_or_none,
join_nonempty,
long_to_bytes,
parse_iso8601,
pkcs1pad,
str_or_none,
@@ -202,13 +200,16 @@ Format: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text'''
't': token,
}).encode())
n, e = self._RSA_KEY
key_size = (n.bit_length() + 7) // 8
# Sometimes authentication fails for no good reason, retry with
# a different random padding
links_data = None
for _ in range(3):
padded_message = bytes(pkcs1pad(message, 128))
n, e = self._RSA_KEY
encrypted_message = long_to_bytes(pow(bytes_to_long(padded_message), e, n))
padded_message = bytes(pkcs1pad(message, key_size))
encrypted_message = pow(
int.from_bytes(padded_message, 'big'), e, n).to_bytes(key_size, 'big')
authorization = base64.b64encode(encrypted_message).decode()
try:
+15
View File
@@ -67,4 +67,19 @@ def random_uuidv4():
return str(uuid.uuid4())
def bytes_to_long(s):
return int.from_bytes(s, 'big')
def long_to_bytes(n, blocksize=0):
n = int(n)
if n <= 0:
return bytes(blocksize) if blocksize > 0 else b'\x00'
length = (n.bit_length() + 7) // 8
if blocksize > 0:
length += -length % blocksize
return n.to_bytes(length, 'big')
compiled_regex_type = type(re.compile(''))
-51
View File
@@ -4379,57 +4379,6 @@ class GeoUtils:
struct.pack('!L', random.randint(addr_min, addr_max))))
# Both long_to_bytes and bytes_to_long are adapted from PyCrypto, which is
# released into Public Domain
# https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Util/number.py#L387
def long_to_bytes(n, blocksize=0):
"""long_to_bytes(n:long, blocksize:int) : string
Convert a long integer to a byte string.
If optional blocksize is given and greater than zero, pad the front of the
byte string with binary zeros so that the length is a multiple of
blocksize.
"""
# after much testing, this algorithm was deemed to be the fastest
s = b''
n = int(n)
while n > 0:
s = struct.pack('>I', n & 0xffffffff) + s
n = n >> 32
# strip off leading zeros
for i in range(len(s)):
if s[i] != b'\000'[0]:
break
else:
# only happens when n == 0
s = b'\000'
i = 0
s = s[i:]
# add back some pad bytes. this could be done more efficiently w.r.t. the
# de-padding being done above, but sigh...
if blocksize > 0 and len(s) % blocksize:
s = (blocksize - len(s) % blocksize) * b'\000' + s
return s
def bytes_to_long(s):
"""bytes_to_long(string) : long
Convert a byte string to a long integer.
This is (essentially) the inverse of long_to_bytes().
"""
acc = 0
length = len(s)
if length % 4:
extra = (4 - length % 4)
s = b'\000' * extra + s
length = length + extra
for i in range(0, length, 4):
acc = (acc << 32) + struct.unpack('>I', s[i:i + 4])[0]
return acc
def ohdave_rsa_encrypt(data, exponent, modulus):
"""
Implement OHDave's RSA algorithm. See http://www.ohdave.com/rsa/