cf-workers-native 0.1.5 → 0.1.6
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/README.md +2 -2
- package/dist/browser/documents/page-source.js +1 -1
- package/dist/browser/headers/index.d.ts +2 -0
- package/dist/browser/headers/index.js +5 -0
- package/dist/browser/response/index.d.ts +2 -0
- package/dist/browser/response/index.js +11 -3
- package/dist/browser/structured-clone/index.d.ts +3 -0
- package/dist/browser/structured-clone/index.js +75 -7
- package/dist/browser/websocket/attachment-size.d.ts +3 -0
- package/dist/browser/websocket/attachment-size.js +336 -0
- package/dist/browser/websocket/brand.d.ts +4 -0
- package/dist/browser/websocket/brand.js +16 -0
- package/dist/browser/websocket/close-event.d.ts +7 -0
- package/dist/browser/websocket/close-event.js +30 -0
- package/dist/browser/websocket/compression.d.ts +5 -0
- package/dist/browser/websocket/compression.js +124 -0
- package/dist/browser/websocket/conversion.d.ts +2 -0
- package/dist/browser/websocket/conversion.js +13 -0
- package/dist/browser/websocket/endpoint.d.ts +39 -0
- package/dist/browser/websocket/endpoint.js +379 -0
- package/dist/browser/websocket/frames.d.ts +21 -0
- package/dist/browser/websocket/frames.js +121 -0
- package/dist/browser/websocket/native.d.ts +14 -0
- package/dist/browser/websocket/native.js +226 -0
- package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +20 -0
- package/dist/compat/crypto/LICENSE.boringssl +238 -0
- package/dist/compat/crypto/LICENSE.ncrypto +21 -0
- package/dist/compat/crypto/asymmetric.js +141 -68
- package/dist/compat/crypto/cipher-handle.d.ts +27 -0
- package/dist/compat/crypto/cipher-handle.js +239 -0
- package/dist/compat/crypto/cipher-info.d.ts +12 -0
- package/dist/compat/crypto/cipher-info.js +324 -0
- package/dist/compat/crypto/constants.d.ts +62 -0
- package/dist/compat/crypto/constants.js +68 -0
- package/dist/compat/crypto/dh-handle.d.ts +17 -0
- package/dist/compat/crypto/dh-handle.js +180 -0
- package/dist/compat/crypto/dh.d.ts +53 -0
- package/dist/compat/crypto/dh.js +138 -0
- package/dist/compat/crypto/digest-stream.d.ts +15 -0
- package/dist/compat/crypto/digest-stream.js +213 -0
- package/dist/compat/crypto/digests.d.ts +3 -0
- package/dist/compat/crypto/digests.js +32 -0
- package/dist/compat/crypto/ecdh.d.ts +16 -6
- package/dist/compat/crypto/hash-handle.d.ts +23 -0
- package/dist/compat/crypto/hash-handle.js +70 -0
- package/dist/compat/crypto/hash.d.ts +37 -0
- package/dist/compat/crypto/hash.js +218 -0
- package/dist/compat/crypto/hkdf.js +40 -14
- package/dist/compat/crypto/index.d.cts +168 -1
- package/dist/compat/crypto/index.d.ts +191 -8
- package/dist/compat/crypto/index.js +36 -23
- package/dist/compat/crypto/key-errors.d.ts +10 -0
- package/dist/compat/crypto/key-errors.js +17 -0
- package/dist/compat/crypto/key-export.d.ts +8 -0
- package/dist/compat/crypto/key-export.js +49 -0
- package/dist/compat/crypto/key-formats.d.ts +2 -1
- package/dist/compat/crypto/key-formats.js +162 -31
- package/dist/compat/crypto/key-options.d.ts +1 -0
- package/dist/compat/crypto/key-options.js +74 -0
- package/dist/compat/crypto/key-state.d.ts +1 -0
- package/dist/compat/crypto/keys.d.ts +20 -12
- package/dist/compat/crypto/keys.js +125 -56
- package/dist/compat/crypto/metadata.d.ts +9 -0
- package/dist/compat/crypto/metadata.js +89 -2
- package/dist/compat/crypto/native-cipher.d.ts +17 -0
- package/dist/compat/crypto/native-cipher.js +192 -0
- package/dist/compat/crypto/native-crypto.d.ts +14 -1
- package/dist/compat/crypto/native-crypto.js +118 -6
- package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
- package/dist/compat/crypto/pbkdf2-handle.js +26 -0
- package/dist/compat/crypto/pbkdf2.d.ts +5 -0
- package/dist/compat/crypto/pbkdf2.js +60 -0
- package/dist/compat/crypto/prime.js +4 -4
- package/dist/compat/crypto/random.d.ts +11 -0
- package/dist/compat/crypto/random.js +86 -6
- package/dist/compat/crypto/rsa.d.ts +17 -1
- package/dist/compat/crypto/rsa.js +8 -4
- package/dist/compat/crypto/scrypt.d.ts +3 -3
- package/dist/compat/crypto/scrypt.js +25 -23
- package/dist/compat/crypto/sign-handle.d.ts +3 -3
- package/dist/compat/crypto/sign-handle.js +55 -11
- package/dist/compat/crypto/signatures.d.ts +17 -11
- package/dist/compat/crypto/subtle-digest.d.ts +1 -0
- package/dist/compat/crypto/subtle-digest.js +56 -0
- package/dist/compat/crypto/symmetric.d.ts +40 -24
- package/dist/compat/crypto/symmetric.js +229 -94
- package/dist/compat/crypto/validation.d.ts +4 -0
- package/dist/compat/crypto/validation.js +29 -2
- package/dist/compat/crypto/webcrypto.d.ts +13 -0
- package/dist/compat/crypto/webcrypto.js +51 -0
- package/dist/compat/crypto/x509.js +2 -1
- package/dist/compat/events/index.js +1 -1
- package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/fs/LICENSE-APACHE +177 -0
- package/dist/compat/fs/directory-backend.d.ts +6 -0
- package/dist/compat/fs/directory-backend.js +51 -0
- package/dist/compat/fs/directory.d.ts +37 -0
- package/dist/compat/fs/directory.js +137 -0
- package/dist/compat/fs/embedded.d.ts +1 -1
- package/dist/compat/fs/embedded.js +7 -2
- package/dist/compat/fs/file-handle.d.ts +75 -0
- package/dist/compat/fs/file-handle.js +226 -0
- package/dist/compat/fs/glob-internal.d.ts +15 -0
- package/dist/compat/fs/glob-internal.js +417 -0
- package/dist/compat/fs/glob.d.ts +10 -0
- package/dist/compat/fs/glob.js +71 -0
- package/dist/compat/fs/index.d.ts +6 -3
- package/dist/compat/fs/index.js +582 -50
- package/dist/compat/fs/io-validation.d.ts +14 -0
- package/dist/compat/fs/io-validation.js +130 -0
- package/dist/compat/fs/io.d.ts +1 -0
- package/dist/compat/fs/io.js +282 -0
- package/dist/compat/fs/promises/index.d.ts +3 -1
- package/dist/compat/fs/public-types.d.ts +120 -0
- package/dist/compat/fs/public-types.js +1 -0
- package/dist/compat/fs/stream-errors.d.ts +6 -0
- package/dist/compat/fs/stream-errors.js +8 -0
- package/dist/compat/fs/stream-utils.d.ts +16 -0
- package/dist/compat/fs/stream-utils.js +29 -0
- package/dist/compat/fs/streams.d.ts +104 -0
- package/dist/compat/fs/streams.js +794 -0
- package/dist/compat/fs/writable.d.ts +8 -0
- package/dist/compat/fs/writable.js +412 -22
- package/dist/compat/http/index.d.ts +11 -28
- package/dist/compat/http/index.js +81 -125
- package/dist/compat/http/server-options.d.ts +1 -0
- package/dist/compat/http/server-options.js +87 -0
- package/dist/compat/http/unique-headers.d.ts +1 -0
- package/dist/compat/http/unique-headers.js +21 -0
- package/dist/compat/http-outgoing/index.d.ts +2 -1
- package/dist/compat/http-outgoing/index.js +2 -17
- package/dist/compat/http-server/index.d.ts +2 -2
- package/dist/compat/http-server/index.js +4 -85
- package/dist/compat/https/index.d.ts +4 -2
- package/dist/compat/net/sockets.js +98 -20
- package/dist/compat/net/validators.d.ts +1 -0
- package/dist/compat/net/validators.js +8 -0
- package/dist/compat/process/configuration.d.ts +113 -0
- package/dist/compat/process/configuration.js +115 -0
- package/dist/compat/process/directory.d.ts +4 -0
- package/dist/compat/process/directory.js +28 -0
- package/dist/compat/process/files.d.ts +3 -0
- package/dist/compat/process/files.js +10 -0
- package/dist/compat/process/index.d.ts +15 -13
- package/dist/compat/process/index.js +47 -74
- package/dist/compat/process/scheduling.d.ts +2 -0
- package/dist/compat/process/scheduling.js +60 -0
- package/dist/compat/process/state.js +1 -0
- package/dist/compat/process/stdio.d.ts +18 -0
- package/dist/compat/process/stdio.js +66 -0
- package/dist/compat/stream/destroy.d.ts +1 -0
- package/dist/compat/stream/destroy.js +21 -1
- package/dist/compat/stream/readable.js +10 -2
- package/dist/compat/tls/jsstream.js +16 -6
- package/dist/compat/tls/wrap.d.ts +1 -1
- package/dist/compat/tls/wrap.js +0 -3
- package/dist/compat/util/inspect.js +5 -1
- package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/zlib/LICENSE-APACHE +177 -0
- package/dist/compat/zlib/LICENSE.zlib +22 -0
- package/dist/compat/zlib/classic-base.cjs +600 -0
- package/dist/compat/zlib/classic-base.d.cts +39 -0
- package/dist/compat/zlib/classic-binding.d.ts +28 -0
- package/dist/compat/zlib/classic-binding.js +105 -0
- package/dist/compat/zlib/classic.d.ts +44 -0
- package/dist/compat/zlib/classic.js +199 -0
- package/dist/compat/zlib/codec-binding.d.ts +33 -0
- package/dist/compat/zlib/codec-binding.js +141 -0
- package/dist/compat/zlib/compression.d.ts +22 -51
- package/dist/compat/zlib/compression.js +129 -218
- package/dist/compat/zlib/errors.d.ts +21 -0
- package/dist/compat/zlib/errors.js +32 -0
- package/dist/compat/zlib/index.d.ts +367 -3
- package/dist/compat/zlib/index.js +28 -11
- package/dist/compat/zlib/input.d.ts +16 -0
- package/dist/compat/zlib/input.js +132 -0
- package/dist/compat/zlib/native-codecs.d.ts +7 -0
- package/dist/compat/zlib/native-codecs.js +22 -10
- package/dist/compat/zlib/native-zlib.d.ts +5 -0
- package/dist/compat/zlib/native-zlib.js +52 -0
- package/dist/compat/zlib/public-types.d.ts +48 -0
- package/dist/compat/zlib/public-types.js +1 -0
- package/dist/compat/zlib/validators.d.ts +3 -0
- package/dist/compat/zlib/validators.js +18 -0
- package/dist/shared/buffer-source.d.ts +5 -0
- package/dist/shared/buffer-source.js +70 -0
- package/dist/shared/globals/platform.d.ts +8 -0
- package/dist/shared/globals/platform.js +1 -0
- package/dist/shared/globals/web.js +5 -0
- package/dist/shared/request-context.d.ts +2 -0
- package/dist/shared/request-context.js +11 -0
- package/dist/shared/request-scope.js +4 -1
- package/dist/tooling/shared.js +6 -0
- package/dist/tooling/vite.js +4 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +38 -7
- package/package.json +2 -2
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class NativeDH {
|
|
2
|
+
private pointer;
|
|
3
|
+
private verifyError;
|
|
4
|
+
constructor(prime: number | ArrayBuffer | ArrayBufferView, generator: number | ArrayBuffer | ArrayBufferView, group?: boolean);
|
|
5
|
+
static group(name: unknown): NativeDH;
|
|
6
|
+
getVerifyError(): number;
|
|
7
|
+
private get;
|
|
8
|
+
getPrime(): Uint8Array<ArrayBufferLike>;
|
|
9
|
+
getGenerator(): Uint8Array<ArrayBufferLike>;
|
|
10
|
+
getPublicKey(): Uint8Array<ArrayBufferLike>;
|
|
11
|
+
getPrivateKey(): Uint8Array<ArrayBufferLike>;
|
|
12
|
+
generateKeys(): Uint8Array<ArrayBufferLike>;
|
|
13
|
+
setPrivateKey(value: ArrayBuffer | ArrayBufferView): void;
|
|
14
|
+
setPublicKey(value: ArrayBuffer | ArrayBufferView): void;
|
|
15
|
+
private set;
|
|
16
|
+
computeSecret(value: ArrayBuffer | ArrayBufferView): Uint8Array;
|
|
17
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Implements pinned workerd dh.c++ using its BoringSSL backend.
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { inputs, library, output } from "./native-cipher.js";
|
|
4
|
+
import { bytes } from "./validation.js";
|
|
5
|
+
import { hasActiveRequest } from "../../shared/request-context.js";
|
|
6
|
+
const released = new FinalizationRegistry((pointer) => library().dh_free(pointer));
|
|
7
|
+
function integer(data) {
|
|
8
|
+
const hex = Buffer.from(data).toString("hex");
|
|
9
|
+
return hex ? BigInt("0x" + hex) : 0n;
|
|
10
|
+
}
|
|
11
|
+
function numeric(value) {
|
|
12
|
+
let hex = value.toString(16);
|
|
13
|
+
if (hex.length % 2)
|
|
14
|
+
hex = "0" + hex;
|
|
15
|
+
return Buffer.from(hex, "hex");
|
|
16
|
+
}
|
|
17
|
+
function peerError(code, operation) {
|
|
18
|
+
if (code === 0)
|
|
19
|
+
return;
|
|
20
|
+
const compute = operation === "computeSecret";
|
|
21
|
+
const prefix = `DiffieHellman ${operation}() failed: `;
|
|
22
|
+
if (code === -1)
|
|
23
|
+
throw new Error(prefix +
|
|
24
|
+
(compute
|
|
25
|
+
? "could not validate peer public key"
|
|
26
|
+
: "could not validate public key"));
|
|
27
|
+
if (code & 1)
|
|
28
|
+
throw new RangeError(prefix + (compute ? "Supplied key is too small" : "key is too small"));
|
|
29
|
+
if (code & 2)
|
|
30
|
+
throw new RangeError(prefix + (compute ? "Supplied key is too large" : "key is too large"));
|
|
31
|
+
throw new Error(prefix + (compute ? "invalid peer public key" : "invalid public key"));
|
|
32
|
+
}
|
|
33
|
+
export class NativeDH {
|
|
34
|
+
pointer;
|
|
35
|
+
verifyError;
|
|
36
|
+
constructor(prime, generator, group = false) {
|
|
37
|
+
const api = library();
|
|
38
|
+
let pointer = 0;
|
|
39
|
+
if (group)
|
|
40
|
+
pointer = api.dh_group(prime);
|
|
41
|
+
else if (typeof prime === "number") {
|
|
42
|
+
if (typeof generator !== "number")
|
|
43
|
+
throw new Error("DiffieHellman init failed: invalid parameters");
|
|
44
|
+
if (!hasActiveRequest())
|
|
45
|
+
throw new Error("DiffieHellman key generation requires an active request");
|
|
46
|
+
if (prime > 10000)
|
|
47
|
+
throw new RangeError("DiffieHellman init failed: requested prime size too large");
|
|
48
|
+
pointer = api.dh_parameters(prime, generator);
|
|
49
|
+
if (!pointer)
|
|
50
|
+
throw new Error("DiffieHellman init failed: could not generate parameters");
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const p = bytes(prime, "prime");
|
|
54
|
+
if (p.length > 1250)
|
|
55
|
+
throw new RangeError("DiffieHellman init failed: key is too large");
|
|
56
|
+
if (!p.length)
|
|
57
|
+
throw new Error("DiffieHellman init failed: invalid key");
|
|
58
|
+
let g;
|
|
59
|
+
if (typeof generator === "number") {
|
|
60
|
+
if (generator < 2)
|
|
61
|
+
throw new RangeError("DiffieHellman init failed: generator too small");
|
|
62
|
+
g = numeric(generator);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
g = bytes(generator, "generator");
|
|
66
|
+
if (g.length > 1250)
|
|
67
|
+
throw new RangeError("DiffieHellman init failed: generator is too large");
|
|
68
|
+
if (!g.length || integer(g) < 2n)
|
|
69
|
+
throw new Error("DiffieHellman init failed: invalid generator");
|
|
70
|
+
}
|
|
71
|
+
if (!(integer(p) & 1n) || integer(g) >= integer(p))
|
|
72
|
+
throw new Error("DiffieHellman init failed: Invalid DH prime generated");
|
|
73
|
+
pointer = inputs([p, g], (api, [pp, gg]) => api.dh_create(pp, p.length, gg, g.length));
|
|
74
|
+
if (!pointer)
|
|
75
|
+
throw new Error("DiffieHellman init failed: could not set keys");
|
|
76
|
+
}
|
|
77
|
+
if (!pointer)
|
|
78
|
+
throw new Error("DiffieHellmanGroup init failed: could not set keys");
|
|
79
|
+
try {
|
|
80
|
+
if (!api.dh_valid(pointer))
|
|
81
|
+
throw new Error("DiffieHellman init failed: Invalid DH prime generated");
|
|
82
|
+
// RFC 3526 primes are fixed safe primes. Preserve BoringSSL's generator
|
|
83
|
+
// suitability flag (including its p mod 24 rule) without re-testing primality.
|
|
84
|
+
const verify = group
|
|
85
|
+
? api.dh_group_check(pointer)
|
|
86
|
+
: api.dh_check(pointer);
|
|
87
|
+
if (verify < 0)
|
|
88
|
+
throw new Error("DiffieHellman init failed");
|
|
89
|
+
this.pointer = pointer;
|
|
90
|
+
this.verifyError = verify;
|
|
91
|
+
released.register(this, pointer, this);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
api.dh_free(pointer);
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
static group(name) {
|
|
99
|
+
const groups = {
|
|
100
|
+
modp14: 2048,
|
|
101
|
+
modp15: 3072,
|
|
102
|
+
modp16: 4096,
|
|
103
|
+
modp17: 6144,
|
|
104
|
+
modp18: 8192,
|
|
105
|
+
};
|
|
106
|
+
const normalized = `${name}`.split("\0")[0].toLowerCase();
|
|
107
|
+
const bits = Object.hasOwn(groups, normalized)
|
|
108
|
+
? groups[normalized]
|
|
109
|
+
: undefined;
|
|
110
|
+
if (!bits)
|
|
111
|
+
throw new Error("Failed to init DiffieHellmanGroup: invalid group. Only groups {modp14, modp15, modp16, modp17, modp18} are supported.");
|
|
112
|
+
return new NativeDH(bits, 2, true);
|
|
113
|
+
}
|
|
114
|
+
getVerifyError() {
|
|
115
|
+
return this.verifyError;
|
|
116
|
+
}
|
|
117
|
+
get(kind) {
|
|
118
|
+
const api = library(), length = api.dh_get(this.pointer, kind, 0);
|
|
119
|
+
if (length < 0)
|
|
120
|
+
throw new Error(kind === 2
|
|
121
|
+
? "No public key"
|
|
122
|
+
: kind === 3
|
|
123
|
+
? "No private key"
|
|
124
|
+
: kind === 0
|
|
125
|
+
? "No DiffieHellman prime"
|
|
126
|
+
: "No DiffieHellman generator");
|
|
127
|
+
return inputs([length], (api, [out]) => {
|
|
128
|
+
if (api.dh_get(this.pointer, kind, out) < 0)
|
|
129
|
+
throw new Error("Error while retrieving DiffieHellman key");
|
|
130
|
+
return output(api, out, length);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
getPrime() {
|
|
134
|
+
return this.get(0);
|
|
135
|
+
}
|
|
136
|
+
getGenerator() {
|
|
137
|
+
return this.get(1);
|
|
138
|
+
}
|
|
139
|
+
getPublicKey() {
|
|
140
|
+
return this.get(2);
|
|
141
|
+
}
|
|
142
|
+
getPrivateKey() {
|
|
143
|
+
return this.get(3);
|
|
144
|
+
}
|
|
145
|
+
generateKeys() {
|
|
146
|
+
if (!library().dh_generate(this.pointer))
|
|
147
|
+
throw new Error("internal error");
|
|
148
|
+
return this.getPublicKey();
|
|
149
|
+
}
|
|
150
|
+
setPrivateKey(value) {
|
|
151
|
+
this.set(value, true);
|
|
152
|
+
}
|
|
153
|
+
setPublicKey(value) {
|
|
154
|
+
this.set(value, false);
|
|
155
|
+
}
|
|
156
|
+
set(value, privateKey) {
|
|
157
|
+
const data = bytes(value, "key");
|
|
158
|
+
inputs([data], (api, [p]) => {
|
|
159
|
+
const code = api.dh_set(this.pointer, +privateKey, p, data.length);
|
|
160
|
+
if (!privateKey)
|
|
161
|
+
peerError(code, "setPublicKey");
|
|
162
|
+
else if (code !== 0)
|
|
163
|
+
throw new Error("internal error");
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
computeSecret(value) {
|
|
167
|
+
const data = bytes(value, "key");
|
|
168
|
+
if (data.length > 0x7fffffff)
|
|
169
|
+
throw new RangeError("DiffieHellman computeSecret() failed: key is too large");
|
|
170
|
+
if (!data.length)
|
|
171
|
+
throw new Error("DiffieHellman computeSecret() failed: invalid key");
|
|
172
|
+
return inputs([data, this.getPrime().length], (api, [p, out]) => {
|
|
173
|
+
peerError(api.dh_peer_check(this.pointer, p, data.length), "computeSecret");
|
|
174
|
+
const length = api.dh_compute(this.pointer, p, data.length, out);
|
|
175
|
+
if (length < 0)
|
|
176
|
+
throw new Error("DiffieHellman computeSecret() failed: error computing shared secret");
|
|
177
|
+
return output(api, out, length);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { NativeDH } from "./dh-handle.js";
|
|
3
|
+
declare const kHandle: unique symbol;
|
|
4
|
+
type Input = string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
|
|
5
|
+
type Encoding = BufferEncoding | "buffer";
|
|
6
|
+
interface SharedDH {
|
|
7
|
+
[kHandle]: NativeDH;
|
|
8
|
+
readonly verifyError: number;
|
|
9
|
+
generateKeys(encoding?: undefined): Buffer;
|
|
10
|
+
generateKeys(encoding: "buffer"): Buffer;
|
|
11
|
+
generateKeys(encoding: BufferEncoding): string;
|
|
12
|
+
computeSecret(key: Input, inputEncoding?: Encoding): Buffer;
|
|
13
|
+
computeSecret(key: Input, inputEncoding: Encoding | undefined, outputEncoding: "buffer"): Buffer;
|
|
14
|
+
computeSecret(key: Input, inputEncoding: Encoding | undefined, outputEncoding: BufferEncoding): string;
|
|
15
|
+
getPrime(encoding?: undefined): Buffer;
|
|
16
|
+
getPrime(encoding: "buffer"): Buffer;
|
|
17
|
+
getPrime(encoding: BufferEncoding): string;
|
|
18
|
+
getGenerator(encoding?: undefined): Buffer;
|
|
19
|
+
getGenerator(encoding: "buffer"): Buffer;
|
|
20
|
+
getGenerator(encoding: BufferEncoding): string;
|
|
21
|
+
getPublicKey(encoding?: undefined): Buffer;
|
|
22
|
+
getPublicKey(encoding: "buffer"): Buffer;
|
|
23
|
+
getPublicKey(encoding: BufferEncoding): string;
|
|
24
|
+
getPrivateKey(encoding?: undefined): Buffer;
|
|
25
|
+
getPrivateKey(encoding: "buffer"): Buffer;
|
|
26
|
+
getPrivateKey(encoding: BufferEncoding): string;
|
|
27
|
+
}
|
|
28
|
+
export interface DiffieHellman extends SharedDH {
|
|
29
|
+
setPrivateKey(key: Input, encoding?: Encoding): this;
|
|
30
|
+
setPublicKey(key: Input, encoding?: Encoding): this;
|
|
31
|
+
}
|
|
32
|
+
export interface DiffieHellmanGroup extends SharedDH {
|
|
33
|
+
}
|
|
34
|
+
type Arguments = [
|
|
35
|
+
sizeOrKey: number | Input,
|
|
36
|
+
keyEncoding?: number | Input,
|
|
37
|
+
generator?: number | Input,
|
|
38
|
+
genEncoding?: Encoding
|
|
39
|
+
];
|
|
40
|
+
export declare const DiffieHellman: {
|
|
41
|
+
new (...args: Arguments): DiffieHellman;
|
|
42
|
+
(...args: Arguments): DiffieHellman;
|
|
43
|
+
prototype: DiffieHellman;
|
|
44
|
+
};
|
|
45
|
+
export declare const DiffieHellmanGroup: {
|
|
46
|
+
new (name: string): DiffieHellmanGroup;
|
|
47
|
+
(name: string): DiffieHellmanGroup;
|
|
48
|
+
prototype: DiffieHellmanGroup;
|
|
49
|
+
};
|
|
50
|
+
export declare function createDiffieHellman(sizeOrKey: number | Input, keyEncoding?: number | Input, generator?: number | Input, genEncoding?: Encoding): DiffieHellman;
|
|
51
|
+
export declare function createDiffieHellmanGroup(name: string): DiffieHellmanGroup;
|
|
52
|
+
export declare function getDiffieHellman(name: string): DiffieHellmanGroup;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
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 { NativeDH } from "./dh-handle.js";
|
|
28
|
+
import { bytes, isAnyBuffer } from "./validation.js";
|
|
29
|
+
import { validateInt32 } from "../net/validators.js";
|
|
30
|
+
import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
|
|
31
|
+
const kHandle = Symbol("kHandle");
|
|
32
|
+
function decode(value, encoding) {
|
|
33
|
+
if (typeof value === "string")
|
|
34
|
+
return Buffer.from(value, encoding === "buffer" ? "utf8" : encoding);
|
|
35
|
+
return bytes(value, "key");
|
|
36
|
+
}
|
|
37
|
+
function encode(value, encoding) {
|
|
38
|
+
const result = Buffer.from(value);
|
|
39
|
+
return encoding && encoding !== "buffer" ? result.toString(encoding) : result;
|
|
40
|
+
}
|
|
41
|
+
export const DiffieHellman = function (sizeOrKey, keyEncoding, generator, genEncoding) {
|
|
42
|
+
if (!(this instanceof DiffieHellman))
|
|
43
|
+
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
|
|
44
|
+
if (typeof sizeOrKey !== "number" &&
|
|
45
|
+
typeof sizeOrKey !== "string" &&
|
|
46
|
+
!ArrayBuffer.isView(sizeOrKey) &&
|
|
47
|
+
!isAnyBuffer(sizeOrKey))
|
|
48
|
+
throw new ERR_INVALID_ARG_TYPE("sizeOrKey", ["number", "string", "ArrayBuffer", "Buffer", "TypedArray", "DataView"], sizeOrKey);
|
|
49
|
+
if (typeof sizeOrKey === "number")
|
|
50
|
+
validateInt32(sizeOrKey, "sizeOrKey");
|
|
51
|
+
if (keyEncoding &&
|
|
52
|
+
keyEncoding !== "buffer" &&
|
|
53
|
+
!Buffer.isEncoding(keyEncoding)) {
|
|
54
|
+
genEncoding = generator;
|
|
55
|
+
generator = keyEncoding;
|
|
56
|
+
keyEncoding = "utf8";
|
|
57
|
+
}
|
|
58
|
+
keyEncoding ??= "utf8";
|
|
59
|
+
genEncoding ??= "utf8";
|
|
60
|
+
if (typeof sizeOrKey !== "number")
|
|
61
|
+
sizeOrKey = decode(sizeOrKey, keyEncoding);
|
|
62
|
+
if (!generator)
|
|
63
|
+
generator = 2;
|
|
64
|
+
else if (typeof generator === "number")
|
|
65
|
+
validateInt32(generator, "generator");
|
|
66
|
+
else if (typeof generator === "string")
|
|
67
|
+
generator = decode(generator, genEncoding);
|
|
68
|
+
else if (!ArrayBuffer.isView(generator) && !isAnyBuffer(generator))
|
|
69
|
+
throw new ERR_INVALID_ARG_TYPE("generator", ["number", "string", "ArrayBuffer", "Buffer", "TypedArray", "DataView"], generator);
|
|
70
|
+
this[kHandle] = new NativeDH(sizeOrKey, generator);
|
|
71
|
+
Object.defineProperty(DiffieHellman.prototype, "verifyError", {
|
|
72
|
+
get() {
|
|
73
|
+
return this[kHandle].getVerifyError();
|
|
74
|
+
},
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
});
|
|
78
|
+
return this;
|
|
79
|
+
};
|
|
80
|
+
export const DiffieHellmanGroup = function (name) {
|
|
81
|
+
if (!(this instanceof DiffieHellmanGroup))
|
|
82
|
+
return new DiffieHellmanGroup(name);
|
|
83
|
+
this[kHandle] = NativeDH.group(name);
|
|
84
|
+
Object.defineProperty(DiffieHellmanGroup.prototype, "verifyError", {
|
|
85
|
+
get() {
|
|
86
|
+
return this[kHandle].getVerifyError();
|
|
87
|
+
},
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
});
|
|
91
|
+
return this;
|
|
92
|
+
};
|
|
93
|
+
const methods = {
|
|
94
|
+
generateKeys(encoding) {
|
|
95
|
+
return encode(this[kHandle].generateKeys(), encoding);
|
|
96
|
+
},
|
|
97
|
+
computeSecret(key, inputEncoding, outputEncoding) {
|
|
98
|
+
return encode(this[kHandle].computeSecret(decode(key, inputEncoding)), outputEncoding);
|
|
99
|
+
},
|
|
100
|
+
getPrime(encoding) {
|
|
101
|
+
return encode(this[kHandle].getPrime(), encoding);
|
|
102
|
+
},
|
|
103
|
+
getGenerator(encoding) {
|
|
104
|
+
return encode(this[kHandle].getGenerator(), encoding);
|
|
105
|
+
},
|
|
106
|
+
getPublicKey(encoding) {
|
|
107
|
+
return encode(this[kHandle].getPublicKey(), encoding);
|
|
108
|
+
},
|
|
109
|
+
getPrivateKey(encoding) {
|
|
110
|
+
return encode(this[kHandle].getPrivateKey(), encoding);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
const setters = {
|
|
114
|
+
setPublicKey(key, encoding) {
|
|
115
|
+
this[kHandle].setPublicKey(decode(key, encoding));
|
|
116
|
+
return this;
|
|
117
|
+
},
|
|
118
|
+
setPrivateKey(key, encoding) {
|
|
119
|
+
this[kHandle].setPrivateKey(decode(key, encoding));
|
|
120
|
+
return this;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
for (const [name, method] of Object.entries({ ...methods, ...setters }))
|
|
124
|
+
Object.defineProperty(method, "name", {
|
|
125
|
+
value: "dh" + name[0].toUpperCase() + name.slice(1),
|
|
126
|
+
configurable: true,
|
|
127
|
+
});
|
|
128
|
+
Object.assign(DiffieHellman.prototype, methods, setters);
|
|
129
|
+
Object.assign(DiffieHellmanGroup.prototype, methods);
|
|
130
|
+
export function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
|
|
131
|
+
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
|
|
132
|
+
}
|
|
133
|
+
export function createDiffieHellmanGroup(name) {
|
|
134
|
+
return new DiffieHellmanGroup(name);
|
|
135
|
+
}
|
|
136
|
+
export function getDiffieHellman(name) {
|
|
137
|
+
return createDiffieHellmanGroup(name);
|
|
138
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { WritableStream } from "../stream-web/runtime.js";
|
|
2
|
+
import { dispose } from "../../shared/globals/symbols.js";
|
|
3
|
+
type Chunk = ArrayBuffer | ArrayBufferView | string;
|
|
4
|
+
export declare class DigestStream extends WritableStream<Chunk> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(algorithm: string | {
|
|
7
|
+
name: string;
|
|
8
|
+
}, options?: {
|
|
9
|
+
toWellFormed?: boolean;
|
|
10
|
+
});
|
|
11
|
+
get digest(): Promise<ArrayBuffer>;
|
|
12
|
+
get bytesWritten(): bigint;
|
|
13
|
+
[dispose](): void;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Workers DigestStream contract, pinned to workerd 5358e3d337336eaf696a48bc1563d808b10feba5.
|
|
2
|
+
// Stream state and string conversion follow src/workerd/api/crypto/crypto.c++.
|
|
3
|
+
import { WritableStream } from "../stream-web/runtime.js";
|
|
4
|
+
import { dispose } from "../../shared/globals/symbols.js";
|
|
5
|
+
import { viewBytes } from "./validation.js";
|
|
6
|
+
import { NativeDigest } from "./native-crypto.js";
|
|
7
|
+
const crcTables = new Map();
|
|
8
|
+
class CrcDigest {
|
|
9
|
+
width;
|
|
10
|
+
value;
|
|
11
|
+
mask;
|
|
12
|
+
table;
|
|
13
|
+
constructor(width, polynomial) {
|
|
14
|
+
this.width = width;
|
|
15
|
+
this.mask = (1n << BigInt(width)) - 1n;
|
|
16
|
+
this.value = this.mask;
|
|
17
|
+
let table = crcTables.get(polynomial);
|
|
18
|
+
if (!table) {
|
|
19
|
+
table = Array.from({ length: 256 }, (_, n) => {
|
|
20
|
+
let v = BigInt(n);
|
|
21
|
+
for (let bit = 0; bit < 8; bit++)
|
|
22
|
+
v = (v >> 1n) ^ (v & 1n ? polynomial : 0n);
|
|
23
|
+
return v;
|
|
24
|
+
});
|
|
25
|
+
crcTables.set(polynomial, table);
|
|
26
|
+
}
|
|
27
|
+
this.table = table;
|
|
28
|
+
}
|
|
29
|
+
update(bytes) {
|
|
30
|
+
for (const byte of bytes)
|
|
31
|
+
this.value =
|
|
32
|
+
(this.value >> 8n) ^
|
|
33
|
+
this.table[Number((this.value ^ BigInt(byte)) & 255n)];
|
|
34
|
+
}
|
|
35
|
+
final() {
|
|
36
|
+
let value = this.value ^ this.mask;
|
|
37
|
+
const result = new Uint8Array(this.width / 8);
|
|
38
|
+
for (let index = result.length - 1; index >= 0; index--) {
|
|
39
|
+
result[index] = Number(value & 255n);
|
|
40
|
+
value >>= 8n;
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
dispose() {
|
|
45
|
+
this.value = 0n;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function context(name) {
|
|
49
|
+
name = name.split("\0")[0].toLowerCase();
|
|
50
|
+
switch (name) {
|
|
51
|
+
case "crc32":
|
|
52
|
+
return new CrcDigest(32, 0xedb88320n);
|
|
53
|
+
case "crc32c":
|
|
54
|
+
return new CrcDigest(32, 0x82f63b78n);
|
|
55
|
+
case "crc64nvme":
|
|
56
|
+
return new CrcDigest(64, 0x9a6c9329ac4bc9b5n);
|
|
57
|
+
case "sha-1":
|
|
58
|
+
case "sha-256":
|
|
59
|
+
case "sha-384":
|
|
60
|
+
case "sha-512":
|
|
61
|
+
case "md5":
|
|
62
|
+
return new NativeDigest(name.replace("-", ""));
|
|
63
|
+
default:
|
|
64
|
+
throw new DOMException("Unrecognized or unimplemented digest algorithm requested.", "NotSupportedError");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function isArrayBuffer(value) {
|
|
68
|
+
try {
|
|
69
|
+
Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get.call(value);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function encodeWtf8(text) {
|
|
77
|
+
const output = [];
|
|
78
|
+
for (let i = 0; i < text.length; i++) {
|
|
79
|
+
let code = text.charCodeAt(i);
|
|
80
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
81
|
+
const trail = text.charCodeAt(i + 1);
|
|
82
|
+
if (trail >= 0xdc00 && trail <= 0xdfff) {
|
|
83
|
+
code = 0x10000 + ((code - 0xd800) << 10) + trail - 0xdc00;
|
|
84
|
+
i++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (code < 0x80)
|
|
88
|
+
output.push(code);
|
|
89
|
+
else if (code < 0x800)
|
|
90
|
+
output.push(0xc0 | (code >> 6), 0x80 | (code & 63));
|
|
91
|
+
else if (code < 0x10000)
|
|
92
|
+
output.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
|
|
93
|
+
else
|
|
94
|
+
output.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 63), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
|
|
95
|
+
}
|
|
96
|
+
return Uint8Array.from(output);
|
|
97
|
+
}
|
|
98
|
+
function fail(state, reason) {
|
|
99
|
+
if (state.status !== "ready")
|
|
100
|
+
return;
|
|
101
|
+
state.status = "errored";
|
|
102
|
+
state.reason = reason;
|
|
103
|
+
state.context.dispose();
|
|
104
|
+
state.reject(reason);
|
|
105
|
+
}
|
|
106
|
+
export class DigestStream extends WritableStream {
|
|
107
|
+
#state;
|
|
108
|
+
constructor(algorithm, options) {
|
|
109
|
+
if (algorithm == null)
|
|
110
|
+
throw new TypeError("Cannot initialize N7workerd3api12SubtleCrypto13HashAlgorithmE with required members from an undefined or null value.");
|
|
111
|
+
const name = typeof algorithm === "object" || typeof algorithm === "function"
|
|
112
|
+
? `${algorithm.name}`
|
|
113
|
+
: `${algorithm}`;
|
|
114
|
+
if (options != null &&
|
|
115
|
+
typeof options !== "object" &&
|
|
116
|
+
typeof options !== "function")
|
|
117
|
+
throw new TypeError("Failed to construct 'DigestStream': constructor parameter 2 is not of type 'Options'.");
|
|
118
|
+
const wellFormed = !!options?.toWellFormed;
|
|
119
|
+
const native = context(name);
|
|
120
|
+
let resolve, reject;
|
|
121
|
+
const promise = new Promise((yes, no) => {
|
|
122
|
+
resolve = yes;
|
|
123
|
+
reject = no;
|
|
124
|
+
});
|
|
125
|
+
void promise.catch(() => { });
|
|
126
|
+
const state = {
|
|
127
|
+
context: native,
|
|
128
|
+
status: "ready",
|
|
129
|
+
reason: undefined,
|
|
130
|
+
promise,
|
|
131
|
+
resolve,
|
|
132
|
+
reject,
|
|
133
|
+
count: 0n,
|
|
134
|
+
pending: "",
|
|
135
|
+
};
|
|
136
|
+
const write = (bytes) => {
|
|
137
|
+
if (!bytes.length)
|
|
138
|
+
return;
|
|
139
|
+
if (state.status === "errored")
|
|
140
|
+
throw state.reason;
|
|
141
|
+
if (state.status === "closed")
|
|
142
|
+
return;
|
|
143
|
+
native.update(bytes);
|
|
144
|
+
state.count += BigInt(bytes.length);
|
|
145
|
+
};
|
|
146
|
+
super({
|
|
147
|
+
write(chunk) {
|
|
148
|
+
if (typeof chunk === "string") {
|
|
149
|
+
if (!wellFormed)
|
|
150
|
+
return write(encodeWtf8(chunk));
|
|
151
|
+
let text = state.pending + chunk;
|
|
152
|
+
state.pending = "";
|
|
153
|
+
const last = text.charCodeAt(text.length - 1);
|
|
154
|
+
if (last >= 0xd800 && last <= 0xdbff) {
|
|
155
|
+
state.pending = text.slice(-1);
|
|
156
|
+
text = text.slice(0, -1);
|
|
157
|
+
}
|
|
158
|
+
return write(new TextEncoder().encode(text));
|
|
159
|
+
}
|
|
160
|
+
if (!isArrayBuffer(chunk) && !ArrayBuffer.isView(chunk))
|
|
161
|
+
throw new TypeError("DigestStream is a byte stream but received an object of non-ArrayBuffer/ArrayBufferView/string type on its writable side.");
|
|
162
|
+
let bytes;
|
|
163
|
+
try {
|
|
164
|
+
bytes = isArrayBuffer(chunk)
|
|
165
|
+
? new Uint8Array(chunk)
|
|
166
|
+
: viewBytes(chunk);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return;
|
|
170
|
+
} // Detached/out-of-bounds sources have zero extent in Workers.
|
|
171
|
+
write(bytes);
|
|
172
|
+
},
|
|
173
|
+
close() {
|
|
174
|
+
if (state.status === "errored")
|
|
175
|
+
throw state.reason;
|
|
176
|
+
if (state.status === "closed")
|
|
177
|
+
return;
|
|
178
|
+
if (state.pending) {
|
|
179
|
+
write(new Uint8Array([0xef, 0xbf, 0xbd]));
|
|
180
|
+
state.pending = "";
|
|
181
|
+
}
|
|
182
|
+
const result = Uint8Array.from(native.final()).buffer;
|
|
183
|
+
native.dispose();
|
|
184
|
+
state.status = "closed";
|
|
185
|
+
state.resolve(result);
|
|
186
|
+
},
|
|
187
|
+
abort(reason) {
|
|
188
|
+
fail(state, reason);
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
this.#state = state;
|
|
192
|
+
}
|
|
193
|
+
get digest() {
|
|
194
|
+
if (this == null || typeof this !== "object" || !(#state in this))
|
|
195
|
+
throw new TypeError("Illegal invocation");
|
|
196
|
+
return this.#state.promise;
|
|
197
|
+
}
|
|
198
|
+
get bytesWritten() {
|
|
199
|
+
if (this == null || typeof this !== "object" || !(#state in this))
|
|
200
|
+
throw new TypeError("Illegal invocation");
|
|
201
|
+
return this.#state.count;
|
|
202
|
+
}
|
|
203
|
+
[dispose]() {
|
|
204
|
+
if (this == null || typeof this !== "object" || !(#state in this))
|
|
205
|
+
throw new TypeError("Illegal invocation");
|
|
206
|
+
fail(this.#state, new TypeError("The DigestStream was disposed."));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
Object.defineProperties(DigestStream.prototype, {
|
|
210
|
+
digest: { enumerable: true },
|
|
211
|
+
bytesWritten: { enumerable: true },
|
|
212
|
+
[Symbol.toStringTag]: { value: "DigestStream", configurable: true },
|
|
213
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const hashNames = [
|
|
2
|
+
"md4",
|
|
3
|
+
"md5",
|
|
4
|
+
"sha1",
|
|
5
|
+
"sha224",
|
|
6
|
+
"sha256",
|
|
7
|
+
"sha384",
|
|
8
|
+
"sha512",
|
|
9
|
+
"md5-sha1",
|
|
10
|
+
"RSA-MD5",
|
|
11
|
+
"RSA-SHA1",
|
|
12
|
+
"RSA-SHA224",
|
|
13
|
+
"RSA-SHA256",
|
|
14
|
+
"RSA-SHA384",
|
|
15
|
+
"RSA-SHA512",
|
|
16
|
+
"DSA-SHA",
|
|
17
|
+
"DSA-SHA1",
|
|
18
|
+
"ecdsa-with-SHA1",
|
|
19
|
+
];
|
|
20
|
+
export const aliases = Object.assign(Object.create(null), Object.fromEntries(hashNames.map((name) => [
|
|
21
|
+
name.toLowerCase(),
|
|
22
|
+
name
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/^rsa-/, "")
|
|
25
|
+
.replace(/^(dsa-sha1?|ecdsa-with-sha1)$/, "sha1"),
|
|
26
|
+
])));
|
|
27
|
+
export function signatureDigest(algorithm) {
|
|
28
|
+
const value = aliases[algorithm.split("\0")[0].toLowerCase()];
|
|
29
|
+
if (!value)
|
|
30
|
+
throw new Error(`Unknown digest: ${algorithm}`);
|
|
31
|
+
return value;
|
|
32
|
+
}
|