TextEncoder Fix

This commit is contained in:
Raven Scott
2026-04-26 22:34:04 -04:00
parent d286ce19b5
commit ef5a30fbef
@@ -2,9 +2,9 @@
* Tamper-evident in-session audit chain (hash-linked NDJSON rows; no persistence unless host appends).
*/
import bareCrypto from 'bare-crypto'
import b4a from 'b4a'
const { createHash } = bareCrypto
const textEncoder = new TextEncoder()
let nodeCreateHash = null
try {
const nodeCrypto = await import('crypto')
@@ -25,13 +25,6 @@ function hexU8(u8) {
return s
}
/**
* @param {string} s
*/
function utf8(s) {
return textEncoder.encode(String(s))
}
/**
* @param {string | null} prev
* @param {string} payload
@@ -47,9 +40,9 @@ function sha256LinkDigest(prev, payload) {
return d instanceof Uint8Array ? d : new Uint8Array(d)
}
const h = createHash('sha256')
if (prev) h.update(utf8(prev))
h.update(utf8('\n'))
h.update(utf8(payload))
if (prev) h.update(b4a.from(String(prev), 'utf8'))
h.update(b4a.from('\n', 'utf8'))
h.update(b4a.from(String(payload), 'utf8'))
const digest = h.digest()
return digest instanceof Uint8Array ? digest : new Uint8Array(digest)
}