@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
74 lines (73 loc) • 2.42 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
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 ssri_exports = {};
__export(ssri_exports, {
hexToSsri: () => hexToSsri,
isValidHex: () => isValidHex,
isValidSsri: () => isValidSsri,
parseSsri: () => parseSsri,
ssriToHex: () => ssriToHex
});
module.exports = __toCommonJS(ssri_exports);
// @__NO_SIDE_EFFECTS__
function ssriToHex(ssri) {
const match = /^([a-z0-9]+)-([A-Za-z0-9+/]+=*)$/i.exec(ssri);
if (!match || !match[2] || match[2].length < 2) {
throw new Error(`Invalid SSRI format: ${ssri}`);
}
const base64Hash = match[2];
const buffer = Buffer.from(base64Hash, "base64");
return buffer.toString("hex");
}
// @__NO_SIDE_EFFECTS__
function hexToSsri(hex, algorithm = "sha256") {
if (!/^[a-f0-9]+$/i.test(hex)) {
throw new Error(`Invalid hex format: ${hex}`);
}
const buffer = Buffer.from(hex, "hex");
const base64Hash = buffer.toString("base64");
return `${algorithm}-${base64Hash}`;
}
// @__NO_SIDE_EFFECTS__
function isValidSsri(value) {
return /^[a-z0-9]+-[A-Za-z0-9+/]{2,}=*$/i.test(value);
}
// @__NO_SIDE_EFFECTS__
function isValidHex(value) {
return /^[a-f0-9]+$/i.test(value);
}
// @__NO_SIDE_EFFECTS__
function parseSsri(ssri) {
const match = /^([a-z0-9]+)-([A-Za-z0-9+/]+=*)$/i.exec(ssri);
if (!match || !match[1] || !match[2] || match[2].length < 2) {
throw new Error(`Invalid SSRI format: ${ssri}`);
}
const algorithm = match[1];
const base64Hash = match[2];
return { algorithm, base64Hash };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
hexToSsri,
isValidHex,
isValidSsri,
parseSsri,
ssriToHex
});