kopia lustrzana https://github.com/micropython/micropython-lib
base64: Ported to MicroPython.
rodzic
a07073b41c
commit
44e2d86508
|
@ -33,7 +33,8 @@ def _bytes_from_decode_data(s):
|
||||||
if isinstance(s, str):
|
if isinstance(s, str):
|
||||||
try:
|
try:
|
||||||
return s.encode('ascii')
|
return s.encode('ascii')
|
||||||
except UnicodeEncodeError:
|
# except UnicodeEncodeError:
|
||||||
|
except:
|
||||||
raise ValueError('string argument should contain only ASCII characters')
|
raise ValueError('string argument should contain only ASCII characters')
|
||||||
elif isinstance(s, bytes_types):
|
elif isinstance(s, bytes_types):
|
||||||
return s
|
return s
|
||||||
|
@ -109,8 +110,8 @@ def standard_b64decode(s):
|
||||||
return b64decode(s)
|
return b64decode(s)
|
||||||
|
|
||||||
|
|
||||||
_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
|
#_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
|
||||||
_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
|
#_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
|
||||||
|
|
||||||
def urlsafe_b64encode(s):
|
def urlsafe_b64encode(s):
|
||||||
"""Encode a byte string using a url-safe Base64 alphabet.
|
"""Encode a byte string using a url-safe Base64 alphabet.
|
||||||
|
@ -119,7 +120,8 @@ def urlsafe_b64encode(s):
|
||||||
returned. The alphabet uses '-' instead of '+' and '_' instead of
|
returned. The alphabet uses '-' instead of '+' and '_' instead of
|
||||||
'/'.
|
'/'.
|
||||||
"""
|
"""
|
||||||
return b64encode(s).translate(_urlsafe_encode_translation)
|
# return b64encode(s).translate(_urlsafe_encode_translation)
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def urlsafe_b64decode(s):
|
def urlsafe_b64decode(s):
|
||||||
"""Decode a byte string encoded with the standard Base64 alphabet.
|
"""Decode a byte string encoded with the standard Base64 alphabet.
|
||||||
|
@ -131,9 +133,10 @@ def urlsafe_b64decode(s):
|
||||||
|
|
||||||
The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
||||||
"""
|
"""
|
||||||
s = _bytes_from_decode_data(s)
|
# s = _bytes_from_decode_data(s)
|
||||||
s = s.translate(_urlsafe_decode_translation)
|
# s = s.translate(_urlsafe_decode_translation)
|
||||||
return b64decode(s)
|
# return b64decode(s)
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -187,13 +190,13 @@ def b32encode(s):
|
||||||
])
|
])
|
||||||
# Adjust for any leftover partial quanta
|
# Adjust for any leftover partial quanta
|
||||||
if leftover == 1:
|
if leftover == 1:
|
||||||
encoded[-6:] = b'======'
|
encoded = encoded[:-6] + b'======'
|
||||||
elif leftover == 2:
|
elif leftover == 2:
|
||||||
encoded[-4:] = b'===='
|
encoded = encoded[:-4] + b'===='
|
||||||
elif leftover == 3:
|
elif leftover == 3:
|
||||||
encoded[-3:] = b'==='
|
encoded = encoded[:-3] + b'==='
|
||||||
elif leftover == 4:
|
elif leftover == 4:
|
||||||
encoded[-1:] = b'='
|
encoded = encoded[:-1] + b'='
|
||||||
return bytes(encoded)
|
return bytes(encoded)
|
||||||
|
|
||||||
|
|
||||||
|
@ -232,12 +235,13 @@ def b32decode(s, casefold=False, map01=None):
|
||||||
# Strip off pad characters from the right. We need to count the pad
|
# 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
|
# characters because this will tell us how many null bytes to remove from
|
||||||
# the end of the decoded string.
|
# the end of the decoded string.
|
||||||
padchars = 0
|
padchars = s.find(b'=')
|
||||||
mo = re.search(b'(?P<pad>[=]*)$', s)
|
|
||||||
if mo:
|
|
||||||
padchars = len(mo.group('pad'))
|
|
||||||
if padchars > 0:
|
if padchars > 0:
|
||||||
|
padchars = len(s) - padchars
|
||||||
s = s[:-padchars]
|
s = s[:-padchars]
|
||||||
|
else:
|
||||||
|
padchars = 0
|
||||||
|
|
||||||
# Now decode the full quanta
|
# Now decode the full quanta
|
||||||
parts = []
|
parts = []
|
||||||
acc = 0
|
acc = 0
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
import base64
|
||||||
|
|
||||||
|
b = base64.b64encode(b'zlutoucky kun upel dabelske ody')
|
||||||
|
print(b)
|
||||||
|
|
||||||
|
if b != b'emx1dG91Y2t5IGt1biB1cGVsIGRhYmVsc2tlIG9keQ==':
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
d = base64.b64decode(b)
|
||||||
|
print(d)
|
||||||
|
|
||||||
|
if d != b'zlutoucky kun upel dabelske ody':
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
base64.test()
|
||||||
|
|
||||||
|
binary = b'\x99\x10\xaa'
|
||||||
|
b = base64.b64encode(binary)
|
||||||
|
if b != b'mRCq':
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
d = base64.b64decode(b)
|
||||||
|
print(d)
|
||||||
|
if d != binary:
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
d = base64.b32encode(b'zlutoucky kun upel dabelske ody')
|
||||||
|
if d != b'PJWHK5DPOVRWW6JANN2W4IDVOBSWYIDEMFRGK3DTNNSSA33EPE======':
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
print(d)
|
||||||
|
b = base64.b32decode(d)
|
||||||
|
if b != b'zlutoucky kun upel dabelske ody':
|
||||||
|
raise Exception("Error")
|
||||||
|
|
||||||
|
print("OK")
|
Ładowanie…
Reference in New Issue