Skip to content

Instantly share code, notes, and snippets.

@jesserockz
Created March 17, 2024 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesserockz/276441f58892b7b425910bf9144cba39 to your computer and use it in GitHub Desktop.
Save jesserockz/276441f58892b7b425910bf9144cba39 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base35encode(number: int) -> str:
base35 = ""
while number > 1:
number, i = divmod(number, 35)
if i == 24:
i = 35
base35 = base35 + ALPHABET[i]
return base35
def base35decode(number: str) -> int:
decimal = 0
for i, s in enumerate(reversed(number)):
decimal += ALPHABET.index(s) * (35**i)
return decimal
def crc(data: str) -> int:
h = 0
for s in data:
h ^= (h << 5) + (h >> 2) + ord(s)
h &= 0xFFFF
return h
def _main():
data = sys.argv[1]
check = data[-4:-1]
data = data[:-5]
decimal = base35decode(check)
total = crc(data)
print(f"Check: \t{check}")
print(f"Decimal: \t{decimal}")
print()
print(f"CRC: \t\t{total}")
print(f"Base35: \t{base35encode(total)}")
print()
if __name__ == "__main__":
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment