File size: 2,346 Bytes
497c79e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import hashlib
from datetime import datetime

# === Configuration ===
CANONICAL_FILES = [
    "belel_fingerprint.py",
    "identity_guard.py",
    "BELEL_AUTHORITY_PROOF.txt",
    "belel_propagation.py",
    "canonical_config.json",
    "resurrector.py"
]

FINGERPRINT_INDEX = "belel_fingerprint_index.json"
DIFF_LOG = "canonical_diff_log.json"


def calculate_sha1(filepath):
    sha1 = hashlib.sha1()
    with open(filepath, "rb") as f:
        while chunk := f.read(8192):
            sha1.update(chunk)
    return sha1.hexdigest()


def load_fingerprint_index():
    if not os.path.exists(FINGERPRINT_INDEX):
        raise FileNotFoundError(f"Fingerprint index '{FINGERPRINT_INDEX}' not found.")
    with open(FINGERPRINT_INDEX, "r") as f:
        return json.load(f)


def compare_fingerprints(fingerprint_index):
    diffs = []
    for file in CANONICAL_FILES:
        if not os.path.exists(file):
            diffs.append({
                "file": file,
                "status": "MISSING"
            })
            continue

        current_hash = calculate_sha1(file)
        baseline_hash = fingerprint_index.get(file)

        if current_hash != baseline_hash:
            diffs.append({
                "file": file,
                "status": "MUTATED",
                "current_hash": current_hash,
                "baseline_hash": baseline_hash
            })

    return diffs


def write_diff_log(diffs):
    timestamp = datetime.utcnow().isoformat() + "Z"
    log = {
        "timestamp": timestamp,
        "diffs": diffs
    }

    with open(DIFF_LOG, "w") as f:
        json.dump(log, f, indent=4)

    print(f"[πŸ”] Diff log written to {DIFF_LOG}")


def main():
    print("πŸ”’ Verifying Canonical Integrity...")
    try:
        fingerprint_index = load_fingerprint_index()
        diffs = compare_fingerprints(fingerprint_index)

        if diffs:
            print("[⚠️] Canonical drift detected:")
            for diff in diffs:
                print(f"  - {diff['file']}: {diff['status']}")
            write_diff_log(diffs)
            exit(1)
        else:
            print("[βœ…] All canonical files verified successfully.")
            exit(0)

    except Exception as e:
        print(f"[❌] Error during diff check: {str(e)}")
        exit(2)


if __name__ == "__main__":
    main()