1
0
mirror of https://github.com/osmarks/random-stuff synced 2024-11-09 13:59:55 +00:00
random-stuff/code-guessing/base32.py

22 lines
586 B
Python
Raw Normal View History

2021-04-26 16:59:23 +00:00
import string, re
def entry(argv):
argv = argv.encode("utf-8")
modulus = len(argv) % 5
if modulus > 0:
argv += b"\x00" * (5-modulus)
chars = string.ascii_uppercase + "234567"
historyLen = 1
b = 0
for c in reversed(argv):
b += c * historyLen
historyLen *= 256
data = []
while b != 0:
data.append(chars[b % 32])
b //= 32
data = "".join(reversed(data))
checksum = { 0: 0, 1: 6, 2: 4, 3: 3, 4: 1 }
return re.sub("A{%d}$" % checksum[modulus], lambda str: str.group(0).replace("A", "="), data)