-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasher.py
More file actions
111 lines (88 loc) · 3.02 KB
/
hasher.py
File metadata and controls
111 lines (88 loc) · 3.02 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
HashGuardian - Text Integrity Checker
Detects whether text has been modified by comparing cryptographic hashes.
"""
import hashlib
import json
import os
import datetime
from pathlib import Path
ALGORITHMS = ["md5", "sha1", "sha256", "sha512"]
VAULT_FILE = "hash_vault.json"
def compute_hash(text: str, algorithm: str = "sha256") -> str:
"""Compute hash of given text using specified algorithm."""
if algorithm not in ALGORITHMS:
raise ValueError(f"Unsupported algorithm. Choose from: {ALGORITHMS}")
h = hashlib.new(algorithm)
h.update(text.encode("utf-8"))
return h.hexdigest()
def compute_all_hashes(text: str) -> dict:
"""Compute hashes using all supported algorithms."""
return {algo: compute_hash(text, algo) for algo in ALGORITHMS}
def save_hash(label: str, text: str, algorithm: str = "sha256") -> dict:
"""Save a hash snapshot to the vault."""
vault = load_vault()
entry = {
"label": label,
"algorithm": algorithm,
"hash": compute_hash(text, algorithm),
"length": len(text),
"timestamp": datetime.datetime.now().isoformat(),
}
vault[label] = entry
save_vault(vault)
return entry
def verify_text(label: str, text: str) -> dict:
"""Verify if text matches the saved hash snapshot."""
vault = load_vault()
if label not in vault:
return {"status": "NOT_FOUND", "message": f"No snapshot found for label '{label}'"}
entry = vault[label]
current_hash = compute_hash(text, entry["algorithm"])
original_hash = entry["hash"]
is_intact = current_hash == original_hash
return {
"status": "INTACT" if is_intact else "MODIFIED",
"label": label,
"algorithm": entry["algorithm"],
"original_hash": original_hash,
"current_hash": current_hash,
"original_length": entry["length"],
"current_length": len(text),
"saved_at": entry["timestamp"],
"is_intact": is_intact,
}
def compare_texts(text1: str, text2: str, algorithm: str = "sha256") -> dict:
"""Compare two texts directly without saving."""
hash1 = compute_hash(text1, algorithm)
hash2 = compute_hash(text2, algorithm)
return {
"algorithm": algorithm,
"hash_1": hash1,
"hash_2": hash2,
"identical": hash1 == hash2,
"length_1": len(text1),
"length_2": len(text2),
}
def load_vault() -> dict:
"""Load the hash vault from disk."""
if Path(VAULT_FILE).exists():
with open(VAULT_FILE, "r") as f:
return json.load(f)
return {}
def save_vault(vault: dict):
"""Save the hash vault to disk."""
with open(VAULT_FILE, "w") as f:
json.dump(vault, f, indent=2)
def list_snapshots() -> list:
"""List all saved snapshots."""
vault = load_vault()
return list(vault.values())
def delete_snapshot(label: str) -> bool:
"""Delete a snapshot from the vault."""
vault = load_vault()
if label in vault:
del vault[label]
save_vault(vault)
return True
return False