فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

feat(A03): synchronous SHA-256 canonical audit chain verification

davoodmehraban89 committed Oct 19, 2026 at 18:56 UTC 567b9af1759df9448e01b9982bfe6208bcff545f
1 file changed +42
src/core/AuditChain.js new
+42
@@ -0,0 +1,42 @@
1 +// Synchronous SHA-256 so hashing does not release an active IndexedDB transaction.
2 +// Hashes provide integrity checks, not authentication against a local attacker.
3 +const K = Array.from({length:64}, (_,i) => { let n=2,count=-1; while(true){let prime=true;for(let j=2;j*j<=n;j++)if(n%j===0){prime=false;break;}if(prime){count++;if(count===i)return Math.floor((Math.cbrt(n)%1)*4294967296)>>>0;}n++;}});
4 +const H0 = Array.from({length:8}, (_,i) => {let n=2,count=-1;while(true){let prime=true;for(let j=2;j*j<=n;j++)if(n%j===0){prime=false;break;}if(prime){count++;if(count===i)return Math.floor((Math.sqrt(n)%1)*4294967296)>>>0;}n++;}});
5 +const rotr=(x,n)=>(x>>>n)|(x<<(32-n));
6 +export function sha256(value) {
7 + const bytes=Array.from(new TextEncoder().encode(value));
8 + const bitLength=bytes.length*8;
9 + bytes.push(128);
10 + while(bytes.length%64!==56)bytes.push(0);
11 + const hi=Math.floor(bitLength/4294967296),lo=bitLength>>>0;
12 + for(const x of [hi,lo])for(let i=3;i>=0;i--)bytes.push((x>>>(i*8))&255);
13 + const h=H0.slice();
14 + for(let offset=0;offset<bytes.length;offset+=64){
15 + const w=new Array(64);
16 + for(let i=0;i<16;i++){const j=offset+i*4;w[i]=((bytes[j]<<24)|(bytes[j+1]<<16)|(bytes[j+2]<<8)|bytes[j+3])>>>0;}
17 + for(let i=16;i<64;i++){const a=w[i-15],c=w[i-2];w[i]=(w[i-16]+(rotr(a,7)^rotr(a,18)^(a>>>3))+w[i-7]+(rotr(c,17)^rotr(c,19)^(c>>>10)))>>>0;}
18 + let [a,c,d,e,f,g,j,k]=h;
19 + for(let i=0;i<64;i++){
20 + const t1=(k+(rotr(f,6)^rotr(f,11)^rotr(f,25))+((f&g)^(~f&j))+K[i]+w[i])>>>0;
21 + const t2=((rotr(a,2)^rotr(a,13)^rotr(a,22))+((a&c)^(a&d)^(c&d)))>>>0;
22 + k=j;j=g;g=f;f=(e+t1)>>>0;e=d;d=c;c=a;a=(t1+t2)>>>0;
23 + }
24 + const out=[a,c,d,e,f,g,j,k];for(let i=0;i<8;i++)h[i]=(h[i]+out[i])>>>0;
25 + }
26 + return h.map(x=>x.toString(16).padStart(8,'0')).join('');
27 +}
28 +export const canonical = value => JSON.stringify(sort(value));
29 +function sort(value){if(Array.isArray(value))return value.map(sort);if(value&&typeof value==='object')return Object.fromEntries(Object.keys(value).sort().map(k=>[k,sort(value[k])]));return value;}
30 +export const auditHash = row => sha256(canonical(Object.fromEntries(Object.entries(row).filter(([k])=>k!=='eventHash'))));
31 +export function verifyChain(rows, anchor = null) {
32 + const ordered=[...rows];
33 + let previous=null;
34 + for(let i=0;i<ordered.length;i++){
35 + const row=ordered[i];
36 + if(!row||row.prevHash!==previous||row.eventHash!==auditHash(row))return {valid:false,index:i,reason:'chain mismatch'};
37 + previous=row.eventHash;
38 + }
39 + if(anchor!==null&&(anchor.count!==ordered.length||anchor.headHash!==previous))
40 + return {valid:false,index:ordered.length,reason:'anchor mismatch'};
41 + return {valid:true,count:ordered.length,headHash:previous,anchored:anchor!==null};
42 +}