-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashkey_verifier.py
More file actions
61 lines (48 loc) · 1.81 KB
/
hashkey_verifier.py
File metadata and controls
61 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import hashlib
import sys
from pathlib import Path
def load_verification_data(hash_data_path):
with open(hash_data_path, "r") as f:
return json.load(f)
def load_known_hash(hash_path):
with open(hash_path, "r") as f:
return f.read().strip()
def compute_hash(obj):
json_bytes = json.dumps(obj, sort_keys=True).encode("utf-8")
return hashlib.sha256(json_bytes).hexdigest()
def main():
if len(sys.argv) != 3:
print("Usage: python3 hashkey_verifier.py <folded_input.json> <known_hash.txt>")
sys.exit(1)
folded_input_path = Path(sys.argv[1])
known_hash_path = Path(sys.argv[2])
if not folded_input_path.exists() or not known_hash_path.exists():
print("Error: One or both files not found.")
sys.exit(1)
# Ask for UID to find matching .json with full metadata
uid = input("Enter user/device UID: ").strip()
safe_uid = "".join(c for c in uid if c.isalnum() or c in ('-', '_')).rstrip()
metadata_path = Path.home() / ".ai_core" / "hashkeys" / f"{safe_uid}.json"
if not metadata_path.exists():
print(f"[!] Metadata file not found: {metadata_path}")
sys.exit(1)
metadata = load_verification_data(metadata_path)
known_hash = load_known_hash(known_hash_path)
# Reconstruct full object from saved metadata
full_obj = {
"uid": metadata["uid"],
"seed": metadata["seed"],
"input": metadata["input"],
"timestamp": metadata["timestamp"]
}
# Compute and compare
generated_hash = compute_hash(full_obj)
if generated_hash == known_hash:
print("✅ Match: Hashes are identical.")
else:
print("❌ Mismatch: Hashes differ.")
print(f"Generated : {generated_hash}")
print(f"Expected : {known_hash}")
if __name__ == "__main__":
main()