Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
0a8e4b1bf1308fa9a7d266f80c544c8207058707eae3ee6df6c9db0dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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`. | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. 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`. | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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. | ||
| ''' | ||
| 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'. | ||
| # 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() | ||
| return binascii.a2b_base32(s) | ||
| b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32', | ||
| extra_args=_B32_DECODE_MAP01_DOCSTRING) | ||
| def b32hexencode(s): | ||
| returnbinascii.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 | ||
| if casefold: | ||
| s = s.upper() | ||
| return binascii.a2b_base32(s, alphabet=binascii.BASE32HEX_ALPHABET) | ||
| b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex', | ||
| extra_args='') | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.