@eristack/checksum
Version:
SHA-256 hex normalize and constant-time compare for exports and file refs
75 lines (71 loc) • 2.36 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
ChecksumParseError: () => ChecksumParseError,
checksumEquals: () => checksumEquals,
normalizeChecksumHex: () => normalizeChecksumHex,
sha256Hex: () => sha256Hex
});
module.exports = __toCommonJS(index_exports);
// src/core/checksum.ts
var import_node_crypto = require("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 = (0, import_node_crypto.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 (0, import_node_crypto.timingSafeEqual)(left, right);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ChecksumParseError,
checksumEquals,
normalizeChecksumHex,
sha256Hex
});
//# sourceMappingURL=index.cjs.map