Issue #17812: Fixed quadratic complexity of base64.b32encode().

This commit is contained in:
Serhiy Storchaka 2013-05-19 11:41:15 +03:00
parent 08231a9c6a
commit 2c3f2f19df
2 changed files with 8 additions and 6 deletions

View File

@ -166,7 +166,7 @@ def b32encode(s):
if leftover: if leftover:
s = s + bytes(5 - leftover) # Don't use += ! s = s + bytes(5 - leftover) # Don't use += !
quanta += 1 quanta += 1
encoded = bytes() encoded = bytearray()
for i in range(quanta): for i in range(quanta):
# c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
# code is to process the 40 bits in units of 5 bits. So we take the 1 # code is to process the 40 bits in units of 5 bits. So we take the 1
@ -187,14 +187,14 @@ def b32encode(s):
]) ])
# Adjust for any leftover partial quanta # Adjust for any leftover partial quanta
if leftover == 1: if leftover == 1:
return encoded[:-6] + b'======' encoded[-6:] = b'======'
elif leftover == 2: elif leftover == 2:
return encoded[:-4] + b'====' encoded[-4:] = b'===='
elif leftover == 3: elif leftover == 3:
return encoded[:-3] + b'===' encoded[-3:] = b'==='
elif leftover == 4: elif leftover == 4:
return encoded[:-1] + b'=' encoded[-1:] = b'='
return encoded return bytes(encoded)
def b32decode(s, casefold=False, map01=None): def b32decode(s, casefold=False, map01=None):

View File

@ -24,6 +24,8 @@ Core and Builtins
Library Library
------- -------
- Issue #17812: Fixed quadratic complexity of base64.b32encode().
- Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of - Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of
service using certificates with many wildcards (CVE-2013-2099). service using certificates with many wildcards (CVE-2013-2099).