-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-146192: Add base32 support to binascii #146193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0a8e4b1
bf1308f
a9a7d26
6f80c54
4c82070
58707ea
e3ee6df
6c9db0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,6 +183,38 @@ 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 contains characters from the base32 alphabet specified | ||
| in :rfc:`4648` in groups of eight (if necessary, the final group is padded | ||
| to eight characters with ``=``). Each group encodes 40 bits of binary data | ||
| in the range from ``0`` to ``2 ** 40 - 1``, inclusive. | ||
|
|
||
| .. note:: | ||
| This function does not map lowercase characters (which are invalid in | ||
| standard base32) to their uppercase counterparts, nor does it | ||
| contextually map ``0`` to ``O`` and ``1`` to ``I``/``L`` as :rfc:`4648` | ||
| allows. | ||
|
|
||
| Optional *alphabet* must be a :class:`bytes` object of length 32 which | ||
| specifies an alternative alphabet. | ||
|
|
||
| Invalid base32 data will raise :exc:`binascii.Error`. | ||
|
|
||
| .. versionadded:: next | ||
|
|
||
| .. function:: b2a_base32(data, /, *, alphabet=BASE32_ALPHABET) | ||
|
|
||
| Convert binary data to a line of ASCII characters in base32 coding, | ||
| 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) | ||
|
|
||
| Convert a block of quoted-printable data back to binary and return the binary | ||
|
|
@@ -327,6 +359,20 @@ The :mod:`!binascii` module defines the following functions: | |
|
|
||
| .. versionadded:: next | ||
|
|
||
| .. data:: BASE32_ALPHABET | ||
|
|
||
| The Base 32 alphabet according to :rfc:`4648`. | ||
|
|
||
| .. versionadded:: next | ||
|
|
||
| .. data:: BASE32HEX_ALPHABET | ||
|
|
||
| The "Extended Hex" Base 32 alphabet according to :rfc:`4648`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mention that this one maintains sort order when encoded. |
||
| Data encoded with this alphabet maintains its sort order during bitwise | ||
| comparisons. | ||
|
|
||
| .. versionadded:: next | ||
|
|
||
|
|
||
| .. seealso:: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,54 +206,13 @@ 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 b32encode(s): | ||
| return binascii.b2a_base32(s) | ||
| b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32') | ||
|
|
||
| def b32decode(s, casefold=False, map01=None): | ||
| s = _bytes_from_decode_data(s) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only needed if map01 is not None.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction: it is also needed if casefold is true, for input like 'ß' or 'ffi'. |
||
| if len(s) % 8: | ||
| raise binascii.Error('Incorrect padding') | ||
| # Handle section 2.4 zero and one mapping. The flag map01 will be either | ||
| # False, or the character to map the digit 1 (one) to. It should be | ||
| # either L (el) or I (eye). | ||
|
|
@@ -263,51 +222,20 @@ 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() | ||
|
|
||
|
|
||
| def b32encode(s): | ||
| return _b32encode(_b32alphabet, s) | ||
| b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32') | ||
|
|
||
| def b32decode(s, casefold=False, map01=None): | ||
| return _b32decode(_b32alphabet, 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): | ||
| s = _bytes_from_decode_data(s) | ||
| # base32hex does not have the 01 mapping | ||
| return _b32decode(_b32hexalphabet, s, casefold) | ||
| if casefold: | ||
| s = s.upper() | ||
| return binascii.a2b_base32(s, alphabet=binascii.BASE32HEX_ALPHABET) | ||
| b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex', | ||
| extra_args='') | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a separate issue. This PR does not change the behavior of existing decoder.
There are more important issues than checking for canonicity of input. They should be fixed first.