@eristack/checksum
Version:
SHA-256 hex normalize and constant-time compare for exports and file refs
45 lines (43 loc) • 1.15 kB
JavaScript
// src/core/checksum.ts
import { createHash, timingSafeEqual } from "crypto";
// src/core/errors.ts
var ChecksumParseError = class extends Error {
code = "CHECKSUM_PARSE_ERROR";
constructor(message) {
super(message);
this.name = "ChecksumParseError";
}
};
// src/core/checksum.ts
var HEX_RE = /^[0-9a-fA-F]+$/;
function sha256Hex(input) {
const hash = createHash("sha256");
if (typeof input === "string") {
hash.update(input, "utf8");
} else {
hash.update(input);
}
return hash.digest("hex");
}
function normalizeChecksumHex(value) {
const trimmed = value.trim().toLowerCase();
if (!trimmed || !HEX_RE.test(trimmed) || trimmed.length % 2 !== 0) {
throw new ChecksumParseError("Checksum must be even-length hex");
}
return trimmed;
}
function checksumEquals(a, b) {
const left = Buffer.from(normalizeChecksumHex(a), "hex");
const right = Buffer.from(normalizeChecksumHex(b), "hex");
if (left.length !== right.length) {
return false;
}
return timingSafeEqual(left, right);
}
export {
ChecksumParseError,
checksumEquals,
normalizeChecksumHex,
sha256Hex
};
//# sourceMappingURL=index.js.map