Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

1.8 KB
Raw Blame
import type { AsarHeaderAdmission } from "../config/asarHeaderAdmission.js";
import { createHash } from "node:crypto";
import type { Readable } from "node:stream";

import { ArtifactReaderFailure } from "./ArtifactReader.js";
import { streamChunkToBuffer } from "./StreamBytes.js";
import {
  createArtifactClassificationProbe,
  type ArtifactClassificationEvidence,
} from "./ArtifactClassificationProbe.js";

/** Full stream digest and byte count with bounded classification evidence. */
export type HashResult = ArtifactClassificationEvidence & {
  readonly sha256: string;
  readonly bytes: number;
  readonly md5?: string;
};

/** Preserve the inventory cancellation reason at a stream boundary. */
export const abortIfNeeded = (signal?: AbortSignal): void => {
  if (signal?.aborted === true)
    throw new ArtifactReaderFailure(
      "cancelled",
      "Artifact inventory cancelled",
      { cause: signal.reason },
    );
};

/** Hash every stream byte and retain its bounded classification prefix. */
export const hashReadable = async (
  stream: Readable,
  signal?: AbortSignal,
  integrityAlgorithm?: "sha256" | "md5",
  asarHeaderAdmission?: AsarHeaderAdmission,
): Promise<HashResult> => {
  const hash = createHash("sha256");
  const md5 = integrityAlgorithm === "md5" ? createHash("md5") : undefined;
  const probe = createArtifactClassificationProbe(asarHeaderAdmission);
  let bytes = 0;
  for await (const raw of stream) {
    abortIfNeeded(signal);
    const chunk = streamChunkToBuffer(raw);
    bytes += chunk.length;
    hash.update(chunk);
    md5?.update(chunk);
    probe.write(chunk);
  }
  abortIfNeeded(signal);
  return {
    sha256: hash.digest("hex"),
    ...(md5 === undefined ? {} : { md5: md5.digest("hex") }),
    bytes,
    ...probe.finish(bytes),
  };
};