|
10 | 10 | def rotate_secret_key(): |
11 | 11 | """ Automatically rotates secret key after 30 days """ |
12 | 12 | while True: |
13 | | - os.environ["HMAC_SECRET_KEY"] = secrets.token_hax(32) |
| 13 | + os.environ["HMAC_SECRET_KEY"] = secrets.token_hex(32) |
14 | 14 | time.sleep(30 * 24 * 60 * 60) |
15 | 15 | def get_secret_key(): |
16 | 16 | """ Gets the HMAC secret key """ |
17 | 17 | secret_key = os.getenv("HMAC_SECRET_KEY") |
18 | 18 | if secret_key is None: |
19 | | - raise RuntimeError("Secret key is missing!") |
20 | | - return secret_key.encode() |
| 19 | + try: |
| 20 | + with open("hmac_key.txt", "r") as f: |
| 21 | + secret_key = f.read().strip() |
| 22 | + except FileNotFoundError: |
| 23 | + raise RuntimeError("Secret key is missing! Set HMAC_SECRET_KEY or create hmac_key.txt.") |
| 24 | + return secret_key.encode() |
| 25 | + |
21 | 26 | def generate_hmac(data): |
22 | 27 | """Generating HMAC signature for integrity verification""" |
23 | | - return hmac.new(get_secret_key(), data.encode(),hashlib.sha256).haxdigit() |
| 28 | + return hmac.new(get_secret_key(), data.encode(),hashlib.sha256).hexdigit() |
24 | 29 | def serialize_graph(graph): |
25 | 30 | """Converts a graph into a string for HMAC signing.""" |
26 | 31 | if not graph.vertices or not graph.edge_weights: |
@@ -222,3 +227,5 @@ def num_edges(self): |
222 | 227 | """ |
223 | 228 | raise NotImplementedError( |
224 | 229 | "This is an abstract method.") |
| 230 | +threading.Thread(target=rotate_secret_key, daemon=True).start() |
| 231 | + |
0 commit comments