add user database code
This commit is contained in:
parent
f7b7918660
commit
15770f2879
13 changed files with 319 additions and 234 deletions
0
roc_fnb/util/__init__.py
Normal file
0
roc_fnb/util/__init__.py
Normal file
30
roc_fnb/util/base64.py
Normal file
30
roc_fnb/util/base64.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from base64 import b64encode
|
||||
from binascii import a2b_base64
|
||||
import binascii
|
||||
|
||||
|
||||
def base64_decode(string: str|bytes) -> bytes:
|
||||
"""
|
||||
Python's base64.b64_decode requires padding to be correct. This does not.
|
||||
|
||||
What silly bullshit.
|
||||
"""
|
||||
encoded: bytes = string.encode() if isinstance(string, str) else string
|
||||
# padc = 4 - len(encoded) % 4
|
||||
# if padc == 4:
|
||||
# padc = 0
|
||||
# fixed = encoded + b'=' * padc
|
||||
fixed = encoded + b'===='
|
||||
try:
|
||||
return a2b_base64(fixed, strict_mode=False)
|
||||
except binascii.Error as e:
|
||||
print(f'{string=!r}\n{len(string)=!r}\n{padc=!r}\n{fixed=!r}\n{len(fixed)=!r}')
|
||||
raise e
|
||||
|
||||
def base64_encode(data: bytes) -> str:
|
||||
"""
|
||||
Return a base64 encoded string with no padding
|
||||
|
||||
Python's b64encode returns bytes. Why?
|
||||
"""
|
||||
return b64encode(data).decode('utf-8').rstrip('=')
|
||||
11
roc_fnb/util/test_base64.py
Normal file
11
roc_fnb/util/test_base64.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from random import randbytes
|
||||
|
||||
from roc_fnb.util.base64 import *
|
||||
|
||||
|
||||
def test_encode_and_decode():
|
||||
for size in range(20-40):
|
||||
data = randbytes(32)
|
||||
string = base64_encode(data)
|
||||
decoded = base64_decode(string)
|
||||
assert data == decoded
|
||||
Loading…
Add table
Add a link
Reference in a new issue