-
-
Notifications
You must be signed in to change notification settings - Fork 34.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
base: main
Are you sure you want to change the base?
Changes from all commits
f47f4bd
ce5bcd2
f59fffe
33437a5
044fa5a
3caf10d
c27319a
1abf3dd
4444158
133505d
File filter
Filter by extension
Conversations
Jump to
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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) | ||
|
Comment on lines
+155
to
+156
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. For clarity in the code, maybe have an _URLSAFE_BASE64_ALPHABET global variable?
Member
Author
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. Do you mean importing |
||
|
|
||
| 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.