@dzhechkov/keysarium-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ # Witness Chain Protocol — Tamper-Evident Artifact Integrity
2
+
3
+ > SHA-256 hash-chain for verifying that pipeline artifacts have not been modified between stages.
4
+
5
+ ## Overview
6
+
7
+ This protocol defines how to compute SHA-256 hashes of pipeline artifacts and link them into a chain. Each artifact's hash includes the previous artifact's hash, making the chain tamper-evident: modifying any artifact breaks all downstream hashes.
8
+
9
+ ## Constants
10
+
11
+ ```
12
+ NULL_HASH = "0000000000000000000000000000000000000000000000000000000000000000"
13
+ CHAIN_FILE = ".witness-chain.json"
14
+ HASH_PREFIX = "sha256:"
15
+ ```
16
+
17
+ ## Hash Computation
18
+
19
+ ### Platform Detection
20
+
21
+ ```bash
22
+ if command -v sha256sum &>/dev/null; then
23
+ SHA_CMD="sha256sum"
24
+ elif command -v shasum &>/dev/null; then
25
+ SHA_CMD="shasum -a 256"
26
+ else
27
+ echo "ERROR: No SHA-256 command found."
28
+ exit 1
29
+ fi
30
+ ```
31
+
32
+ ### Computing a File Hash
33
+
34
+ ```bash
35
+ HASH=$(${SHA_CMD} "path/to/file.md" | awk '{print $1}')
36
+ ```
37
+
38
+ ### Computing a Chained Hash
39
+
40
+ ```bash
41
+ FILE_CONTENT=$(cat "path/to/file.md")
42
+ PREV_HASH="<previous hash value>"
43
+ CHAINED_HASH=$(printf '%s%s' "${FILE_CONTENT}" "${PREV_HASH}" | ${SHA_CMD} | awk '{print $1}')
44
+ ```
45
+
46
+ **Important:** Use `printf '%s%s'` (not `echo`) to avoid trailing newline issues.
47
+
48
+ ### Content Normalization
49
+
50
+ Hash the raw file content as stored on disk. Do NOT trim whitespace, normalize line endings, or strip BOM markers. The hash must match the exact bytes.
51
+
52
+ ## Chain Operations
53
+
54
+ ### Create Genesis Record
55
+
56
+ Called at the first stage to initialize the chain.
57
+
58
+ **Preconditions:**
59
+ - Project directory exists
60
+ - First stage artifact has been created
61
+ - No `.witness-chain.json` exists yet
62
+
63
+ **Procedure:**
64
+ 1. Compute hash of first artifact with NULL_HASH as previous
65
+ 2. Create `.witness-chain.json` with a single chain record
66
+
67
+ ### Append Record
68
+
69
+ Called at each subsequent stage checkpoint.
70
+
71
+ **Preconditions:**
72
+ - `.witness-chain.json` exists
73
+ - New artifact file has been created
74
+ - Previous stage's record exists in the chain
75
+
76
+ **Procedure:**
77
+ 1. Read chain, extract last record's hash
78
+ 2. Compute new chained hash: `SHA-256(new_content + last_hash)`
79
+ 3. Append record to chain array
80
+ 4. Update `last_updated` timestamp
81
+
82
+ ### Verify Chain
83
+
84
+ Walk the entire chain and verify each hash.
85
+
86
+ **Algorithm:**
87
+ ```
88
+ 1. Load .witness-chain.json
89
+ 2. For each record (index i):
90
+ a. Read the artifact file
91
+ b. Get previous_hash (NULL_HASH for i==0, else chain[i-1].hash)
92
+ c. Compute expected = SHA-256(file_content + previous_hash)
93
+ d. Compare expected with stored hash
94
+ e. If mismatch: record broken link
95
+ 3. Produce verification report
96
+ ```
97
+
98
+ ## Chain File Schema
99
+
100
+ ```json
101
+ {
102
+ "project_slug": "string — project directory name",
103
+ "created_at": "ISO-8601",
104
+ "last_updated": "ISO-8601",
105
+ "chain": [
106
+ {
107
+ "sequence": 0,
108
+ "stage": "string — stage identifier",
109
+ "artifact": "string — filename relative to project dir",
110
+ "hash": "sha256:<64 hex chars>",
111
+ "previous_hash": "sha256:<64 hex chars or null hash>",
112
+ "timestamp": "ISO-8601",
113
+ "promise_tag": "string — semantic completion promise"
114
+ }
115
+ ]
116
+ }
117
+ ```
118
+
119
+ ## Chain Repair
120
+
121
+ If an artifact is legitimately modified (e.g., after human feedback at checkpoint):
122
+
123
+ 1. Re-hash the modified artifact using the previous record's hash
124
+ 2. Update the modified record's hash
125
+ 3. Re-hash ALL subsequent records (cascade update)
126
+ 4. Update `last_updated` timestamp
127
+
128
+ This is expected behavior. The witness chain protects against *undetected* modifications.
129
+
130
+ ## Platform Compatibility
131
+
132
+ | Platform | Command | Notes |
133
+ |----------|---------|-------|
134
+ | Linux | `sha256sum` | Default in coreutils |
135
+ | macOS | `shasum -a 256` | Default |
136
+ | Windows (WSL/Git Bash) | `sha256sum` | Available |
137
+
138
+ Attempt `sha256sum` first, fall back to `shasum -a 256`.