Add lab-02-security-as-a-system
This commit is contained in:
1 parent
dcf58ecf2a
commit
3213ab1435
10 files changed
+652
No files matched your search
+42
@@ -0,0 +1,42 @@
|
||||
#!/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()
|
||||
Reference in new issue
Block a user