forked from warner/foolscap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase32.py
35 lines (30 loc) · 909 Bytes
/
base32.py
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
30
31
32
33
34
35
# copied from the waterken.org Web-Calculus python implementation
def encode(bytes):
chars = ""
buffer = 0;
n = 0;
for b in bytes:
buffer = buffer << 8
buffer = buffer | ord(b)
n = n + 8
while n >= 5:
chars = chars + _encode((buffer >> (n - 5)) & 0x1F)
n = n - 5;
buffer = buffer & 0x1F # To quiet any warning from << operator
if n > 0:
buffer = buffer << (5 - n)
chars = chars + _encode(buffer & 0x1F)
return chars
def _encode(v):
if v < 26:
return chr(ord('a') + v)
else:
return chr(ord('2') + (v - 26))
# we use the rfc4648 base32 alphabet, in lowercase
BASE32_ALPHABET = "".join([_encode(i) for i in range(0x20)])
# 'abcdefghijklmnopqrstuvwxyz234567'
def is_base32(s):
for c in s.lower():
if c not in BASE32_ALPHABET:
return False
return True