Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Doc/library/binascii.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ The :mod:`!binascii` module defines the following functions:

.. versionadded:: 3.15

.. function:: a2b_base32(string, /, *, alphabet=BASE32_ALPHABET)

Convert base32 data back to binary and return the binary data.

Valid base32 data:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is incomplete and redundant. I think it is better to follow the example of ascii85 and base85 (with a reference to the RFC). Mention that the mapping is case-sensitive and no optional mapping of the digit "0" and "1" to letters "O", "I" or "l" is used.


* Conforms to :rfc:`4648`.
* Contains only characters from the base32 alphabet.
* Contains no excess data after padding (including excess padding, newlines, etc.).
* Does not start with padding.

Optional *alphabet* must be a :class:`bytes` object of length 32 which
specifies an alternative alphabet.

Invalid base32 data will raise :exc:`binascii.Error`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How invalid? ex: I think our implementation has always ignored excess bits in the final character that don't map to a byte. We should be explicit about this (and cover the behavior in a test). I think it is reasonable to change our behavior to conform strictly to the "MAY choose to" in https://datatracker.ietf.org/doc/html/rfc4648#section-3.5 and raise the Error when there are excess bits in the input.

We should also have base64's strict_mode=True do the same.

(I do not think we need strict_mode for a2b_base32, we're always being strict here)


.. versionadded:: next

.. function:: b2a_base32(data, /, *, alphabet=BASE32_ALPHABET)

Convert binary data to a line(s) of ASCII characters in base32 coding,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a single line.

I will add wrapcol in a separate issue.

as specified in :rfc:`4648`. The return value is the converted line.

Optional *alphabet* must be a :term:`bytes-like object` of length 32 which
specifies an alternative alphabet.

.. versionadded:: next

.. function:: a2b_qp(data, header=False)

Expand Down Expand Up @@ -327,6 +354,18 @@ The :mod:`!binascii` module defines the following functions:

.. versionadded:: next

.. data:: BASE32_ALPHABET

The base32 alphabet according to :rfc:`4648`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The base32 alphabet according to :rfc:`4648`.
The Base 32 alphabet according to :rfc:`4648`.


.. versionadded:: next

.. data:: BASE32HEX_ALPHABET

The "Extended Hex" base32hex alphabet according to :rfc:`4648`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The "Extended Hex" base32hex alphabet according to :rfc:`4648`.
The "Extended Hex" Base 32 alphabet according to :rfc:`4648`.

These are the names used in the table 3 and 4 captions in RFC 4648.

Oh, we can even refer directly to the table:

Suggested change
The "Extended Hex" base32hex alphabet according to :rfc:`4648`.
The "Extended Hex" Base 32 alphabet according to :rfc:`4648`, table 4.

Add this also for Base 64 alphabets if you choose this variant.

Copy link
Contributor Author

@kangtastic kangtastic Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if this would come up. RFC 4648 uses all four of the terms "Base 32", "Base32", "base 32", and "base32" to refer to this encoding at various points, but it also states e.g.:

This encoding may be referred to as "base32hex". This encoding should not be regarded as the same as the "base32" encoding and should not be referred to as only "base32".

and e.g.:

One property with this alphabet, which the base64 and base32 alphabets lack...

thus implying that "base32" and "base32hex" are preferred, even if the rest of the document doesn't adhere to the implication.

Anyway, I'll refer to it as "Base 32" in docs for now to fit what's already there, and not reference the table number or touch any Base64 stuff so as to keep the scope of this PR limited.


.. versionadded:: next


.. seealso::

Expand Down
85 changes: 9 additions & 76 deletions Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,51 +206,8 @@ def urlsafe_b64decode(s):
the letter O). For security purposes the default is None, so that
0 and 1 are not allowed in the input.
'''
_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
_b32hexalphabet = b'0123456789ABCDEFGHIJKLMNOPQRSTUV'
_b32tab2 = {}
_b32rev = {}

def _b32encode(alphabet, s):
# Delay the initialization of the table to not waste memory
# if the function is never called
if alphabet not in _b32tab2:
b32tab = [bytes((i,)) for i in alphabet]
_b32tab2[alphabet] = [a + b for a in b32tab for b in b32tab]
b32tab = None

if not isinstance(s, bytes_types):
s = memoryview(s).tobytes()
leftover = len(s) % 5
# Pad the last quantum with zero bits if necessary
if leftover:
s = s + b'\0' * (5 - leftover) # Don't use += !
encoded = bytearray()
from_bytes = int.from_bytes
b32tab2 = _b32tab2[alphabet]
for i in range(0, len(s), 5):
c = from_bytes(s[i: i + 5]) # big endian
encoded += (b32tab2[c >> 30] + # bits 1 - 10
b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
b32tab2[c & 0x3ff] # bits 31 - 40
)
# Adjust for any leftover partial quanta
if leftover == 1:
encoded[-6:] = b'======'
elif leftover == 2:
encoded[-4:] = b'===='
elif leftover == 3:
encoded[-3:] = b'==='
elif leftover == 4:
encoded[-1:] = b'='
return encoded.take_bytes()

def _b32decode(alphabet, s, casefold=False, map01=None):
# Delay the initialization of the table to not waste memory
# if the function is never called
if alphabet not in _b32rev:
_b32rev[alphabet] = {v: k for k, v in enumerate(alphabet)}

def _b32decode_prepare(s, casefold=False, map01=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to inline this function. map01 handling is only needed for standard alphabet, and the code for casefold is trivial.

s = _bytes_from_decode_data(s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only needed if map01 is not None.

if len(s) % 8:
raise binascii.Error('Incorrect padding')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this be handled in the C code?

Expand All @@ -263,51 +220,27 @@ def _b32decode(alphabet, s, casefold=False, map01=None):
s = s.translate(bytes.maketrans(b'01', b'O' + map01))
if casefold:
s = s.upper()
# Strip off pad characters from the right. We need to count the pad
# characters because this will tell us how many null bytes to remove from
# the end of the decoded string.
l = len(s)
s = s.rstrip(b'=')
padchars = l - len(s)
# Now decode the full quanta
decoded = bytearray()
b32rev = _b32rev[alphabet]
for i in range(0, len(s), 8):
quanta = s[i: i + 8]
acc = 0
try:
for c in quanta:
acc = (acc << 5) + b32rev[c]
except KeyError:
raise binascii.Error('Non-base32 digit found') from None
decoded += acc.to_bytes(5) # big endian
# Process the last, partial quanta
if l % 8 or padchars not in {0, 1, 3, 4, 6}:
raise binascii.Error('Incorrect padding')
if padchars and decoded:
acc <<= 5 * padchars
last = acc.to_bytes(5) # big endian
leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
decoded[-5:] = last[:leftover]
return decoded.take_bytes()
return s


def b32encode(s):
return _b32encode(_b32alphabet, s)
return binascii.b2a_base32(s)
b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32')

def b32decode(s, casefold=False, map01=None):
return _b32decode(_b32alphabet, s, casefold, map01)
s = _b32decode_prepare(s, casefold, map01)
return binascii.a2b_base32(s)
b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32',
extra_args=_B32_DECODE_MAP01_DOCSTRING)

def b32hexencode(s):
return _b32encode(_b32hexalphabet, s)
return binascii.b2a_base32(s, alphabet=binascii.BASE32HEX_ALPHABET)
b32hexencode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32hex')

def b32hexdecode(s, casefold=False):
# base32hex does not have the 01 mapping
return _b32decode(_b32hexalphabet, s, casefold)
s = _b32decode_prepare(s, casefold)
return binascii.a2b_base32(s, alphabet=binascii.BASE32HEX_ALPHABET)
b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex',
extra_args='')

Expand Down
180 changes: 178 additions & 2 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@


# Note: "*_hex" functions are aliases for "(un)hexlify"
b2a_functions = ['b2a_ascii85', 'b2a_base64', 'b2a_base85',
b2a_functions = ['b2a_ascii85', 'b2a_base32', 'b2a_base64', 'b2a_base85',
'b2a_hex', 'b2a_qp', 'b2a_uu',
'hexlify']
a2b_functions = ['a2b_ascii85', 'a2b_base64', 'a2b_base85',
a2b_functions = ['a2b_ascii85', 'a2b_base32', 'a2b_base64', 'a2b_base85',
'a2b_hex', 'a2b_qp', 'a2b_uu',
'unhexlify']
all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx']
Expand Down Expand Up @@ -670,6 +670,182 @@ def test_base85_alphabet(self):
with self.assertRaises(TypeError):
binascii.a2b_base64(data, alphabet=bytearray(alphabet))

def test_base32_valid(self):
# Test base32 with valid data
lines = []
step = 0
i = 0
while i < len(self.rawdata):
b = self.type2test(self.rawdata[i:i + step])
a = binascii.b2a_base32(b)
lines.append(a)
i += step
step += 1
res = bytes()
for line in lines:
a = self.type2test(line)
b = binascii.a2b_base32(a)
res += b
self.assertEqual(res, self.rawdata)

def test_base32_errors(self):
def _fixPadding(data):
fixed = data.replace(b"=", b"")
len_8 = len(fixed) % 8
p = 8 - len_8 if len_8 else 0
return fixed + b"=" * p

def _assertRegexTemplate(assert_regex, data, good_padding_result=None):
with self.assertRaisesRegex(binascii.Error, assert_regex):
binascii.a2b_base32(self.type2test(data))
if good_padding_result:
fixed = self.type2test(_fixPadding(data))
self.assertEqual(binascii.a2b_base32(fixed), good_padding_result)

def assertNonBase32Data(*args):
_assertRegexTemplate(r"(?i)Only base32 data", *args)

def assertExcessData(*args):
_assertRegexTemplate(r"(?i)Excess data", *args)

def assertExcessPadding(*args):
_assertRegexTemplate(r"(?i)Excess padding", *args)

def assertLeadingPadding(*args):
_assertRegexTemplate(r"(?i)Leading padding", *args)

def assertIncorrectPadding(*args):
_assertRegexTemplate(r"(?i)Incorrect padding", *args)

def assertDiscontinuousPadding(*args):
_assertRegexTemplate(r"(?i)Discontinuous padding", *args)

def assertInvalidLength(*args):
_assertRegexTemplate(r"(?i)Invalid.+number of data characters", *args)

assertNonBase32Data(b"a")
assertNonBase32Data(b"AA-")
assertNonBase32Data(b"ABCDE==!")
assertNonBase32Data(b"ab:(){:|:&};:==")

assertExcessData(b"AB======C")
assertExcessData(b"AB======CD")
assertExcessData(b"ABCD====E")
assertExcessData(b"ABCDE===FGH")
assertExcessData(b"ABCDEFG=H")
assertExcessData(b"432Z====55555555")

assertExcessData(b"BE======EF", b"\t\x08")
assertExcessData(b"BEEF====C", b"\t\x08Q")
assertExcessData(b"BEEFC===AK", b"\t\x08Q\x01")
assertExcessData(b"BEEFCAK=E", b"\t\x08Q\x01D")

assertExcessPadding(b"BE=======", b"\t")
assertExcessPadding(b"BE========", b"\t")
assertExcessPadding(b"BEEF=====", b"\t\x08")
assertExcessPadding(b"BEEF======", b"\t\x08")
assertExcessPadding(b"BEEFC====", b"\t\x08Q")
assertExcessPadding(b"BEEFC=====", b"\t\x08Q")
assertExcessPadding(b"BEEFCAK==", b"\t\x08Q\x01")
assertExcessPadding(b"BEEFCAK===", b"\t\x08Q\x01")
assertExcessPadding(b"BEEFCAKE=", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE==", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE===", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE====", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE=====", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE======", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE=======", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE========", b"\t\x08Q\x01D")
assertExcessPadding(b"BEEFCAKE=========", b"\t\x08Q\x01D")

assertLeadingPadding(b"=", b"")
assertLeadingPadding(b"==", b"")
assertLeadingPadding(b"===", b"")
assertLeadingPadding(b"====", b"")
assertLeadingPadding(b"=====", b"")
assertLeadingPadding(b"======", b"")
assertLeadingPadding(b"=======", b"")
assertLeadingPadding(b"========", b"")
assertLeadingPadding(b"=========", b"")
assertLeadingPadding(b"=BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"==BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"===BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"====BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"=====BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"======BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"=======BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"========BEEFCAKE", b"\t\x08Q\x01D")
assertLeadingPadding(b"=========BEEFCAKE", b"\t\x08Q\x01D")

assertIncorrectPadding(b"A")
assertIncorrectPadding(b"AB")
assertIncorrectPadding(b"ABC")
assertIncorrectPadding(b"ABCD")
assertIncorrectPadding(b"ABCDE")
assertIncorrectPadding(b"ABCDEF")
assertIncorrectPadding(b"ABCDEFG")

assertIncorrectPadding(b"BE=", b"\t")
assertIncorrectPadding(b"BE==", b"\t")
assertIncorrectPadding(b"BE===", b"\t")
assertIncorrectPadding(b"BE====", b"\t")
assertIncorrectPadding(b"BE=====", b"\t")
assertIncorrectPadding(b"BEEF=", b"\t\x08")
assertIncorrectPadding(b"BEEF==", b"\t\x08")
assertIncorrectPadding(b"BEEF===", b"\t\x08")
assertIncorrectPadding(b"BEEFC=", b"\t\x08Q")
assertIncorrectPadding(b"BEEFC==", b"\t\x08Q")

assertDiscontinuousPadding(b"BE=EF===", b"\t\x08")
assertDiscontinuousPadding(b"BE==EF==", b"\t\x08")
assertDiscontinuousPadding(b"BEEF=C==", b"\t\x08Q")
assertDiscontinuousPadding(b"BEEFC=AK", b"\t\x08Q\x01")

assertInvalidLength(b"A=")
assertInvalidLength(b"A==")
assertInvalidLength(b"A===")
assertInvalidLength(b"A====")
assertInvalidLength(b"A=====")
assertInvalidLength(b"A======")
assertInvalidLength(b"ABC=")
assertInvalidLength(b"ABC==")
assertInvalidLength(b"ABC===")
assertInvalidLength(b"ABC====")
assertInvalidLength(b"ABCDEF=")

assertInvalidLength(b"B=E=====", b"\t")
assertInvalidLength(b"B==E====", b"\t")
assertInvalidLength(b"BEE=F===", b"\t\x08")
assertInvalidLength(b"BEE==F==", b"\t\x08")
assertInvalidLength(b"BEEFCA=K", b"\t\x08Q\x01")
assertInvalidLength(b"BEEFCA=====K", b"\t\x08Q\x01")

def test_base32_alphabet(self):
alphabet = b'0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9JjKk'
data = self.type2test(self.rawdata)
encoded = binascii.b2a_base32(data, alphabet=alphabet)
trans = bytes.maketrans(binascii.BASE32_ALPHABET, alphabet)
expected = binascii.b2a_base32(data).translate(trans)
self.assertEqual(encoded, expected)
self.assertEqual(binascii.a2b_base32(encoded, alphabet=alphabet), self.rawdata)
self.assertEqual(binascii.b2a_base32(data, alphabet=self.type2test(alphabet)), expected)

data = self.type2test(b'')
self.assertEqual(binascii.b2a_base32(data, alphabet=alphabet), b'')
self.assertEqual(binascii.a2b_base32(data, alphabet=alphabet), b'')

for func in binascii.b2a_base32, binascii.a2b_base32:
with self.assertRaises(TypeError):
func(data, alphabet=None)
with self.assertRaises(TypeError):
func(data, alphabet=alphabet.decode())
with self.assertRaises(ValueError):
func(data, alphabet=alphabet[:-1])
with self.assertRaises(ValueError):
func(data, alphabet=alphabet+b'?')
with self.assertRaises(TypeError):
binascii.a2b_base32(data, alphabet=bytearray(alphabet))

def test_uu(self):
MAX_UU = 45
for backtick in (True, False):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add base32 support to :mod:`binascii` and improve the performance of the
base-32 converters in :mod:`base64`. Patch by James Seo.
Loading
Loading