initial commit. phase 1 complete

This commit is contained in:
2026-05-05 20:45:19 +02:00
parent d9c68313a0
commit 89e058ffac
20631 changed files with 3224610 additions and 43 deletions
+46
View File
@@ -0,0 +1,46 @@
import derive from './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 derive(normalizeDigest(digest), normalizeIkm(ikm), normalizeUint8Array(salt, 'salt'), normalizeInfo(info), normalizeKeylen(keylen, digest));
}
export { hkdf, hkdf as default };
+1
View File
@@ -0,0 +1 @@
{"type":"module","sideEffects":false}
+18
View File
@@ -0,0 +1,18 @@
const getGlobal = () => {
if (typeof globalThis !== 'undefined')
return globalThis;
if (typeof self !== 'undefined')
return self;
if (typeof window !== 'undefined')
return window;
throw new Error('unable to locate global object');
};
export default async (digest, ikm, salt, info, keylen) => {
const { crypto: { subtle }, } = getGlobal();
return new Uint8Array(await subtle.deriveBits({
name: 'HKDF',
hash: `SHA-${digest.substr(3)}`,
salt,
info,
}, await subtle.importKey('raw', ikm, 'HKDF', false, ['deriveBits']), keylen << 3));
};