"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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);
var integrity_exports = {};
__export(integrity_exports, {
  DlxHashMismatchError: () => DlxHashMismatchError,
  computeHashes: () => computeHashes,
  isChecksumString: () => isChecksumString,
  isIntegrityString: () => isIntegrityString,
  normalizeHash: () => normalizeHash,
  verifyHash: () => verifyHash
});
module.exports = __toCommonJS(integrity_exports);
var import_node_crypto = require("node:crypto");
var import_hash = require("./crypto/hash");
var import_buffer = require("./primordials/buffer");
var import_error = require("./primordials/error");
var import_string = require("./primordials/string");
const INTEGRITY_PREFIX = "sha512-";
const INTEGRITY_BODY_RE = /^[A-Za-z0-9+/=]+$/;
const CHECKSUM_RE = /^[a-f0-9]{64}$/i;
function computeHashes(bytes) {
  const integrity = `sha512-${(0, import_hash.hash)("sha512", bytes, "base64")}`;
  const checksum = (0, import_hash.hash)("sha256", bytes, "hex");
  return { integrity, checksum };
}
function isChecksumString(s) {
  return CHECKSUM_RE.test(s);
}
function isIntegrityString(s) {
  if (!(0, import_string.StringPrototypeStartsWith)(s, INTEGRITY_PREFIX)) {
    return false;
  }
  const body = (0, import_string.StringPrototypeSlice)(s, INTEGRITY_PREFIX.length);
  return body.length > 0 && INTEGRITY_BODY_RE.test(body);
}
function normalizeHash(spec) {
  if (typeof spec === "object" && spec !== null) {
    if (spec.type === "integrity") {
      if (!isIntegrityString(spec.value)) {
        throw new import_error.TypeErrorCtor(
          `Expected SRI integrity string "sha512-<base64>", got: ${spec.value}`
        );
      }
      return { type: "integrity", value: spec.value };
    }
    if (spec.type === "checksum") {
      if (!isChecksumString(spec.value)) {
        throw new import_error.TypeErrorCtor(
          `Expected sha256 hex string (64 hex chars), got: ${spec.value}`
        );
      }
      return { type: "checksum", value: spec.value };
    }
    throw new import_error.TypeErrorCtor(
      `Unknown hash type: ${spec.type}`
    );
  }
  if (typeof spec !== "string") {
    throw new import_error.TypeErrorCtor(
      `HashSpec must be a string or { type, value } object, got: ${typeof spec}`
    );
  }
  if (isIntegrityString(spec)) {
    return { type: "integrity", value: spec };
  }
  if (isChecksumString(spec)) {
    return { type: "checksum", value: spec };
  }
  throw new import_error.TypeErrorCtor(
    `Unrecognized hash format. Expected SRI integrity ("sha512-<base64>") or sha256 hex (64 hex chars), got: ${spec}`
  );
}
function verifyHash(expected, computed) {
  const actual = expected.type === "integrity" ? computed.integrity : computed.checksum;
  const expectedBuf = (0, import_buffer.BufferFrom)(expected.value);
  const actualBuf = (0, import_buffer.BufferFrom)(actual);
  if (expectedBuf.length !== actualBuf.length || !(0, import_node_crypto.timingSafeEqual)(expectedBuf, actualBuf)) {
    throw new DlxHashMismatchError(expected, computed);
  }
}
class DlxHashMismatchError extends Error {
  expected;
  actual;
  constructor(expected, actual) {
    const actualValue = expected.type === "integrity" ? actual.integrity : actual.checksum;
    super(
      `Hash mismatch (${expected.type}): expected ${expected.value}, got ${actualValue}`
    );
    this.name = "DlxHashMismatchError";
    this.expected = expected;
    this.actual = actual;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DlxHashMismatchError,
  computeHashes,
  isChecksumString,
  isIntegrityString,
  normalizeHash,
  verifyHash
});
