Neah/node_modules/@panva/hkdf/dist/node/cjs/index.js
2025-04-17 12:39:10 +02:00

51 lines
1.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = exports.hkdf = void 0;
const hkdf_js_1 = require("./runtime/hkdf.js");
function normalizeDigest(digest) {
switch (digest) {
case 'sha256':
case 'sha384':
case 'sha512':
case 'sha1':
return digest;
default:
throw new TypeError('unsupported "digest" value');
}
}
function normalizeUint8Array(input, label) {
if (typeof input === 'string')
return new TextEncoder().encode(input);
if (!(input instanceof Uint8Array))
throw new TypeError(`"${label}"" must be an instance of Uint8Array or a string`);
return input;
}
function normalizeIkm(input) {
const ikm = normalizeUint8Array(input, 'ikm');
if (!ikm.byteLength)
throw new TypeError(`"ikm" must be at least one byte in length`);
return ikm;
}
function normalizeInfo(input) {
const info = normalizeUint8Array(input, 'info');
if (info.byteLength > 1024) {
throw TypeError('"info" must not contain more than 1024 bytes');
}
return info;
}
function normalizeKeylen(input, digest) {
if (typeof input !== 'number' || !Number.isInteger(input) || input < 1) {
throw new TypeError('"keylen" must be a positive integer');
}
const hashlen = parseInt(digest.substr(3), 10) >> 3 || 20;
if (input > 255 * hashlen) {
throw new TypeError('"keylen" too large');
}
return input;
}
async function hkdf(digest, ikm, salt, info, keylen) {
return (0, hkdf_js_1.default)(normalizeDigest(digest), normalizeIkm(ikm), normalizeUint8Array(salt, 'salt'), normalizeInfo(info), normalizeKeylen(keylen, digest));
}
exports.hkdf = hkdf;
exports.default = hkdf;