Files

43 lines
757 B
Python
Executable File

#!/usr/bin/env python3
import sys
MODULUS = 1 << 24
FACTOR = 5
BIAS = 0x456789
FACTOR_INVERSE = pow(FACTOR, -1, MODULUS)
def pack(value):
number = int(value, 16)
result = (number * FACTOR + BIAS) % MODULUS
return f"{result:06x}"
def unpack(value):
number = int(value, 16)
result = ((number - BIAS) * FACTOR_INVERSE) % MODULUS
return f"{result:06x}"
def main():
if len(sys.argv) != 3:
raise SystemExit(2)
operation = sys.argv[1]
value = sys.argv[2].lower()
if len(value) != 6:
raise SystemExit(2)
if operation == "pack":
print(pack(value))
elif operation == "unpack":
print(unpack(value))
else:
raise SystemExit(2)
if __name__ == "__main__":
main()