-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathezbase32.py
executable file
·83 lines (72 loc) · 2.2 KB
/
ezbase32.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import random
# GEN {0x0223776d 0x04464ffa 0x088c3efd 0x110cf8f3 0x209dd5cf} F_mod=37 E_mod=[29, 21, 1] E_primitive=[8, 30] alphalog=247 alpha=[6, 14] c=750 minpolys=[x^2 + 7*x + 4, x^2 + 3*x + 14, x^2 + 5*x + 21]
def bch(c, d):
b = c >> 25
c = d ^ (c & 0x1FFFFFF) << 5
c ^= -((b >> 0) & 1) & 0x223776D
c ^= -((b >> 1) & 1) & 0x4464FFA
c ^= -((b >> 2) & 1) & 0x88C3EFD
c ^= -((b >> 3) & 1) & 0x110CF8F3
c ^= -((b >> 4) & 1) & 0x209DD5CF
return c
def convertbits(data, frombits, tobits, pad=True):
acc = 0
bits = 0
ret = []
maxv = (1 << tobits) - 1
for d in data:
if d < 0 or (d >> frombits):
return None
acc = (acc << frombits) | d
bits += frombits
while (bits >= tobits):
bits -= tobits
ret.append((acc >> bits) & maxv)
if (pad):
if (bits):
ret.append((acc << (tobits - bits)) & maxv)
elif (acc << (tobits - bits)) & maxv:
return None
return ret
ZBASE32 = "qyp9g8cazxr4jknhuvft3wed26s507ml"
INIT = 0x34056df6
def decode(s):
r = []
p = 0
chk = 1
for c in s:
if c == '-':
chk = bch(chk, p)
r.append(-1)
else:
f = ZBASE32.find(c)
if f == -1:
return None
chk = bch(chk, f)
r.append(f)
if c != INIT:
return None
def baseencode(data):
n = 0
chk = [len(data)]
for s in data:
print("data: %r" % ','.join("%i" % x for x in s))
n += len(s)
chk.append(n % 32)
print("meta: %r" % chk)
for s in data:
chk.extend(s)
print("all: %r" % chk)
checksum = bch(chk) ^ 0x0a8ce26f
print("chk: %r" % chk)
return ('-'.join([''.join([ZBASE32[x] for x in s]) for s in data])) + ''.join([ZBASE32[x] for x in convertbits([checksum], 30, 5)])
for j in [0,1,2,3]:
for i in [0,4,8,12,16,20,24,28]:
print(ZBASE32[i+j] + " ", end='')
print(" ")
if __name__ == '__main__':
random.seed()
ff = [random.randrange(0,256) for x in xrange(32)]
print("odata: %s" % ''.join("%02x" % x for x in ff))
print("res: %r\n" % baseencode([[0,1],[0] + convertbits(ff, 8, 5)]))