diff --git a/scripts/fixtures/android/fetch.mjs b/scripts/fixtures/android/fetch.mjs index 00120e40954..2a8579faf14 100644 --- a/scripts/fixtures/android/fetch.mjs +++ b/scripts/fixtures/android/fetch.mjs @@ -1,5 +1,5 @@ +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import { createHash, randomUUID } from "node:crypto"; -import { createReadStream } from "node:fs"; import { link, mkdir, open, rm } from "node:fs/promises"; import { resolve, join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -16,11 +16,6 @@ const directory = resolve( ), ); await mkdir(directory, { recursive: true }); -const hashFile = async (path) => { - const hash = createHash("sha256"); - for await (const chunk of createReadStream(path)) hash.update(chunk); - return hash.digest("hex"); -}; for (const item of [ { url: JADX_RELEASE.url, @@ -31,7 +26,7 @@ for (const item of [ ]) { const path = join(directory, item.filename); try { - const digest = await hashFile(path); + const digest = await sha256File(path); if (digest !== item.sha256) throw new Error( `Existing ${path} has a different SHA-256; it was not overwritten`, diff --git a/scripts/fixtures/harmony/fetch.mjs b/scripts/fixtures/harmony/fetch.mjs index efc752185de..fb6e9c62ddf 100644 --- a/scripts/fixtures/harmony/fetch.mjs +++ b/scripts/fixtures/harmony/fetch.mjs @@ -1,5 +1,5 @@ +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import { createHash, randomUUID } from "node:crypto"; -import { createReadStream } from "node:fs"; import { link, mkdir, open, rm } from "node:fs/promises"; import { resolve, join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -15,14 +15,9 @@ const directory = resolve( ), ); await mkdir(directory, { recursive: true }); -const hashFile = async (path) => { - const hash = createHash("sha256"); - for await (const chunk of createReadStream(path)) hash.update(chunk); - return hash.digest("hex"); -}; const path = join(directory, fixture.filename); try { - const digest = await hashFile(path); + const digest = await sha256File(path); if (digest !== fixture.sha256) throw new Error( `Existing ${path} has a different SHA-256; it was not overwritten`, diff --git a/scripts/verify/android/digest.mjs b/scripts/lib/verifier-file-sha256.mjs similarity index 57% rename from scripts/verify/android/digest.mjs rename to scripts/lib/verifier-file-sha256.mjs index e4ed4d0eef4..409317bc257 100644 --- a/scripts/verify/android/digest.mjs +++ b/scripts/lib/verifier-file-sha256.mjs @@ -1,8 +1,11 @@ import { createHash } from "node:crypto"; import { createReadStream } from "node:fs"; -/** Verify artifact identity without depending on private runtime snapshot helpers. */ -export const hashAndroidFile = async (path) => { +/** + * Stream a verifier input into a lowercase hexadecimal SHA-256 digest. + * @param {import("node:fs").PathLike} path + */ +export const sha256File = async (path) => { const hash = createHash("sha256"); for await (const bytes of createReadStream(path)) hash.update(bytes); return hash.digest("hex"); diff --git a/scripts/verify-real-ida.mjs b/scripts/verify-real-ida.mjs index 9b5647c08ec..14c3af1c1a8 100644 --- a/scripts/verify-real-ida.mjs +++ b/scripts/verify-real-ida.mjs @@ -1,4 +1,5 @@ #!/usr/bin/env node +import { sha256File } from "./lib/verifier-file-sha256.mjs"; import { assertFeatureTrace, findMappedNonFunction, @@ -12,7 +13,6 @@ import { import { withVerifierResourceOwner } from "./lib/verifier-file-resources.mjs"; import assert from "node:assert/strict"; -import { createHash } from "node:crypto"; import { createReadStream } from "node:fs"; import { access, readdir, writeFile } from "node:fs/promises"; import { basename, dirname, isAbsolute, join, resolve } from "node:path"; @@ -133,11 +133,6 @@ let phase = "target admission"; const workspaces = new Set(); let targetIsDatabase = false; const databases = new Set(); -const digest = async () => { - const hash = createHash("sha256"); - for await (const chunk of createReadStream(target)) hash.update(chunk); - return hash.digest("hex"); -}; const observe = (input) => { const evidence = evidenceSchema.parse(input); assert.equal(evidence.provider.id, "ida"); @@ -547,7 +542,7 @@ const cleanup = async () => { await upstream.connect(); if (config.mode === "attached") { const metadata = await upstream.readResource("ida://idb/metadata"); - assert.equal(metadata.sha256.toLowerCase(), await digest()); + assert.equal(metadata.sha256.toLowerCase(), await sha256File(target)); report.checks.push( "existing GUI target remains open with the original input identity", ); @@ -583,13 +578,13 @@ const cleanup = async () => { } }; try { - const before = await digest(); + const before = await sha256File(target); for await (const head of createReadStream(target, { end: 3 })) targetIsDatabase = hasIdaDatabaseSignature(head); const siblings = (await readdir(dirname(target))).sort(); await mcp(await cli()); await cleanup(); - assert.equal(await digest(), before); + assert.equal(await sha256File(target), before); // Engines write databases beside their input; only the private copy may get one. assert.deepEqual((await readdir(dirname(target))).sort(), siblings); report.checks.push( diff --git a/scripts/verify/android/analysis.mjs b/scripts/verify/android/analysis.mjs index 37b9017a620..7363ed52d09 100644 --- a/scripts/verify/android/analysis.mjs +++ b/scripts/verify/android/analysis.mjs @@ -8,7 +8,7 @@ import { promisify } from "node:util"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { parseEvidence } from "../../../dist/domain/evidence.js"; -import { hashAndroidFile } from "./digest.mjs"; +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import { JADX_RELEASE } from "../../../dist/android/JadxRelease.js"; import fixture from "../../fixtures/android/apidemos.json" with { type: "json" }; import { verifyAndroidCliCancellation } from "./cli-cancellation.mjs"; @@ -31,12 +31,12 @@ if (apk === undefined || jar === undefined) await access(apk); await access(jar); assert.equal( - await hashAndroidFile(apk), + await sha256File(apk), fixture.sha256, "Real Android lane requires the fixed ApiDemos release fixture", ); assert.equal( - await hashAndroidFile(jar), + await sha256File(jar), JADX_RELEASE.sha256, "Real Android lane requires the audited headless JADX release JAR", ); @@ -312,7 +312,7 @@ await verifyAndroidFailureRecovery({ fixture, }); assert.equal( - await hashAndroidFile(apk), + await sha256File(apk), fixture.sha256, "Original APK must remain unchanged", ); diff --git a/scripts/verify/android/direct-invokes.mjs b/scripts/verify/android/direct-invokes.mjs index 5fbc00331f6..3bf4168ed46 100644 --- a/scripts/verify/android/direct-invokes.mjs +++ b/scripts/verify/android/direct-invokes.mjs @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { parseEvidence } from "../../../dist/domain/evidence.js"; -import { hashAndroidFile } from "./digest.mjs"; +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; /** The public result keeps one using node and exposes its two raw DEX invokes. */ export const verifyAndroidDirectInvokes = async ({ @@ -13,7 +13,7 @@ export const verifyAndroidDirectInvokes = async ({ environment, execute, }) => { - const digest = await hashAndroidFile(apk); + const digest = await sha256File(apk); const check = (value) => { const evidence = parseEvidence(value); assert.equal(evidence.subject.identity.sha256, digest); @@ -102,7 +102,7 @@ export const verifyAndroidDirectInvokes = async ({ await client.close(); } assert.equal( - await hashAndroidFile(apk), + await sha256File(apk), digest, "Static inspection changed fixture bytes", ); diff --git a/scripts/verify/android/external-invokes.mjs b/scripts/verify/android/external-invokes.mjs index c8888db4aad..ef13af63208 100644 --- a/scripts/verify/android/external-invokes.mjs +++ b/scripts/verify/android/external-invokes.mjs @@ -10,7 +10,7 @@ import { requireMcpOutput, requireMcpToolError, } from "../../lib/mcp-verifier-results.mjs"; -import { hashAndroidFile } from "./digest.mjs"; +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; const operation = "trace_android_references"; const className = "android.app.NotificationManager"; @@ -94,7 +94,7 @@ const cli = async (options, path, index, selector = input(path, index)) => { }; const verifyApk = async (options, path, dexEntry) => { - const digest = await hashAndroidFile(path); + const digest = await sha256File(path); const cliResults = []; for (const index of [0, 1]) cliResults.push( @@ -172,7 +172,7 @@ const verifyApk = async (options, path, dexEntry) => { assert.deepEqual(replay.normalized_result, cliResults[0].normalized_result); }, [() => client.close(), () => transport.close()]); assert.equal( - await hashAndroidFile(path), + await sha256File(path), digest, "Static references changed APK bytes", ); @@ -187,7 +187,7 @@ const verifyLimit = async ( stage = "caller_inventory", selector, ) => { - const digest = await hashAndroidFile(path); + const digest = await sha256File(path); try { await cli(options, path, 0, selector); assert.fail("Oversized external reference inventory was accepted"); @@ -198,7 +198,7 @@ const verifyLimit = async ( assert.equal(failure.details.reported_limits.stage, stage); assert.equal(failure.retryable, false); } - assert.equal(await hashAndroidFile(path), digest); + assert.equal(await sha256File(path), digest); console.log( `PASS real external ${stage} representation refusal retains typed resource constraint and immutable APK`, ); @@ -247,7 +247,7 @@ const checkSiteBudget = (value, digest) => { /** Optional instruction positions cannot displace the complete caller inventory. */ const verifySiteBudget = async (options, path, control) => { - const digest = await hashAndroidFile(path); + const digest = await sha256File(path); const first = checkSiteBudget(await cli(options, path, 0), digest); const transport = new StdioClientTransport({ command: process.execPath, @@ -298,7 +298,7 @@ const verifySiteBudget = async (options, path, control) => { ); check( requireMcpOutput(await call(input(control, 0)), operation), - await hashAndroidFile(control), + await sha256File(control), 0, "classes.dex", ); @@ -308,7 +308,7 @@ const verifySiteBudget = async (options, path, control) => { ); assert.deepEqual(replay.normalized_result, first.normalized_result); }, [() => client.close(), () => transport.close()]); - assert.equal(await hashAndroidFile(path), digest); + assert.equal(await sha256File(path), digest); console.log( "PASS site-heavy CLI/MCP complete11 callers, partial positions, bounded output, target replacement/recovery and awaited cleanup", ); diff --git a/scripts/verify/android/method-bodies.mjs b/scripts/verify/android/method-bodies.mjs index dedb68553ce..fee27aa10bd 100644 --- a/scripts/verify/android/method-bodies.mjs +++ b/scripts/verify/android/method-bodies.mjs @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { parseEvidence } from "../../../dist/domain/evidence.js"; -import { hashAndroidFile } from "./digest.mjs"; +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; const initializers = [ { @@ -98,7 +98,7 @@ export const verifyAndroidMethodBodies = async ({ environment, execute, }) => { - const digest = await hashAndroidFile(apk); + const digest = await sha256File(apk); const check = (value, scenario) => { const evidence = parseEvidence(value); assert.equal(evidence.subject.identity.sha256, digest); @@ -198,7 +198,7 @@ export const verifyAndroidMethodBodies = async ({ await client.close(); } assert.equal( - await hashAndroidFile(apk), + await sha256File(apk), digest, "Static inspection changed fixture bytes", ); diff --git a/scripts/verify/harmony/analysis.mjs b/scripts/verify/harmony/analysis.mjs index ddb017c96f3..79661c750fd 100644 --- a/scripts/verify/harmony/analysis.mjs +++ b/scripts/verify/harmony/analysis.mjs @@ -1,8 +1,7 @@ +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import { requireMcpOutput } from "../../lib/mcp-verifier-results.mjs"; import assert from "node:assert/strict"; import { execFile } from "node:child_process"; -import { createHash } from "node:crypto"; -import { createReadStream } from "node:fs"; import { access, writeFile } from "node:fs/promises"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -26,13 +25,8 @@ if (hap === undefined) "verify:harmony requires REA_HARMONY_TEST_HAP. Run npm run fixtures:harmony and set it to the downloaded file; see docs/guides/harmony/harmony-analysis.md.", ); await access(hap); -const hashFile = async (path) => { - const hash = createHash("sha256"); - for await (const chunk of createReadStream(path)) hash.update(chunk); - return hash.digest("hex"); -}; assert.equal( - await hashFile(hap), + await sha256File(hap), fixture.sha256, "Real HarmonyOS lane requires the pinned Harmony pipeline release fixture", ); @@ -216,7 +210,7 @@ await withVerifierFixture(workspaceFixture, async () => { assert.notEqual(empty.isError, true, JSON.stringify(empty)); assert.equal(empty.structuredContent?.result?.open, false); assert.equal( - await hashFile(path), + await sha256File(path), fixture.sha256, "MCP workflow must preserve source bytes", ); diff --git a/scripts/verify/javascript/native-handoffs.mjs b/scripts/verify/javascript/native-handoffs.mjs index 74d102acb20..7c3d3a5896d 100644 --- a/scripts/verify/javascript/native-handoffs.mjs +++ b/scripts/verify/javascript/native-handoffs.mjs @@ -1,8 +1,8 @@ #!/usr/bin/env node +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import assert from "node:assert/strict"; import { createHash, randomUUID } from "node:crypto"; -import { createReadStream } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; import { isAbsolute, join } from "node:path"; import { performance } from "node:perf_hooks"; @@ -104,11 +104,6 @@ const client = new Client({ const serialize = (value) => JSON.stringify(value); const digest = (value) => createHash("sha256").update(serialize(value)).digest("hex"); -const fileDigest = async (path) => { - const hash = createHash("sha256"); - for await (const chunk of createReadStream(path)) hash.update(chunk); - return hash.digest("hex"); -}; const simulatedNativeEvidence = (path, sha256) => createEvidence( { path, sha256, format: "raw-binary" }, @@ -151,11 +146,11 @@ const ownedCommand = async (command, arguments_, timeoutMs = 60_000) => { } }; await withVerifierCleanup(async () => { - const archiveSha256 = await fileDigest(values.artifact); + const archiveSha256 = await sha256File(values.artifact); const companion = join(`${values.artifact}.unpacked`, values["addon-path"]); let companionSha256 = null; try { - companionSha256 = await fileDigest(companion); + companionSha256 = await sha256File(companion); } catch (cause) { if (cause?.code !== "ENOENT") throw cause; } diff --git a/scripts/verify/javascript/output.mjs b/scripts/verify/javascript/output.mjs index 7b6b89b72e0..b95dd3cdab5 100644 --- a/scripts/verify/javascript/output.mjs +++ b/scripts/verify/javascript/output.mjs @@ -1,3 +1,4 @@ +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import assert from "node:assert/strict"; import { constants } from "node:buffer"; import { createHash } from "node:crypto"; @@ -157,9 +158,7 @@ await withVerifierFixture(fixture, async () => { assert.equal(output.error, undefined); assert.equal(unexpectedOutputBytes, 0); - const actualHash = createHash("sha256"); - for await (const chunk of createReadStream(path)) actualHash.update(chunk); - const digest = actualHash.digest("hex"); + const digest = await sha256File(path); assert.equal(digest, expectedHash.digest("hex")); assert.equal((await stat(path)).size, expectedBytes); let verifiedTokenCount; diff --git a/scripts/verify/javascript/retained-facts.mjs b/scripts/verify/javascript/retained-facts.mjs index 4be34617463..90e77a71019 100644 --- a/scripts/verify/javascript/retained-facts.mjs +++ b/scripts/verify/javascript/retained-facts.mjs @@ -1,8 +1,8 @@ #!/usr/bin/env node +import { sha256File } from "../../lib/verifier-file-sha256.mjs"; import assert from "node:assert/strict"; import { createHash, randomUUID } from "node:crypto"; -import { createReadStream } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; import { isAbsolute, join } from "node:path"; import { performance } from "node:perf_hooks"; @@ -111,10 +111,7 @@ const resourceSnapshot = async () => { await withVerifierCleanup(async () => { const start = performance.now(); - const artifactHash = createHash("sha256"); - for await (const chunk of createReadStream(values.artifact)) - artifactHash.update(chunk); - const artifactSha256 = artifactHash.digest("hex"); + const artifactSha256 = await sha256File(values.artifact); temporary = await VerifierFixture.open({ prefix: "rea-retained-facts-verifier-", });