Last active
March 23, 2024 23:21
-
-
Save minitech/610850ea303d8835d67df1a0e3377749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import hmac | |
import json | |
import struct | |
import sys | |
def main(): | |
if len(sys.argv) != 2: | |
print(f"Usage: python3 {sys.argv[0]} COURSES.jsonl\n", file=sys.stderr) | |
raise SystemExit(1) | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
course = json.loads(line) | |
print(f"ID: {get_course_code(course['id'])}") | |
print(f"Course Name: {course['course_name']}") | |
print(f"Creator Name: {course['creator']['nnid']}") | |
print() | |
def get_course_code(idno: int) -> str: | |
key = hashlib.md5(b"9f2b4678").digest() | |
data = struct.pack("<Q", idno) | |
checksum = hmac.HMAC(key, data, "md5").digest() | |
checksum = checksum[3:1:-1].hex().upper() | |
return f"{checksum}0000{idno:08X}" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment