cf-workers-native 0.1.4 → 0.1.5
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.
- package/dist/browser/documents/page-source.js +1 -1
- package/dist/browser/fetch/host-call.js +6 -1
- package/dist/browser/fetch/native-network.d.ts +1 -1
- package/dist/browser/fetch/relay.js +2 -0
- package/dist/browser/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- package/dist/cloudflare/workers/worker-runtime.js +2 -1
- package/dist/compat/console/index.d.ts +35 -0
- package/dist/compat/console/index.js +92 -0
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +19 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +239 -0
- package/dist/compat/crypto/brorand.cjs +74 -0
- package/dist/compat/crypto/brorand.d.cts +6 -0
- package/dist/compat/crypto/certificate.d.ts +14 -0
- package/dist/compat/crypto/certificate.js +66 -0
- package/dist/compat/crypto/ecdh.d.ts +15 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +52 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +2 -0
- package/dist/compat/crypto/index.d.ts +30 -0
- package/dist/compat/crypto/index.js +83 -0
- package/dist/compat/crypto/key-formats.d.ts +6 -0
- package/dist/compat/crypto/key-formats.js +120 -0
- package/dist/compat/crypto/key-state.d.ts +9 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +50 -0
- package/dist/compat/crypto/keys.js +209 -0
- package/dist/compat/crypto/metadata.d.ts +4 -0
- package/dist/compat/crypto/metadata.js +22 -0
- package/dist/compat/crypto/native-crypto.d.ts +68 -0
- package/dist/compat/crypto/native-crypto.js +569 -0
- package/dist/compat/crypto/prime.d.ts +19 -0
- package/dist/compat/crypto/prime.js +146 -0
- package/dist/compat/crypto/random.d.ts +7 -0
- package/dist/compat/crypto/random.js +123 -0
- package/dist/compat/crypto/rsa.d.ts +5 -0
- package/dist/compat/crypto/rsa.js +173 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +150 -0
- package/dist/compat/crypto/sign-errors.d.ts +8 -0
- package/dist/compat/crypto/sign-errors.js +13 -0
- package/dist/compat/crypto/sign-handle.d.ts +22 -0
- package/dist/compat/crypto/sign-handle.js +105 -0
- package/dist/compat/crypto/signatures.d.ts +28 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/symmetric.d.ts +29 -0
- package/dist/compat/crypto/symmetric.js +104 -0
- package/dist/compat/crypto/validation.d.ts +3 -0
- package/dist/compat/crypto/validation.js +19 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +406 -0
- package/dist/compat/events/errors.d.ts +8 -0
- package/dist/compat/events/errors.js +39 -0
- package/dist/compat/events/index.cjs +2 -0
- package/dist/compat/events/index.d.cts +2 -0
- package/dist/compat/events/index.d.ts +64 -0
- package/dist/compat/events/index.js +792 -0
- package/dist/compat/events/intrinsics.d.ts +1 -0
- package/dist/compat/events/intrinsics.js +2 -0
- package/dist/compat/fs/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +255 -0
- package/dist/compat/fs/index.d.ts +7 -84
- package/dist/compat/fs/index.js +218 -252
- package/dist/compat/fs/mode.d.ts +1 -0
- package/dist/compat/fs/mode.js +13 -0
- package/dist/compat/fs/promises/index.d.ts +1 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/writable.d.ts +6 -0
- package/dist/compat/fs/writable.js +142 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -0
- package/dist/compat/http-common/index.d.ts +37 -0
- package/dist/compat/http-common/index.js +43 -0
- package/dist/compat/http-outgoing/index.d.ts +13 -0
- package/dist/compat/http-outgoing/index.js +32 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +107 -0
- package/dist/compat/module/builtins.d.ts +1 -0
- package/dist/compat/module/builtins.js +101 -0
- package/dist/compat/module/index.d.ts +4 -2
- package/dist/compat/module/index.js +22 -17
- package/dist/compat/module/metadata.d.ts +44 -0
- package/dist/compat/module/metadata.js +127 -0
- package/dist/compat/net/internal-net.js +4 -0
- package/dist/compat/net/sockets.d.ts +5 -0
- package/dist/compat/net/sockets.js +74 -13
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +18 -0
- package/dist/compat/process/index.js +140 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +4 -0
- package/dist/compat/stream-web/LICENSE +22 -0
- package/dist/compat/stream-web/ponyfill.d.ts +1 -1
- package/dist/compat/stream-web/ponyfill.js +2369 -1775
- package/dist/compat/stream-web/runtime.d.ts +1 -1
- package/dist/compat/stream-web/runtime.js +3 -2
- package/dist/compat/stream-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/stream-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/stream-wrap/index.d.ts +1 -0
- package/dist/compat/stream-wrap/index.js +1 -0
- package/dist/compat/tls/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls/LICENSE-APACHE +177 -0
- package/dist/compat/tls/constants.d.ts +7 -0
- package/dist/compat/tls/constants.js +14 -0
- package/dist/compat/tls/errors.d.ts +35 -0
- package/dist/compat/tls/errors.js +68 -0
- package/dist/compat/tls/helpers.d.ts +9 -0
- package/dist/compat/tls/helpers.js +207 -0
- package/dist/compat/tls/index.d.ts +26 -0
- package/dist/compat/tls/index.js +50 -0
- package/dist/compat/tls/jsstream.d.ts +19 -0
- package/dist/compat/tls/jsstream.js +186 -0
- package/dist/compat/tls/native-session.d.ts +3 -0
- package/dist/compat/tls/native-session.js +187 -0
- package/dist/compat/tls/wrap.d.ts +65 -0
- package/dist/compat/tls/wrap.js +544 -0
- package/dist/compat/tls-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/tls-wrap/index.d.ts +7 -0
- package/dist/compat/tls-wrap/index.js +3 -0
- package/dist/compat/util/index.js +43 -1
- package/dist/compat/util/types/index.d.ts +3 -0
- package/dist/compat/util/types/index.js +5 -0
- package/dist/compat/zlib/LICENSE.brotli +19 -0
- package/dist/compat/zlib/LICENSE.emscripten +102 -0
- package/dist/compat/zlib/LICENSE.musl +193 -0
- package/dist/compat/zlib/LICENSE.zstd +30 -0
- package/dist/compat/zlib/compression.d.ts +55 -0
- package/dist/compat/zlib/compression.js +259 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/index.d.ts +180 -0
- package/dist/compat/zlib/index.js +177 -0
- package/dist/compat/zlib/native-codecs.d.ts +17 -0
- package/dist/compat/zlib/native-codecs.js +168 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +11 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +20 -1
- package/dist/tooling/vite.js +4 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +56 -8
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +4 -2
- package/native/ios/NetworkBridge.swift +11 -1
- package/native/ios/SocketBridge.swift +37 -2
- package/native/ios/WorkersCapacitorPlugin.swift +4 -0
- package/package.json +3 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
export type PrimeNum = ArrayBuffer | ArrayBufferView | Buffer | bigint;
|
|
3
|
+
export interface GeneratePrimeOptions {
|
|
4
|
+
add?: PrimeNum;
|
|
5
|
+
rem?: PrimeNum;
|
|
6
|
+
safe?: boolean;
|
|
7
|
+
bigint?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface CheckPrimeOptions {
|
|
10
|
+
checks?: number;
|
|
11
|
+
}
|
|
12
|
+
export type GeneratePrimeCallback = (err: Error | null, prime?: bigint | ArrayBuffer) => void;
|
|
13
|
+
export type CheckPrimeCallback = (err: Error | null, prime?: boolean) => void;
|
|
14
|
+
export declare function generatePrimeSync(size: number, options?: GeneratePrimeOptions): bigint | ArrayBuffer;
|
|
15
|
+
export declare function generatePrime(size: number, options: GeneratePrimeOptions, callback: GeneratePrimeCallback): void;
|
|
16
|
+
export declare function generatePrime(size: number, callback: GeneratePrimeCallback): void;
|
|
17
|
+
export declare function checkPrimeSync(candidate: PrimeNum, options?: CheckPrimeOptions): boolean;
|
|
18
|
+
export declare function checkPrime(candidate: PrimeNum, options: CheckPrimeOptions, callback: CheckPrimeCallback): void;
|
|
19
|
+
export declare function checkPrime(candidate: PrimeNum, callback: CheckPrimeCallback): void;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Adapted from pinned workerd crypto_random.ts (Apache-2.0 / Node MIT).
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { validateObject, validateFunction, validateBoolean, validateInt32, } from "../net/validators.js";
|
|
4
|
+
import { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
|
|
5
|
+
import { isAnyArrayBuffer, isArrayBufferView } from "../util/types/index.js";
|
|
6
|
+
import { nativePrimeCheck, nativePrimeGenerate } from "./native-crypto.js";
|
|
7
|
+
function view(value) {
|
|
8
|
+
return ArrayBuffer.isView(value)
|
|
9
|
+
? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
|
10
|
+
: new Uint8Array(value);
|
|
11
|
+
}
|
|
12
|
+
function arrayBufferToUnsignedBigInt(value) {
|
|
13
|
+
const hex = Buffer.from(view(value)).toString("hex");
|
|
14
|
+
return hex ? BigInt("0x" + hex) : 0n;
|
|
15
|
+
}
|
|
16
|
+
const cryptoImpl = {
|
|
17
|
+
checkPrimeSync(candidate, checks) {
|
|
18
|
+
const data = view(candidate);
|
|
19
|
+
if (checks > 64)
|
|
20
|
+
throw new RangeError("Invalid number of checks");
|
|
21
|
+
if (data.length > 1024)
|
|
22
|
+
throw new RangeError("checkPrime candidate exceeds maximum size");
|
|
23
|
+
return nativePrimeCheck(data, checks);
|
|
24
|
+
},
|
|
25
|
+
randomPrime(bits, safe, add, rem) {
|
|
26
|
+
if (bits > 8192)
|
|
27
|
+
throw new RangeError("generatePrime size exceeds maximum (8192 bits)");
|
|
28
|
+
if (add !== undefined) {
|
|
29
|
+
const a = arrayBufferToUnsignedBigInt(add), r = rem === undefined ? undefined : arrayBufferToUnsignedBigInt(rem);
|
|
30
|
+
if (!((a === 12n && r === 11n) ||
|
|
31
|
+
(a === 24n && r === 23n) ||
|
|
32
|
+
(a === 60n && r === 59n)))
|
|
33
|
+
throw new RangeError("Invalid values for add and rem");
|
|
34
|
+
if (a.toString(2).length > bits)
|
|
35
|
+
throw new RangeError("options.add must not be bigger than size of the requested prime");
|
|
36
|
+
}
|
|
37
|
+
return nativePrimeGenerate(bits, safe, add === undefined ? undefined : view(add), rem === undefined ? undefined : view(rem)).buffer;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
function processGeneratePrimeOptions(options) {
|
|
41
|
+
validateObject(options, "options");
|
|
42
|
+
const { safe = false, bigint = false } = options;
|
|
43
|
+
let { add, rem } = options;
|
|
44
|
+
validateBoolean(safe, "options.safe");
|
|
45
|
+
validateBoolean(bigint, "options.bigint");
|
|
46
|
+
if (add !== undefined) {
|
|
47
|
+
if (typeof add === "bigint") {
|
|
48
|
+
add = unsignedBigIntToBuffer(add, "options.add");
|
|
49
|
+
}
|
|
50
|
+
else if (!isAnyArrayBuffer(add) && !isArrayBufferView(add)) {
|
|
51
|
+
throw new ERR_INVALID_ARG_TYPE("options.add", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], add);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (rem !== undefined) {
|
|
55
|
+
if (typeof rem === "bigint") {
|
|
56
|
+
rem = unsignedBigIntToBuffer(rem, "options.rem");
|
|
57
|
+
}
|
|
58
|
+
else if (!isAnyArrayBuffer(rem) && !isArrayBufferView(rem)) {
|
|
59
|
+
throw new ERR_INVALID_ARG_TYPE("options.rem", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], rem);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
safe,
|
|
64
|
+
bigint,
|
|
65
|
+
add: add,
|
|
66
|
+
rem: rem,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function generatePrimeSync(size, options = {}) {
|
|
70
|
+
validateInt32(size, "size", 1);
|
|
71
|
+
const { safe, bigint, add, rem } = processGeneratePrimeOptions(options);
|
|
72
|
+
const primeBuf = cryptoImpl.randomPrime(size, safe, add, rem);
|
|
73
|
+
return bigint ? arrayBufferToUnsignedBigInt(primeBuf) : primeBuf;
|
|
74
|
+
}
|
|
75
|
+
export function generatePrime(size, options, callback) {
|
|
76
|
+
validateInt32(size, "size", 1);
|
|
77
|
+
if (typeof options === "function") {
|
|
78
|
+
callback = options;
|
|
79
|
+
options = {};
|
|
80
|
+
}
|
|
81
|
+
validateFunction(callback, "callback");
|
|
82
|
+
const { safe, bigint, add, rem } = processGeneratePrimeOptions(options);
|
|
83
|
+
new Promise((res, rej) => {
|
|
84
|
+
try {
|
|
85
|
+
const primeBuf = cryptoImpl.randomPrime(size, safe, add, rem);
|
|
86
|
+
res(bigint ? arrayBufferToUnsignedBigInt(primeBuf) : primeBuf);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
rej(err);
|
|
90
|
+
}
|
|
91
|
+
}).then((val) => {
|
|
92
|
+
callback(null, val);
|
|
93
|
+
}, (err) => {
|
|
94
|
+
callback(err);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function unsignedBigIntToBuffer(bigint, name) {
|
|
98
|
+
if (bigint < 0) {
|
|
99
|
+
throw new ERR_OUT_OF_RANGE(name, ">= 0", bigint);
|
|
100
|
+
}
|
|
101
|
+
const hex = bigint.toString(16);
|
|
102
|
+
const padded = hex.padStart(hex.length + (hex.length % 2), "0");
|
|
103
|
+
return Buffer.from(padded, "hex");
|
|
104
|
+
}
|
|
105
|
+
function validateCandidate(candidate) {
|
|
106
|
+
if (typeof candidate === "bigint")
|
|
107
|
+
candidate = unsignedBigIntToBuffer(candidate, "candidate");
|
|
108
|
+
if (!isAnyArrayBuffer(candidate) && !isArrayBufferView(candidate)) {
|
|
109
|
+
throw new ERR_INVALID_ARG_TYPE("candidate", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], candidate);
|
|
110
|
+
}
|
|
111
|
+
return candidate;
|
|
112
|
+
}
|
|
113
|
+
function validateChecks(options) {
|
|
114
|
+
const { checks = 0 } = options;
|
|
115
|
+
// The checks option is unsigned but must fit into a signed 32-bit integer for OpenSSL.
|
|
116
|
+
validateInt32(checks, "options.checks", 0);
|
|
117
|
+
return checks;
|
|
118
|
+
}
|
|
119
|
+
export function checkPrimeSync(candidate, options = {}) {
|
|
120
|
+
candidate = validateCandidate(candidate);
|
|
121
|
+
validateObject(options, "options");
|
|
122
|
+
const checks = validateChecks(options);
|
|
123
|
+
return cryptoImpl.checkPrimeSync(candidate, checks);
|
|
124
|
+
}
|
|
125
|
+
export function checkPrime(candidate, options, callback) {
|
|
126
|
+
candidate = validateCandidate(candidate);
|
|
127
|
+
if (typeof options === "function") {
|
|
128
|
+
callback = options;
|
|
129
|
+
options = {};
|
|
130
|
+
}
|
|
131
|
+
validateObject(options, "options");
|
|
132
|
+
validateFunction(callback, "callback");
|
|
133
|
+
const checks = validateChecks(options);
|
|
134
|
+
new Promise((res, rej) => {
|
|
135
|
+
try {
|
|
136
|
+
res(cryptoImpl.checkPrimeSync(candidate, checks));
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
rej(err);
|
|
140
|
+
}
|
|
141
|
+
}).then((val) => {
|
|
142
|
+
callback(null, val);
|
|
143
|
+
}, (err) => {
|
|
144
|
+
callback(err);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RandomUUIDOptions } from "node:crypto";
|
|
2
|
+
export type RandomIntCallback = (err: Error | null, n?: number) => void;
|
|
3
|
+
export declare function randomInt(max: number): number;
|
|
4
|
+
export declare function randomInt(min: number, max: number): number;
|
|
5
|
+
export declare function randomInt(max: number, callback: RandomIntCallback): void;
|
|
6
|
+
export declare function randomInt(min: number, max: number, callback: RandomIntCallback): void;
|
|
7
|
+
export declare function randomUUID(options?: RandomUUIDOptions): string;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Copyright (c) 2017-2022 Cloudflare, Inc.
|
|
2
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
3
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
6
|
+
//
|
|
7
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
// copy of this software and associated documentation files (the
|
|
9
|
+
// "Software"), to deal in the Software without restriction, including
|
|
10
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
12
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
13
|
+
// following conditions:
|
|
14
|
+
//
|
|
15
|
+
// The above copyright notice and this permission notice shall be included
|
|
16
|
+
// in all copies or substantial portions of the Software.
|
|
17
|
+
//
|
|
18
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
19
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
21
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
22
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
23
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
24
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
// Adapted from workerd@5358e3d337336eaf696a48bc1563d808b10feba5.
|
|
26
|
+
import { Buffer } from "node:buffer";
|
|
27
|
+
import { validateInteger } from "./validation.js";
|
|
28
|
+
import { validateBoolean, validateObject } from "../net/validators.js";
|
|
29
|
+
import { ERR_OUT_OF_RANGE } from "../net/errors.js";
|
|
30
|
+
// Cache size stays below the Web Crypto per-call limit.
|
|
31
|
+
function randomFillSync(buffer) {
|
|
32
|
+
return globalThis.crypto.getRandomValues(buffer);
|
|
33
|
+
}
|
|
34
|
+
const RAND_MAX = 0xffff_ffff_ffff;
|
|
35
|
+
// Cache random data to use in randomInt. The cache size must be evenly
|
|
36
|
+
// divisible by 6 because each attempt to obtain a random int uses 6 bytes.
|
|
37
|
+
const randomCache = Buffer.alloc(6 * 1024);
|
|
38
|
+
let randomCacheOffset = 0;
|
|
39
|
+
let initialized = false;
|
|
40
|
+
function getRandomInt(min, max) {
|
|
41
|
+
if (!initialized) {
|
|
42
|
+
randomFillSync(randomCache);
|
|
43
|
+
initialized = true;
|
|
44
|
+
}
|
|
45
|
+
// First we generate a random int between [0..range)
|
|
46
|
+
const range = max - min;
|
|
47
|
+
if (!(range <= RAND_MAX)) {
|
|
48
|
+
throw new ERR_OUT_OF_RANGE(`max${max ? "" : " - min"}`, `<= ${RAND_MAX}`, range);
|
|
49
|
+
}
|
|
50
|
+
// For (x % range) to produce an unbiased value greater than or equal to 0 and
|
|
51
|
+
// less than range, x must be drawn randomly from the set of integers greater
|
|
52
|
+
// than or equal to 0 and less than randLimit.
|
|
53
|
+
const randLimit = RAND_MAX - (RAND_MAX % range);
|
|
54
|
+
// If we don't have a callback, or if there is still data in the cache, we can
|
|
55
|
+
// do this synchronously, which is super fast.
|
|
56
|
+
while (randomCacheOffset <= randomCache.length) {
|
|
57
|
+
if (randomCacheOffset === randomCache.length) {
|
|
58
|
+
// This might block the thread for a bit, but we are in sync mode.
|
|
59
|
+
randomFillSync(randomCache);
|
|
60
|
+
randomCacheOffset = 0;
|
|
61
|
+
}
|
|
62
|
+
const x = randomCache.readUIntBE(randomCacheOffset, 6);
|
|
63
|
+
randomCacheOffset += 6;
|
|
64
|
+
if (x < randLimit) {
|
|
65
|
+
return (x % range) + min;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return 0; // Should be unreachable.
|
|
69
|
+
}
|
|
70
|
+
export function randomInt(minOrMax, maxOrCallback, callback) {
|
|
71
|
+
let min = 0;
|
|
72
|
+
let max = 0;
|
|
73
|
+
if (typeof callback === "function") {
|
|
74
|
+
validateInteger(minOrMax, "min");
|
|
75
|
+
validateInteger(maxOrCallback, "max");
|
|
76
|
+
min = minOrMax;
|
|
77
|
+
max = maxOrCallback;
|
|
78
|
+
}
|
|
79
|
+
else if (typeof maxOrCallback === "function") {
|
|
80
|
+
min = 0;
|
|
81
|
+
validateInteger(minOrMax, "max");
|
|
82
|
+
max = minOrMax;
|
|
83
|
+
callback = maxOrCallback;
|
|
84
|
+
}
|
|
85
|
+
else if (arguments.length === 2) {
|
|
86
|
+
validateInteger(minOrMax, "min");
|
|
87
|
+
validateInteger(maxOrCallback, "max");
|
|
88
|
+
min = minOrMax;
|
|
89
|
+
max = maxOrCallback;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
min = 0;
|
|
93
|
+
validateInteger(minOrMax, "max");
|
|
94
|
+
max = minOrMax;
|
|
95
|
+
}
|
|
96
|
+
if (min >= max) {
|
|
97
|
+
throw new ERR_OUT_OF_RANGE("min", "min < max", min);
|
|
98
|
+
}
|
|
99
|
+
if (callback != null) {
|
|
100
|
+
new Promise((res) => {
|
|
101
|
+
res(getRandomInt(min, max));
|
|
102
|
+
}).then((n) => {
|
|
103
|
+
callback(null, n);
|
|
104
|
+
}, (err) => {
|
|
105
|
+
callback(err);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return getRandomInt(min, max);
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
export function randomUUID(options) {
|
|
114
|
+
// While we do not actually use the entropy cache, we go ahead and validate
|
|
115
|
+
// the input parameters as Node.js does.
|
|
116
|
+
if (options !== undefined) {
|
|
117
|
+
validateObject(options, "options");
|
|
118
|
+
if (options.disableEntropyCache !== undefined) {
|
|
119
|
+
validateBoolean(options.disableEntropyCache, "options.disableEntropyCache");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return crypto.randomUUID();
|
|
123
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
export declare function privateEncrypt(privateKey: CreateAsymmetricKeyOptions | KeyObject | CryptoKey | undefined, buffer: string | ArrayBufferView | ArrayBuffer | undefined): Buffer;
|
|
3
|
+
export declare function privateDecrypt(privateKey: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
|
|
4
|
+
export declare function publicEncrypt(key: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
|
|
5
|
+
export declare function publicDecrypt(key: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Pinned workerd crypto_cipher.ts; LICENSE-APACHE and LICENSE-3RD-PARTY.md.
|
|
3
|
+
import { Buffer } from "node:buffer";
|
|
4
|
+
import { validateNumber, validateString } from "../net/validators.js";
|
|
5
|
+
import { ERR_INVALID_ARG_TYPE, ERR_MISSING_ARGS } from "../net/errors.js";
|
|
6
|
+
import { ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE } from "./sign-errors.js";
|
|
7
|
+
import { isAnyArrayBuffer, isArrayBufferView } from "../util/types/index.js";
|
|
8
|
+
import { isKeyObject, getKeyObjectHandle } from "./keys.js";
|
|
9
|
+
import { createPrivateKey, createPublicKey } from "./asymmetric.js";
|
|
10
|
+
import { keyStates } from "./key-state.js";
|
|
11
|
+
const kRsaPkcs1Padding = 1;
|
|
12
|
+
function operate(operation, key, data, options) {
|
|
13
|
+
const handle = keyStates.get(key)?.asymmetric;
|
|
14
|
+
if (!handle)
|
|
15
|
+
throw Object.assign(new Error("Synchronous access to opaque host key material is unavailable"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
|
|
16
|
+
if (data instanceof ArrayBuffer)
|
|
17
|
+
data = new Uint8Array(data);
|
|
18
|
+
return handle.rsa(operation, data, options);
|
|
19
|
+
}
|
|
20
|
+
const cryptoImpl = {
|
|
21
|
+
publicEncrypt: (...args) => operate(0, ...args),
|
|
22
|
+
privateDecrypt: (...args) => operate(1, ...args),
|
|
23
|
+
privateEncrypt: (...args) => operate(2, ...args),
|
|
24
|
+
publicDecrypt: (...args) => operate(3, ...args),
|
|
25
|
+
};
|
|
26
|
+
function getPaddingAndHash(options) {
|
|
27
|
+
const { padding = kRsaPkcs1Padding, oaepHash = "sha256", oaepLabel, encoding = "utf8", } = options;
|
|
28
|
+
validateNumber(padding, "options.padding");
|
|
29
|
+
validateString(oaepHash, "options.oaepHash");
|
|
30
|
+
let label;
|
|
31
|
+
if (oaepLabel !== undefined) {
|
|
32
|
+
if (typeof oaepLabel === "string") {
|
|
33
|
+
label = Buffer.from(oaepLabel, encoding);
|
|
34
|
+
}
|
|
35
|
+
else if (isAnyArrayBuffer(oaepLabel) || isArrayBufferView(oaepLabel)) {
|
|
36
|
+
label = oaepLabel;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new ERR_INVALID_ARG_TYPE("options.oaepLabel", ["string", "Buffer", "TypedArray", "DataView", "ArrayBuffer"], oaepLabel);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { padding, oaepHash, oaepLabel: label };
|
|
43
|
+
}
|
|
44
|
+
export function privateEncrypt(privateKey, buffer) {
|
|
45
|
+
if (privateKey === undefined) {
|
|
46
|
+
throw new ERR_MISSING_ARGS("privateKey");
|
|
47
|
+
}
|
|
48
|
+
if (buffer === undefined) {
|
|
49
|
+
throw new ERR_MISSING_ARGS("buffer");
|
|
50
|
+
}
|
|
51
|
+
const key = (() => {
|
|
52
|
+
if (privateKey instanceof CryptoKey) {
|
|
53
|
+
if (privateKey.type !== "private") {
|
|
54
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
|
|
55
|
+
}
|
|
56
|
+
return privateKey;
|
|
57
|
+
}
|
|
58
|
+
else if (isKeyObject(privateKey)) {
|
|
59
|
+
if (privateKey.type !== "private") {
|
|
60
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
|
|
61
|
+
}
|
|
62
|
+
return getKeyObjectHandle(privateKey);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const pvtKey = createPrivateKey(privateKey);
|
|
66
|
+
return getKeyObjectHandle(pvtKey);
|
|
67
|
+
}
|
|
68
|
+
})();
|
|
69
|
+
if (typeof buffer === "string") {
|
|
70
|
+
const { encoding = "utf8" } = privateKey;
|
|
71
|
+
buffer = Buffer.from(buffer, encoding);
|
|
72
|
+
}
|
|
73
|
+
return Buffer.from(cryptoImpl.privateEncrypt(key, buffer, getPaddingAndHash(privateKey)));
|
|
74
|
+
}
|
|
75
|
+
export function privateDecrypt(privateKey, buffer) {
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
77
|
+
if (privateKey === undefined) {
|
|
78
|
+
throw new ERR_MISSING_ARGS("privateKey");
|
|
79
|
+
}
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
81
|
+
if (buffer === undefined) {
|
|
82
|
+
throw new ERR_MISSING_ARGS("buffer");
|
|
83
|
+
}
|
|
84
|
+
const key = (() => {
|
|
85
|
+
if (privateKey instanceof CryptoKey) {
|
|
86
|
+
if (privateKey.type !== "private") {
|
|
87
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
|
|
88
|
+
}
|
|
89
|
+
return privateKey;
|
|
90
|
+
}
|
|
91
|
+
else if (isKeyObject(privateKey)) {
|
|
92
|
+
if (privateKey.type !== "private") {
|
|
93
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
|
|
94
|
+
}
|
|
95
|
+
return getKeyObjectHandle(privateKey);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const pvtKey = createPrivateKey(privateKey);
|
|
99
|
+
return getKeyObjectHandle(pvtKey);
|
|
100
|
+
}
|
|
101
|
+
})();
|
|
102
|
+
if (typeof buffer === "string") {
|
|
103
|
+
const { encoding = "utf8" } = privateKey;
|
|
104
|
+
buffer = Buffer.from(buffer, encoding);
|
|
105
|
+
}
|
|
106
|
+
return Buffer.from(cryptoImpl.privateDecrypt(key, buffer, getPaddingAndHash(privateKey)));
|
|
107
|
+
}
|
|
108
|
+
export function publicEncrypt(key, buffer) {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
110
|
+
if (key === undefined) {
|
|
111
|
+
throw new ERR_MISSING_ARGS("key");
|
|
112
|
+
}
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
114
|
+
if (buffer === undefined) {
|
|
115
|
+
throw new ERR_MISSING_ARGS("buffer");
|
|
116
|
+
}
|
|
117
|
+
const pkey = (() => {
|
|
118
|
+
if (key instanceof CryptoKey) {
|
|
119
|
+
if (key.type !== "public") {
|
|
120
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
|
|
121
|
+
}
|
|
122
|
+
return key;
|
|
123
|
+
}
|
|
124
|
+
else if (isKeyObject(key)) {
|
|
125
|
+
if (key.type !== "public") {
|
|
126
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
|
|
127
|
+
}
|
|
128
|
+
return getKeyObjectHandle(key);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const pubKey = createPublicKey(key);
|
|
132
|
+
return getKeyObjectHandle(pubKey);
|
|
133
|
+
}
|
|
134
|
+
})();
|
|
135
|
+
if (typeof buffer === "string") {
|
|
136
|
+
const { encoding = "utf8" } = key;
|
|
137
|
+
buffer = Buffer.from(buffer, encoding);
|
|
138
|
+
}
|
|
139
|
+
return Buffer.from(cryptoImpl.publicEncrypt(pkey, buffer, getPaddingAndHash(key)));
|
|
140
|
+
}
|
|
141
|
+
export function publicDecrypt(key, buffer) {
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
143
|
+
if (key === undefined) {
|
|
144
|
+
throw new ERR_MISSING_ARGS("key");
|
|
145
|
+
}
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
147
|
+
if (buffer === undefined) {
|
|
148
|
+
throw new ERR_MISSING_ARGS("buffer");
|
|
149
|
+
}
|
|
150
|
+
const pkey = (() => {
|
|
151
|
+
if (key instanceof CryptoKey) {
|
|
152
|
+
if (key.type !== "public") {
|
|
153
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
|
|
154
|
+
}
|
|
155
|
+
return key;
|
|
156
|
+
}
|
|
157
|
+
else if (isKeyObject(key)) {
|
|
158
|
+
if (key.type !== "public") {
|
|
159
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
|
|
160
|
+
}
|
|
161
|
+
return getKeyObjectHandle(key);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const pubKey = createPublicKey(key);
|
|
165
|
+
return getKeyObjectHandle(pubKey);
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
if (typeof buffer === "string") {
|
|
169
|
+
const { encoding = "utf8" } = key;
|
|
170
|
+
buffer = Buffer.from(buffer, encoding);
|
|
171
|
+
}
|
|
172
|
+
return Buffer.from(cryptoImpl.publicDecrypt(pkey, buffer, getPaddingAndHash(key)));
|
|
173
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
type ArrayLike = any;
|
|
3
|
+
export interface ValidatedScryptOptions {
|
|
4
|
+
password: ArrayLike;
|
|
5
|
+
salt: ArrayLike;
|
|
6
|
+
keylen: number;
|
|
7
|
+
N: number;
|
|
8
|
+
r: number;
|
|
9
|
+
p: number;
|
|
10
|
+
maxmem: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ScryptOptions {
|
|
13
|
+
N?: number;
|
|
14
|
+
r?: number;
|
|
15
|
+
p?: number;
|
|
16
|
+
maxmem?: number;
|
|
17
|
+
cost?: number;
|
|
18
|
+
blockSize?: number;
|
|
19
|
+
parallelization?: number;
|
|
20
|
+
}
|
|
21
|
+
type Callback = (err: Error | null, derivedKey?: Buffer) => void;
|
|
22
|
+
type OptionsOrCallback = ScryptOptions | Callback;
|
|
23
|
+
export declare function scrypt(password: ArrayLike, salt: ArrayLike, keylen: number, options: OptionsOrCallback, callback?: OptionsOrCallback): void;
|
|
24
|
+
export declare function scryptSync(password: ArrayLike, salt: ArrayLike, keylen: number, options?: ScryptOptions): Buffer;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Copyright (c) 2017-2023 Cloudflare, Inc.
|
|
2
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
3
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
6
|
+
//
|
|
7
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
// copy of this software and associated documentation files (the
|
|
9
|
+
// "Software"), to deal in the Software without restriction, including
|
|
10
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
12
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
13
|
+
// following conditions:
|
|
14
|
+
//
|
|
15
|
+
// The above copyright notice and this permission notice shall be included
|
|
16
|
+
// in all copies or substantial portions of the Software.
|
|
17
|
+
//
|
|
18
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
19
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
21
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
22
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
23
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
24
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
// Validation/callback behavior from pinned workerd; algorithm from scrypt-js.
|
|
26
|
+
import { syncScrypt } from "scrypt-js";
|
|
27
|
+
import { Buffer } from "node:buffer";
|
|
28
|
+
import { validateInteger, bytes as getArrayBufferOrView, } from "./validation.js";
|
|
29
|
+
import { validateFunction, validateUint32 } from "../net/validators.js";
|
|
30
|
+
import { ERR_INVALID_ARG_VALUE } from "../net/errors.js";
|
|
31
|
+
const kMaxLength = 2147483647;
|
|
32
|
+
const cryptoImpl = {
|
|
33
|
+
getScrypt(password, salt, N, r, p, maxmem, length) {
|
|
34
|
+
if (N < 2 ||
|
|
35
|
+
(N & (N - 1)) !== 0 ||
|
|
36
|
+
r < 1 ||
|
|
37
|
+
p < 1 ||
|
|
38
|
+
128 * N * r + 128 * r * p + 256 * r > maxmem) {
|
|
39
|
+
throw new Error("Invalid scrypt params");
|
|
40
|
+
}
|
|
41
|
+
return new Uint8Array(syncScrypt(password, salt, N, r, p, length)).buffer;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const defaults = {
|
|
45
|
+
N: 16384,
|
|
46
|
+
r: 8,
|
|
47
|
+
p: 1,
|
|
48
|
+
maxmem: 32 << 20,
|
|
49
|
+
};
|
|
50
|
+
function validateParameters(password, salt, keylen, options) {
|
|
51
|
+
// TODO(soon): Add support for KeyObject input.
|
|
52
|
+
password = getArrayBufferOrView(password, "password");
|
|
53
|
+
salt = getArrayBufferOrView(salt, "salt");
|
|
54
|
+
if (password.byteLength >= kMaxLength) {
|
|
55
|
+
throw new ERR_INVALID_ARG_VALUE("password", password, "is too big");
|
|
56
|
+
}
|
|
57
|
+
if (salt.byteLength >= kMaxLength) {
|
|
58
|
+
throw new ERR_INVALID_ARG_VALUE("salt", salt, "is too big");
|
|
59
|
+
}
|
|
60
|
+
validateInteger(keylen, "keylen", 0, kMaxLength);
|
|
61
|
+
let { N, r, p, maxmem } = defaults;
|
|
62
|
+
if (options && options !== defaults) {
|
|
63
|
+
const has_N = options.N !== undefined;
|
|
64
|
+
if (has_N) {
|
|
65
|
+
N = options.N;
|
|
66
|
+
validateUint32(N, "N");
|
|
67
|
+
}
|
|
68
|
+
if (options.cost !== undefined) {
|
|
69
|
+
if (has_N)
|
|
70
|
+
throw new ERR_INVALID_ARG_VALUE("cost", options.cost);
|
|
71
|
+
N = options.cost;
|
|
72
|
+
validateUint32(N, "cost");
|
|
73
|
+
}
|
|
74
|
+
const has_r = options.r !== undefined;
|
|
75
|
+
if (has_r) {
|
|
76
|
+
r = options.r;
|
|
77
|
+
validateUint32(r, "r");
|
|
78
|
+
}
|
|
79
|
+
if (options.blockSize !== undefined) {
|
|
80
|
+
if (has_r)
|
|
81
|
+
throw new ERR_INVALID_ARG_VALUE("blockSize", options.blockSize);
|
|
82
|
+
r = options.blockSize;
|
|
83
|
+
validateUint32(r, "blockSize");
|
|
84
|
+
}
|
|
85
|
+
const has_p = options.p !== undefined;
|
|
86
|
+
if (has_p) {
|
|
87
|
+
p = options.p;
|
|
88
|
+
validateUint32(p, "p");
|
|
89
|
+
}
|
|
90
|
+
if (options.parallelization !== undefined) {
|
|
91
|
+
if (has_p)
|
|
92
|
+
throw new ERR_INVALID_ARG_VALUE("parallelization", options.parallelization);
|
|
93
|
+
p = options.parallelization;
|
|
94
|
+
validateUint32(p, "parallelization");
|
|
95
|
+
}
|
|
96
|
+
if (options.maxmem !== undefined) {
|
|
97
|
+
maxmem = options.maxmem;
|
|
98
|
+
validateInteger(maxmem, "maxmem", 0);
|
|
99
|
+
}
|
|
100
|
+
if (N === 0)
|
|
101
|
+
N = defaults.N;
|
|
102
|
+
if (r === 0)
|
|
103
|
+
r = defaults.r;
|
|
104
|
+
if (p === 0)
|
|
105
|
+
p = defaults.p;
|
|
106
|
+
if (maxmem === 0)
|
|
107
|
+
maxmem = defaults.maxmem;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
password,
|
|
111
|
+
salt,
|
|
112
|
+
keylen,
|
|
113
|
+
N: N,
|
|
114
|
+
r: r,
|
|
115
|
+
p: p,
|
|
116
|
+
maxmem: maxmem,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function scrypt(password, salt, keylen, options, callback = defaults) {
|
|
120
|
+
if (callback === defaults) {
|
|
121
|
+
callback = options;
|
|
122
|
+
options = defaults;
|
|
123
|
+
}
|
|
124
|
+
let N;
|
|
125
|
+
let r;
|
|
126
|
+
let p;
|
|
127
|
+
let maxmem;
|
|
128
|
+
({ password, salt, keylen, N, r, p, maxmem } = validateParameters(password, salt, keylen, options));
|
|
129
|
+
validateFunction(callback, "callback");
|
|
130
|
+
new Promise((res, rej) => {
|
|
131
|
+
try {
|
|
132
|
+
res(cryptoImpl.getScrypt(password, salt, N, r, p, maxmem, keylen));
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
rej(err);
|
|
136
|
+
}
|
|
137
|
+
}).then((val) => {
|
|
138
|
+
callback(null, Buffer.from(val));
|
|
139
|
+
}, (err) => {
|
|
140
|
+
callback(err);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
export function scryptSync(password, salt, keylen, options) {
|
|
144
|
+
let N;
|
|
145
|
+
let r;
|
|
146
|
+
let p;
|
|
147
|
+
let maxmem;
|
|
148
|
+
({ password, salt, keylen, N, r, p, maxmem } = validateParameters(password, salt, keylen, options));
|
|
149
|
+
return Buffer.from(cryptoImpl.getScrypt(password, salt, N, r, p, maxmem, keylen));
|
|
150
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NodeError } from "../net/error-base.js";
|
|
2
|
+
export { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE } from "../net/errors.js";
|
|
3
|
+
export declare class ERR_CRYPTO_SIGN_KEY_REQUIRED extends NodeError {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export declare class ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE extends NodeError {
|
|
7
|
+
constructor(actual: string, expected: string);
|
|
8
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Adapted from workerd internal_errors.ts (Apache-2.0 / Node MIT).
|
|
2
|
+
import { NodeError } from "../net/error-base.js";
|
|
3
|
+
export { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE } from "../net/errors.js";
|
|
4
|
+
export class ERR_CRYPTO_SIGN_KEY_REQUIRED extends NodeError {
|
|
5
|
+
constructor() {
|
|
6
|
+
super("ERR_CRYPTO_SIGN_KEY_REQUIRED", "No key provided to sign");
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE extends NodeError {
|
|
10
|
+
constructor(actual, expected) {
|
|
11
|
+
super("ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE", `Invalid key object type ${actual}, expected ${expected}.`);
|
|
12
|
+
}
|
|
13
|
+
}
|