@volter/tabnode 0.5.21 → 0.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -23705,6 +23705,83 @@ function createCryptoModule(require2) {
|
|
|
23705
23705
|
const keyInfo = extractKeyInfo(key);
|
|
23706
23706
|
return new KeyObject2("private", keyInfo.keyData, keyInfo.algorithm);
|
|
23707
23707
|
}
|
|
23708
|
+
const NAMED_CURVES = {
|
|
23709
|
+
"prime256v1": "P-256",
|
|
23710
|
+
"p-256": "P-256",
|
|
23711
|
+
"secp256r1": "P-256",
|
|
23712
|
+
"secp384r1": "P-384",
|
|
23713
|
+
"p-384": "P-384",
|
|
23714
|
+
"secp521r1": "P-521",
|
|
23715
|
+
"p-521": "P-521"
|
|
23716
|
+
};
|
|
23717
|
+
function keyPairAlgorithm(type2, options) {
|
|
23718
|
+
const exponent = (value) => {
|
|
23719
|
+
let rest = value ?? 65537;
|
|
23720
|
+
const bytes2 = [];
|
|
23721
|
+
while (rest > 0) {
|
|
23722
|
+
bytes2.unshift(rest & 255);
|
|
23723
|
+
rest = Math.floor(rest / 256);
|
|
23724
|
+
}
|
|
23725
|
+
return new Uint8Array(bytes2);
|
|
23726
|
+
};
|
|
23727
|
+
switch (type2) {
|
|
23728
|
+
case "rsa":
|
|
23729
|
+
case "rsa-pss": {
|
|
23730
|
+
if (!Number.isInteger(options.modulusLength)) throw Object.assign(new TypeError('The "options.modulusLength" property must be of type number.'), { code: "ERR_INVALID_ARG_TYPE" });
|
|
23731
|
+
const name = type2 === "rsa" ? "RSASSA-PKCS1-v1_5" : "RSA-PSS";
|
|
23732
|
+
return { generate: { name, modulusLength: options.modulusLength, publicExponent: exponent(options.publicExponent), hash: "SHA-256" }, usages: ["sign", "verify"], name: type2 === "rsa" ? "RSA-SHA256" : "RSA-PSS" };
|
|
23733
|
+
}
|
|
23734
|
+
case "ec": {
|
|
23735
|
+
const curve = NAMED_CURVES[String(options.namedCurve ?? "").toLowerCase()];
|
|
23736
|
+
if (!curve) throw Object.assign(new TypeError(`Invalid EC curve name ${String(options.namedCurve)}`), { code: "ERR_CRYPTO_INVALID_CURVE" });
|
|
23737
|
+
return { generate: { name: "ECDSA", namedCurve: curve }, usages: ["sign", "verify"], name: curve === "P-256" ? "ES256" : curve === "P-384" ? "ES384" : "ES512" };
|
|
23738
|
+
}
|
|
23739
|
+
case "ed25519":
|
|
23740
|
+
return { generate: { name: "Ed25519" }, usages: ["sign", "verify"], name: "Ed25519" };
|
|
23741
|
+
case "x25519":
|
|
23742
|
+
return { generate: { name: "X25519" }, usages: ["deriveBits"], name: "X25519" };
|
|
23743
|
+
default:
|
|
23744
|
+
throw Object.assign(new TypeError(`The argument 'type' must be a supported key type. Received '${type2}'`), { code: "ERR_INVALID_ARG_VALUE" });
|
|
23745
|
+
}
|
|
23746
|
+
}
|
|
23747
|
+
function encodedKey(der, encoding, kind) {
|
|
23748
|
+
const expected = kind === "public" ? "spki" : "pkcs8";
|
|
23749
|
+
if (encoding.type !== void 0 && encoding.type !== expected) {
|
|
23750
|
+
unsupported2(`Encoding a ${kind} key as ${encoding.type}`);
|
|
23751
|
+
}
|
|
23752
|
+
const bytes2 = Buffer2.from(der);
|
|
23753
|
+
if (encoding.format === "der") return bytes2;
|
|
23754
|
+
if (encoding.format !== "pem") unsupported2(`Encoding a key in the ${String(encoding.format)} format`);
|
|
23755
|
+
const label = kind === "public" ? "PUBLIC KEY" : "PRIVATE KEY";
|
|
23756
|
+
const body = bytes2.toString("base64").match(/.{1,64}/g).join("\n");
|
|
23757
|
+
return `-----BEGIN ${label}-----
|
|
23758
|
+
${body}
|
|
23759
|
+
-----END ${label}-----
|
|
23760
|
+
`;
|
|
23761
|
+
}
|
|
23762
|
+
async function generateKeyPairAsync(type2, options) {
|
|
23763
|
+
const algorithm = keyPairAlgorithm(type2, options);
|
|
23764
|
+
const pair = await crypto.subtle.generateKey(algorithm.generate, true, algorithm.usages);
|
|
23765
|
+
const publicKey = options.publicKeyEncoding ? encodedKey(await crypto.subtle.exportKey("spki", pair.publicKey), options.publicKeyEncoding, "public") : new KeyObject2("public", pair.publicKey, algorithm.name);
|
|
23766
|
+
const privateKey = options.privateKeyEncoding ? encodedKey(await crypto.subtle.exportKey("pkcs8", pair.privateKey), options.privateKeyEncoding, "private") : new KeyObject2("private", pair.privateKey, algorithm.name);
|
|
23767
|
+
return { publicKey, privateKey };
|
|
23768
|
+
}
|
|
23769
|
+
function generateKeyPair2(type2, options, callback) {
|
|
23770
|
+
if (typeof options === "function") {
|
|
23771
|
+
callback = options;
|
|
23772
|
+
options = {};
|
|
23773
|
+
}
|
|
23774
|
+
if (typeof callback !== "function") throw Object.assign(new TypeError('The "callback" argument must be of type function.'), { code: "ERR_INVALID_ARG_TYPE" });
|
|
23775
|
+
const done = callback;
|
|
23776
|
+
keyPairAlgorithm(type2, options ?? {});
|
|
23777
|
+
generateKeyPairAsync(type2, options ?? {}).then(
|
|
23778
|
+
({ publicKey, privateKey }) => done(null, publicKey, privateKey),
|
|
23779
|
+
(error) => done(error instanceof Error ? error : new Error(String(error)))
|
|
23780
|
+
);
|
|
23781
|
+
}
|
|
23782
|
+
Object.defineProperty(generateKeyPair2, Symbol.for("nodejs.util.promisify.custom"), {
|
|
23783
|
+
value: (type2, options) => generateKeyPairAsync(type2, options ?? {})
|
|
23784
|
+
});
|
|
23708
23785
|
function timingSafeEqual2(a, b) {
|
|
23709
23786
|
if (a.length !== b.length) {
|
|
23710
23787
|
return false;
|
|
@@ -23861,11 +23938,12 @@ function createCryptoModule(require2) {
|
|
|
23861
23938
|
KeyObject: KeyObject2,
|
|
23862
23939
|
createSecretKey: createSecretKey2,
|
|
23863
23940
|
createPublicKey: createPublicKey2,
|
|
23864
|
-
createPrivateKey: createPrivateKey2
|
|
23941
|
+
createPrivateKey: createPrivateKey2,
|
|
23942
|
+
generateKeyPair: generateKeyPair2
|
|
23865
23943
|
};
|
|
23866
23944
|
}
|
|
23867
23945
|
const cryptoModule = createCryptoModule();
|
|
23868
|
-
const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
23946
|
+
const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, generateKeyPair, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
23869
23947
|
var cryptoShim = /* @__PURE__ */ Object.freeze({
|
|
23870
23948
|
__proto__: null,
|
|
23871
23949
|
KeyObject,
|
|
@@ -23879,6 +23957,7 @@ var cryptoShim = /* @__PURE__ */ Object.freeze({
|
|
|
23879
23957
|
createSign,
|
|
23880
23958
|
createVerify,
|
|
23881
23959
|
default: cryptoModule,
|
|
23960
|
+
generateKeyPair,
|
|
23882
23961
|
getCiphers,
|
|
23883
23962
|
getHashes,
|
|
23884
23963
|
getRandomValues,
|
package/dist/index.mjs
CHANGED
|
@@ -21322,10 +21322,17 @@ var implementation$1 = function bind2(that) {
|
|
|
21322
21322
|
var implementation = implementation$1;
|
|
21323
21323
|
var functionBind = Function.prototype.bind || implementation;
|
|
21324
21324
|
var functionCall = Function.prototype.call;
|
|
21325
|
-
var functionApply
|
|
21325
|
+
var functionApply;
|
|
21326
|
+
var hasRequiredFunctionApply;
|
|
21327
|
+
function requireFunctionApply() {
|
|
21328
|
+
if (hasRequiredFunctionApply) return functionApply;
|
|
21329
|
+
hasRequiredFunctionApply = 1;
|
|
21330
|
+
functionApply = Function.prototype.apply;
|
|
21331
|
+
return functionApply;
|
|
21332
|
+
}
|
|
21326
21333
|
var reflectApply = typeof Reflect !== "undefined" && Reflect && Reflect.apply;
|
|
21327
21334
|
var bind$2 = functionBind;
|
|
21328
|
-
var $apply$1 =
|
|
21335
|
+
var $apply$1 = requireFunctionApply();
|
|
21329
21336
|
var $call$2 = functionCall;
|
|
21330
21337
|
var $reflectApply = reflectApply;
|
|
21331
21338
|
var actualApply = $reflectApply || bind$2.call($call$2, $apply$1);
|
|
@@ -21445,7 +21452,7 @@ var hasSymbols = requireHasSymbols()();
|
|
|
21445
21452
|
var getProto = requireGetProto();
|
|
21446
21453
|
var $ObjectGPO = requireObject_getPrototypeOf();
|
|
21447
21454
|
var $ReflectGPO = requireReflect_getPrototypeOf();
|
|
21448
|
-
var $apply =
|
|
21455
|
+
var $apply = requireFunctionApply();
|
|
21449
21456
|
var $call = functionCall;
|
|
21450
21457
|
var needsEval = {};
|
|
21451
21458
|
var TypedArray = typeof Uint8Array === "undefined" || !getProto ? undefined$1 : getProto(Uint8Array);
|
|
@@ -22086,7 +22093,7 @@ function requireApplyBind() {
|
|
|
22086
22093
|
if (hasRequiredApplyBind) return applyBind;
|
|
22087
22094
|
hasRequiredApplyBind = 1;
|
|
22088
22095
|
var bind3 = functionBind;
|
|
22089
|
-
var $apply2 =
|
|
22096
|
+
var $apply2 = requireFunctionApply();
|
|
22090
22097
|
var actualApply$1 = actualApply;
|
|
22091
22098
|
applyBind = function applyBind2() {
|
|
22092
22099
|
return actualApply$1(bind3, $apply2, arguments);
|
|
@@ -23874,6 +23881,83 @@ function createCryptoModule(require2) {
|
|
|
23874
23881
|
const keyInfo = extractKeyInfo(key);
|
|
23875
23882
|
return new KeyObject2("private", keyInfo.keyData, keyInfo.algorithm);
|
|
23876
23883
|
}
|
|
23884
|
+
const NAMED_CURVES = {
|
|
23885
|
+
"prime256v1": "P-256",
|
|
23886
|
+
"p-256": "P-256",
|
|
23887
|
+
"secp256r1": "P-256",
|
|
23888
|
+
"secp384r1": "P-384",
|
|
23889
|
+
"p-384": "P-384",
|
|
23890
|
+
"secp521r1": "P-521",
|
|
23891
|
+
"p-521": "P-521"
|
|
23892
|
+
};
|
|
23893
|
+
function keyPairAlgorithm(type2, options) {
|
|
23894
|
+
const exponent = (value) => {
|
|
23895
|
+
let rest = value ?? 65537;
|
|
23896
|
+
const bytes2 = [];
|
|
23897
|
+
while (rest > 0) {
|
|
23898
|
+
bytes2.unshift(rest & 255);
|
|
23899
|
+
rest = Math.floor(rest / 256);
|
|
23900
|
+
}
|
|
23901
|
+
return new Uint8Array(bytes2);
|
|
23902
|
+
};
|
|
23903
|
+
switch (type2) {
|
|
23904
|
+
case "rsa":
|
|
23905
|
+
case "rsa-pss": {
|
|
23906
|
+
if (!Number.isInteger(options.modulusLength)) throw Object.assign(new TypeError('The "options.modulusLength" property must be of type number.'), { code: "ERR_INVALID_ARG_TYPE" });
|
|
23907
|
+
const name = type2 === "rsa" ? "RSASSA-PKCS1-v1_5" : "RSA-PSS";
|
|
23908
|
+
return { generate: { name, modulusLength: options.modulusLength, publicExponent: exponent(options.publicExponent), hash: "SHA-256" }, usages: ["sign", "verify"], name: type2 === "rsa" ? "RSA-SHA256" : "RSA-PSS" };
|
|
23909
|
+
}
|
|
23910
|
+
case "ec": {
|
|
23911
|
+
const curve = NAMED_CURVES[String(options.namedCurve ?? "").toLowerCase()];
|
|
23912
|
+
if (!curve) throw Object.assign(new TypeError(`Invalid EC curve name ${String(options.namedCurve)}`), { code: "ERR_CRYPTO_INVALID_CURVE" });
|
|
23913
|
+
return { generate: { name: "ECDSA", namedCurve: curve }, usages: ["sign", "verify"], name: curve === "P-256" ? "ES256" : curve === "P-384" ? "ES384" : "ES512" };
|
|
23914
|
+
}
|
|
23915
|
+
case "ed25519":
|
|
23916
|
+
return { generate: { name: "Ed25519" }, usages: ["sign", "verify"], name: "Ed25519" };
|
|
23917
|
+
case "x25519":
|
|
23918
|
+
return { generate: { name: "X25519" }, usages: ["deriveBits"], name: "X25519" };
|
|
23919
|
+
default:
|
|
23920
|
+
throw Object.assign(new TypeError(`The argument 'type' must be a supported key type. Received '${type2}'`), { code: "ERR_INVALID_ARG_VALUE" });
|
|
23921
|
+
}
|
|
23922
|
+
}
|
|
23923
|
+
function encodedKey(der, encoding, kind) {
|
|
23924
|
+
const expected = kind === "public" ? "spki" : "pkcs8";
|
|
23925
|
+
if (encoding.type !== void 0 && encoding.type !== expected) {
|
|
23926
|
+
unsupported2(`Encoding a ${kind} key as ${encoding.type}`);
|
|
23927
|
+
}
|
|
23928
|
+
const bytes2 = Buffer2.from(der);
|
|
23929
|
+
if (encoding.format === "der") return bytes2;
|
|
23930
|
+
if (encoding.format !== "pem") unsupported2(`Encoding a key in the ${String(encoding.format)} format`);
|
|
23931
|
+
const label = kind === "public" ? "PUBLIC KEY" : "PRIVATE KEY";
|
|
23932
|
+
const body = bytes2.toString("base64").match(/.{1,64}/g).join("\n");
|
|
23933
|
+
return `-----BEGIN ${label}-----
|
|
23934
|
+
${body}
|
|
23935
|
+
-----END ${label}-----
|
|
23936
|
+
`;
|
|
23937
|
+
}
|
|
23938
|
+
async function generateKeyPairAsync(type2, options) {
|
|
23939
|
+
const algorithm = keyPairAlgorithm(type2, options);
|
|
23940
|
+
const pair = await crypto.subtle.generateKey(algorithm.generate, true, algorithm.usages);
|
|
23941
|
+
const publicKey = options.publicKeyEncoding ? encodedKey(await crypto.subtle.exportKey("spki", pair.publicKey), options.publicKeyEncoding, "public") : new KeyObject2("public", pair.publicKey, algorithm.name);
|
|
23942
|
+
const privateKey = options.privateKeyEncoding ? encodedKey(await crypto.subtle.exportKey("pkcs8", pair.privateKey), options.privateKeyEncoding, "private") : new KeyObject2("private", pair.privateKey, algorithm.name);
|
|
23943
|
+
return { publicKey, privateKey };
|
|
23944
|
+
}
|
|
23945
|
+
function generateKeyPair2(type2, options, callback) {
|
|
23946
|
+
if (typeof options === "function") {
|
|
23947
|
+
callback = options;
|
|
23948
|
+
options = {};
|
|
23949
|
+
}
|
|
23950
|
+
if (typeof callback !== "function") throw Object.assign(new TypeError('The "callback" argument must be of type function.'), { code: "ERR_INVALID_ARG_TYPE" });
|
|
23951
|
+
const done = callback;
|
|
23952
|
+
keyPairAlgorithm(type2, options ?? {});
|
|
23953
|
+
generateKeyPairAsync(type2, options ?? {}).then(
|
|
23954
|
+
({ publicKey, privateKey }) => done(null, publicKey, privateKey),
|
|
23955
|
+
(error) => done(error instanceof Error ? error : new Error(String(error)))
|
|
23956
|
+
);
|
|
23957
|
+
}
|
|
23958
|
+
Object.defineProperty(generateKeyPair2, Symbol.for("nodejs.util.promisify.custom"), {
|
|
23959
|
+
value: (type2, options) => generateKeyPairAsync(type2, options ?? {})
|
|
23960
|
+
});
|
|
23877
23961
|
function timingSafeEqual2(a, b) {
|
|
23878
23962
|
if (a.length !== b.length) {
|
|
23879
23963
|
return false;
|
|
@@ -24030,11 +24114,12 @@ function createCryptoModule(require2) {
|
|
|
24030
24114
|
KeyObject: KeyObject2,
|
|
24031
24115
|
createSecretKey: createSecretKey2,
|
|
24032
24116
|
createPublicKey: createPublicKey2,
|
|
24033
|
-
createPrivateKey: createPrivateKey2
|
|
24117
|
+
createPrivateKey: createPrivateKey2,
|
|
24118
|
+
generateKeyPair: generateKeyPair2
|
|
24034
24119
|
};
|
|
24035
24120
|
}
|
|
24036
24121
|
const cryptoModule = createCryptoModule();
|
|
24037
|
-
const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported: unsupported$1, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
24122
|
+
const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported: unsupported$1, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, generateKeyPair, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
24038
24123
|
const cryptoShim = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
24039
24124
|
__proto__: null,
|
|
24040
24125
|
KeyObject,
|
|
@@ -24048,6 +24133,7 @@ const cryptoShim = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePr
|
|
|
24048
24133
|
createSign,
|
|
24049
24134
|
createVerify,
|
|
24050
24135
|
default: cryptoModule,
|
|
24136
|
+
generateKeyPair,
|
|
24051
24137
|
getCiphers,
|
|
24052
24138
|
getHashes,
|
|
24053
24139
|
getRandomValues,
|
|
@@ -39839,7 +39925,7 @@ class WorkerRuntime {
|
|
|
39839
39925
|
this.worker = new Worker(
|
|
39840
39926
|
new URL(
|
|
39841
39927
|
/* @vite-ignore */
|
|
39842
|
-
"/assets/runtime-worker-
|
|
39928
|
+
"/assets/runtime-worker-Bw5ePWPu.js",
|
|
39843
39929
|
import.meta.url
|
|
39844
39930
|
),
|
|
39845
39931
|
{ type: "module" }
|
package/dist/shims/crypto.d.ts
CHANGED
|
@@ -245,6 +245,19 @@ export declare function createCryptoModule(require?: (name: string) => any): {
|
|
|
245
245
|
format?: string;
|
|
246
246
|
}): Buffer | string;
|
|
247
247
|
};
|
|
248
|
+
generateKeyPair: (type: string, options: {
|
|
249
|
+
modulusLength?: number;
|
|
250
|
+
publicExponent?: number;
|
|
251
|
+
namedCurve?: string;
|
|
252
|
+
publicKeyEncoding?: {
|
|
253
|
+
type?: string;
|
|
254
|
+
format?: string;
|
|
255
|
+
};
|
|
256
|
+
privateKeyEncoding?: {
|
|
257
|
+
type?: string;
|
|
258
|
+
format?: string;
|
|
259
|
+
};
|
|
260
|
+
} | ((error: Error | null, publicKey?: unknown, privateKey?: unknown) => void), callback?: (error: Error | null, publicKey?: unknown, privateKey?: unknown) => void) => void;
|
|
248
261
|
};
|
|
249
262
|
declare const cryptoModule: {
|
|
250
263
|
unsupported: (operation: string) => never;
|
|
@@ -477,6 +490,19 @@ declare const cryptoModule: {
|
|
|
477
490
|
format?: string;
|
|
478
491
|
}): Buffer | string;
|
|
479
492
|
};
|
|
493
|
+
generateKeyPair: (type: string, options: {
|
|
494
|
+
modulusLength?: number;
|
|
495
|
+
publicExponent?: number;
|
|
496
|
+
namedCurve?: string;
|
|
497
|
+
publicKeyEncoding?: {
|
|
498
|
+
type?: string;
|
|
499
|
+
format?: string;
|
|
500
|
+
};
|
|
501
|
+
privateKeyEncoding?: {
|
|
502
|
+
type?: string;
|
|
503
|
+
format?: string;
|
|
504
|
+
};
|
|
505
|
+
} | ((error: Error | null, publicKey?: unknown, privateKey?: unknown) => void), callback?: (error: Error | null, publicKey?: unknown, privateKey?: unknown) => void) => void;
|
|
480
506
|
};
|
|
481
507
|
export declare const randomBytes: {
|
|
482
508
|
(size: number): Buffer;
|
|
@@ -679,6 +705,18 @@ export declare const randomBytes: {
|
|
|
679
705
|
type?: string;
|
|
680
706
|
format?: string;
|
|
681
707
|
}): Buffer | string;
|
|
682
|
-
},
|
|
708
|
+
}, generateKeyPair: (type: string, options: {
|
|
709
|
+
modulusLength?: number;
|
|
710
|
+
publicExponent?: number;
|
|
711
|
+
namedCurve?: string;
|
|
712
|
+
publicKeyEncoding?: {
|
|
713
|
+
type?: string;
|
|
714
|
+
format?: string;
|
|
715
|
+
};
|
|
716
|
+
privateKeyEncoding?: {
|
|
717
|
+
type?: string;
|
|
718
|
+
format?: string;
|
|
719
|
+
};
|
|
720
|
+
} | ((error: Error | null, publicKey?: unknown, privateKey?: unknown) => void), callback?: (error: Error | null, publicKey?: unknown, privateKey?: unknown) => void) => void, timingSafeEqual: (a: Buffer | Uint8Array, b: Buffer | Uint8Array) => boolean, getCiphers: () => string[], getHashes: () => string[];
|
|
683
721
|
export type KeyObject = InstanceType<typeof KeyObject>;
|
|
684
722
|
export default cryptoModule;
|
package/package.json
CHANGED
package/src/shims/crypto.ts
CHANGED
|
@@ -800,6 +800,92 @@ function createPrivateKey(key: KeyLike): KeyObject {
|
|
|
800
800
|
return new KeyObject('private', keyInfo.keyData as Uint8Array, keyInfo.algorithm);
|
|
801
801
|
}
|
|
802
802
|
|
|
803
|
+
// ============================================================================
|
|
804
|
+
// generateKeyPair: Node's asynchronous key generation, done by WebCrypto.
|
|
805
|
+
// A browser has no synchronous key generation, so generateKeyPairSync stays
|
|
806
|
+
// absent; the callback form and its promisified form (which resolves
|
|
807
|
+
// { publicKey, privateKey }, as Node's custom promisify does) are complete for
|
|
808
|
+
// the types WebCrypto generates. jose's Node build promisifies this function
|
|
809
|
+
// when it loads, so its absence failed any module that imported jose.
|
|
810
|
+
// ============================================================================
|
|
811
|
+
|
|
812
|
+
type KeyEncoding = { type?: string; format?: string };
|
|
813
|
+
type KeyPairOptions = { modulusLength?: number; publicExponent?: number; namedCurve?: string; publicKeyEncoding?: KeyEncoding; privateKeyEncoding?: KeyEncoding };
|
|
814
|
+
|
|
815
|
+
const NAMED_CURVES: Record<string, string> = {
|
|
816
|
+
'prime256v1': 'P-256', 'p-256': 'P-256', 'secp256r1': 'P-256',
|
|
817
|
+
'secp384r1': 'P-384', 'p-384': 'P-384',
|
|
818
|
+
'secp521r1': 'P-521', 'p-521': 'P-521',
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
function keyPairAlgorithm(type: string, options: KeyPairOptions): { generate: AlgorithmIdentifier & Record<string, unknown>; usages: KeyUsage[]; name: string } {
|
|
822
|
+
const exponent = (value: number | undefined): Uint8Array => {
|
|
823
|
+
let rest = value ?? 0x10001;
|
|
824
|
+
const bytes: number[] = [];
|
|
825
|
+
while (rest > 0) { bytes.unshift(rest & 0xff); rest = Math.floor(rest / 256); }
|
|
826
|
+
return new Uint8Array(bytes);
|
|
827
|
+
};
|
|
828
|
+
switch (type) {
|
|
829
|
+
case 'rsa':
|
|
830
|
+
case 'rsa-pss': {
|
|
831
|
+
if (!Number.isInteger(options.modulusLength)) throw Object.assign(new TypeError('The "options.modulusLength" property must be of type number.'), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
832
|
+
const name = type === 'rsa' ? 'RSASSA-PKCS1-v1_5' : 'RSA-PSS';
|
|
833
|
+
return { generate: { name, modulusLength: options.modulusLength, publicExponent: exponent(options.publicExponent), hash: 'SHA-256' }, usages: ['sign', 'verify'], name: type === 'rsa' ? 'RSA-SHA256' : 'RSA-PSS' };
|
|
834
|
+
}
|
|
835
|
+
case 'ec': {
|
|
836
|
+
const curve = NAMED_CURVES[String(options.namedCurve ?? '').toLowerCase()];
|
|
837
|
+
if (!curve) throw Object.assign(new TypeError(`Invalid EC curve name ${String(options.namedCurve)}`), { code: 'ERR_CRYPTO_INVALID_CURVE' });
|
|
838
|
+
return { generate: { name: 'ECDSA', namedCurve: curve }, usages: ['sign', 'verify'], name: curve === 'P-256' ? 'ES256' : curve === 'P-384' ? 'ES384' : 'ES512' };
|
|
839
|
+
}
|
|
840
|
+
case 'ed25519':
|
|
841
|
+
return { generate: { name: 'Ed25519' }, usages: ['sign', 'verify'], name: 'Ed25519' };
|
|
842
|
+
case 'x25519':
|
|
843
|
+
return { generate: { name: 'X25519' }, usages: ['deriveBits'], name: 'X25519' };
|
|
844
|
+
default:
|
|
845
|
+
throw Object.assign(new TypeError(`The argument 'type' must be a supported key type. Received '${type}'`), { code: 'ERR_INVALID_ARG_VALUE' });
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
function encodedKey(der: ArrayBuffer, encoding: KeyEncoding, kind: 'public' | 'private'): string | Buffer {
|
|
850
|
+
const expected = kind === 'public' ? 'spki' : 'pkcs8';
|
|
851
|
+
if (encoding.type !== undefined && encoding.type !== expected) {
|
|
852
|
+
unsupported(`Encoding a ${kind} key as ${encoding.type}`);
|
|
853
|
+
}
|
|
854
|
+
const bytes = Buffer.from(der);
|
|
855
|
+
if (encoding.format === 'der') return bytes;
|
|
856
|
+
if (encoding.format !== 'pem') unsupported(`Encoding a key in the ${String(encoding.format)} format`);
|
|
857
|
+
const label = kind === 'public' ? 'PUBLIC KEY' : 'PRIVATE KEY';
|
|
858
|
+
const body = bytes.toString('base64').match(/.{1,64}/g)!.join('\n');
|
|
859
|
+
return `-----BEGIN ${label}-----\n${body}\n-----END ${label}-----\n`;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
async function generateKeyPairAsync(type: string, options: KeyPairOptions): Promise<{ publicKey: KeyObject | string | Buffer; privateKey: KeyObject | string | Buffer }> {
|
|
863
|
+
const algorithm = keyPairAlgorithm(type, options);
|
|
864
|
+
const pair = await crypto.subtle.generateKey(algorithm.generate as AlgorithmIdentifier, true, algorithm.usages) as CryptoKeyPair;
|
|
865
|
+
const publicKey = options.publicKeyEncoding
|
|
866
|
+
? encodedKey(await crypto.subtle.exportKey('spki', pair.publicKey), options.publicKeyEncoding, 'public')
|
|
867
|
+
: new KeyObject('public', pair.publicKey, algorithm.name);
|
|
868
|
+
const privateKey = options.privateKeyEncoding
|
|
869
|
+
? encodedKey(await crypto.subtle.exportKey('pkcs8', pair.privateKey), options.privateKeyEncoding, 'private')
|
|
870
|
+
: new KeyObject('private', pair.privateKey, algorithm.name);
|
|
871
|
+
return { publicKey, privateKey };
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function generateKeyPair(type: string, options: KeyPairOptions | ((error: Error | null, publicKey?: unknown, privateKey?: unknown) => void), callback?: (error: Error | null, publicKey?: unknown, privateKey?: unknown) => void): void {
|
|
875
|
+
if (typeof options === 'function') { callback = options; options = {}; }
|
|
876
|
+
if (typeof callback !== 'function') throw Object.assign(new TypeError('The "callback" argument must be of type function.'), { code: 'ERR_INVALID_ARG_TYPE' });
|
|
877
|
+
const done = callback;
|
|
878
|
+
// Argument errors throw synchronously, as Node's do.
|
|
879
|
+
keyPairAlgorithm(type, options ?? {});
|
|
880
|
+
generateKeyPairAsync(type, options ?? {}).then(
|
|
881
|
+
({ publicKey, privateKey }) => done(null, publicKey, privateKey),
|
|
882
|
+
(error) => done(error instanceof Error ? error : new Error(String(error))),
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
Object.defineProperty(generateKeyPair, Symbol.for('nodejs.util.promisify.custom'), {
|
|
886
|
+
value: (type: string, options?: KeyPairOptions) => generateKeyPairAsync(type, options ?? {}),
|
|
887
|
+
});
|
|
888
|
+
|
|
803
889
|
// ============================================================================
|
|
804
890
|
// Utility functions
|
|
805
891
|
// ============================================================================
|
|
@@ -1067,10 +1153,11 @@ return {
|
|
|
1067
1153
|
createSecretKey,
|
|
1068
1154
|
createPublicKey,
|
|
1069
1155
|
createPrivateKey,
|
|
1156
|
+
generateKeyPair,
|
|
1070
1157
|
};
|
|
1071
1158
|
|
|
1072
1159
|
}
|
|
1073
1160
|
const cryptoModule = createCryptoModule();
|
|
1074
|
-
export const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
1161
|
+
export const { randomBytes, randomFillSync, randomFill, randomUUID, randomInt, getRandomValues, unsupported, createHash, createHmac, hash, pbkdf2Sync, pbkdf2, sign, verify, createSign, createVerify, KeyObject, createSecretKey, createPublicKey, createPrivateKey, generateKeyPair, timingSafeEqual, getCiphers, getHashes } = cryptoModule;
|
|
1075
1162
|
export type KeyObject = InstanceType<typeof KeyObject>;
|
|
1076
1163
|
export default cryptoModule;
|