generated from erangel1/generic-template
initial commit. phase 1 complete
This commit is contained in:
+46
@@ -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 };
|
||||
Reference in New Issue
Block a user