-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase32
executable file
·57 lines (48 loc) · 1.32 KB
/
base32
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
import hashlib
import sys
def encode(string):
#token= hashlib.sha1(string).hexdigest()#[0:10]
token= string.lower()
num= int(token,16)
encode=[
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h',
'j','k',
'm','n',
'p','q','r','s','t',
'v','w','x','y','z']
o=''
i=0
while num:
v= num & 31
#if i and i % 4 == 0: o+= '-'
o= encode[v] + o
print(num)
print(o)
num=num>>5
i+=1
return o
def decode(string):
decode={
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14,
'f': 15, 'g': 16, 'h': 17, 'i': 1, 'j': 18,
'k': 19, 'l': 1, 'm': 20, 'n': 21, 'o': 0,
'p': 22, 'q': 23, 'r': 24, 's': 25, 't': 26,
'v': 27, 'w': 28, 'x': 29, 'y': 30, 'z': 31}
result=0
for c in string:#[::-1]:
print(result)
print(c)
if c=='-': continue
result = result << 5
result += decode[c.lower()]
decoded= "{0:x}".format(result)#.zfill(10)
return decoded
if sys.argv[1] == '-d':
print(decode(sys.argv[2]))
else:
if sys.argv[1] == '-e': sys.argv.pop(1)
print(encode(sys.argv[1]))