Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-145980: Add support for alternative alphabets in the binascii module#145981
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f47f4bdce5bcd2f59fffe33437a5044fa5a3caf10dc27319a1abf3dd4444158133505df0f5960c609bbbFile 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -56,11 +56,13 @@ def b64encode(s, altchars=None, *, wrapcol=0): | ||
| If wrapcol is non-zero, insert a newline (b'\\n') character after at most | ||
| every wrapcol characters. | ||
| """ | ||
| encoded = binascii.b2a_base64(s, wrapcol=wrapcol, newline=False) | ||
| if altchars is not None: | ||
| assert len(altchars) == 2, repr(altchars) | ||
| return encoded.translate(bytes.maketrans(b'+/', altchars)) | ||
| return encoded | ||
| if len(altchars) != 2: | ||
| raise ValueError(f'invalid altchars: {altchars!r}') | ||
| alphabet = binascii.BASE64_ALPHABET[:-2] + altchars | ||
| return binascii.b2a_base64(s, wrapcol=wrapcol, newline=False, | ||
| alphabet=alphabet) | ||
| return binascii.b2a_base64(s, wrapcol=wrapcol, newline=False) | ||
| def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPECIFIED): | ||
| @@ -100,15 +102,10 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPE | ||
| break | ||
| s = s.translate(bytes.maketrans(altchars, b'+/')) | ||
| else: | ||
| trans_in = set(b'+/') - set(altchars) | ||
| if len(trans_in) == 2: | ||
| # we can't use the reqult of unordered sets here | ||
| trans = bytes.maketrans(altchars + b'+/', b'+/' + altchars) | ||
| else: | ||
| trans = bytes.maketrans(altchars + bytes(trans_in), | ||
| b'+/' + bytes(set(altchars) - set(b'+/'))) | ||
| s = s.translate(trans) | ||
| ignorechars = ignorechars.translate(trans) | ||
| alphabet = binascii.BASE64_ALPHABET[:-2] + altchars | ||
| return binascii.a2b_base64(s, strict_mode=validate, | ||
| alphabet=alphabet, | ||
| ignorechars=ignorechars) | ||
| if ignorechars is _NOT_SPECIFIED: | ||
| ignorechars = b'' | ||
| result = binascii.a2b_base64(s, strict_mode=validate, | ||
| @@ -146,7 +143,6 @@ def standard_b64decode(s): | ||
| return b64decode(s) | ||
| _urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_') | ||
| _urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/') | ||
| def urlsafe_b64encode(s): | ||
| @@ -156,7 +152,8 @@ def urlsafe_b64encode(s): | ||
| bytes object. The alphabet uses '-' instead of '+' and '_' instead of | ||
| '/'. | ||
| """ | ||
| return b64encode(s).translate(_urlsafe_encode_translation) | ||
| return binascii.b2a_base64(s, newline=False, | ||
| alphabet=binascii.URLSAFE_BASE64_ALPHABET) | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def urlsafe_b64decode(s): | ||
| """Decode bytes using the URL- and filesystem-safe Base64 alphabet. | ||
| @@ -399,14 +396,14 @@ def b85decode(b): | ||
| def z85encode(s, pad=False): | ||
| """Encode bytes-like object b in z85 format and return a bytes object.""" | ||
| return binascii.b2a_z85(s, pad=pad) | ||
| return binascii.b2a_base85(s, pad=pad, alphabet=binascii.Z85_ALPHABET) | ||
| def z85decode(s): | ||
| """Decode the z85-encoded bytes-like object or ASCII string b | ||
| The result is returned as a bytes object. | ||
| """ | ||
| return binascii.a2b_z85(s) | ||
| return binascii.a2b_base85(s, alphabet=binascii.Z85_ALPHABET) | ||
| # Legacy interface. This code could be cleaned up since I don't believe | ||
| # binascii has any line length limitations. It just doesn't seem worth it | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.