Skip to content

Commit 2c3f2f1

Browse files
Issue #17812: Fixed quadratic complexity of base64.b32encode().
1 parent 08231a9 commit 2c3f2f1

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

Lib/base64.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def b32encode(s):
166166
if leftover:
167167
s = s + bytes(5 - leftover) # Don't use += !
168168
quanta += 1
169-
encoded = bytes()
169+
encoded = bytearray()
170170
for i in range(quanta):
171171
# c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
172172
# code is to process the 40 bits in units of 5 bits. So we take the 1
@@ -187,14 +187,14 @@ def b32encode(s):
187187
])
188188
# Adjust for any leftover partial quanta
189189
if leftover == 1:
190-
return encoded[:-6] + b'======'
190+
encoded[-6:] = b'======'
191191
elif leftover == 2:
192-
return encoded[:-4] + b'===='
192+
encoded[-4:] = b'===='
193193
elif leftover == 3:
194-
return encoded[:-3] + b'==='
194+
encoded[-3:] = b'==='
195195
elif leftover == 4:
196-
return encoded[:-1] + b'='
197-
return encoded
196+
encoded[-1:] = b'='
197+
return bytes(encoded)
198198

199199

200200
def b32decode(s, casefold=False, map01=None):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #17812: Fixed quadratic complexity of base64.b32encode().
28+
2729
- Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of
2830
service using certificates with many wildcards (CVE-2013-2099).
2931

0 commit comments

Comments
 (0)