|
import os |
|
import hashlib |
|
import json |
|
import argparse |
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
def deterministic_hash(email, salt): |
|
return hashlib.pbkdf2_hmac("sha256", email.encode(), salt, 100000).hex() |
|
|
|
|
|
def main(args): |
|
|
|
load_dotenv() |
|
|
|
|
|
encryption_salt = os.getenv("EMAIL_ENCRYPTION_KEY").encode() |
|
|
|
|
|
with open(args.students_file, "r") as file: |
|
emails = json.load(file) |
|
|
|
|
|
hashed_emails = { |
|
deterministic_hash(email, encryption_salt): roles |
|
for email, roles in emails.items() |
|
} |
|
|
|
|
|
with open(args.encrypted_students_file, "w") as file: |
|
json.dump(hashed_emails, file) |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser( |
|
description="Encrypt student emails in a JSON file." |
|
) |
|
parser.add_argument( |
|
"--students-file", |
|
type=str, |
|
default="private/students.json", |
|
help="Path to the students JSON file", |
|
) |
|
parser.add_argument( |
|
"--encrypted-students-file", |
|
type=str, |
|
default="public/files/students_encrypted.json", |
|
help="Path to save the encrypted students JSON file", |
|
) |
|
args = parser.parse_args() |
|
|
|
main(args) |
|
|