-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.py
More file actions
29 lines (27 loc) · 749 Bytes
/
base64.py
File metadata and controls
29 lines (27 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
alphabet = b'ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz0123456789+/'
print(alphabet)
src = 'abcd'
def base64encode(src:str):
ret = bytearray()
if isinstance(src, str):
_src = src.encode()
else:
return
length = len(_src)
for offset in range(0, length, 3):
triple = _src[offset:offset+3]
r = 3 - len(triple)
if r:
triple += b'\x00' * r
b = int.from_bytes(triple, 'big')
for i in range(18, -1, -6):
index = b >> i if i == 18 else b >> i & 0x3F
ret.append(alphabet[index])
if r:
ret[-r:] = b'=' * r
return bytes(ret)
strlist = ['a', 'b']
for x in strlist:
print(x)
print(base64encode(x))
print()