react-native-quick-crypto 0.7.2 → 0.7.4
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 +13 -46
- package/cpp/Cipher/MGLPublicCipher.h +5 -1
- package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
- package/cpp/MGLQuickCryptoHostObject.cpp +8 -5
- package/lib/commonjs/Cipher.js +113 -111
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/Hash.js +8 -5
- package/lib/commonjs/Hash.js.map +1 -1
- package/lib/commonjs/Hashnames.js +4 -1
- package/lib/commonjs/Hashnames.js.map +1 -1
- package/lib/commonjs/Hmac.js +5 -3
- package/lib/commonjs/Hmac.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +7 -7
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -4
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/commonjs/Utils.js +98 -63
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/aes.js +9 -8
- package/lib/commonjs/aes.js.map +1 -1
- package/lib/commonjs/ec.js +2 -3
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/index.js +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keygen.js +4 -2
- package/lib/commonjs/keygen.js.map +1 -1
- package/lib/commonjs/keys.js +7 -2
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/pbkdf2.js +13 -21
- package/lib/commonjs/pbkdf2.js.map +1 -1
- package/lib/commonjs/random.js +6 -8
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/rsa.js.map +1 -1
- package/lib/commonjs/sig.js +1 -2
- package/lib/commonjs/sig.js.map +1 -1
- package/lib/commonjs/subtle.js +4 -3
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/module/Cipher.js +114 -110
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hash.js +9 -4
- package/lib/module/Hash.js.map +1 -1
- package/lib/module/Hashnames.js +6 -1
- package/lib/module/Hashnames.js.map +1 -1
- package/lib/module/Hmac.js +6 -2
- package/lib/module/Hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +9 -7
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +13 -4
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/aes.js +2 -0
- package/lib/module/NativeQuickCrypto/hash.js +1 -1
- package/lib/module/NativeQuickCrypto/hmac.js +1 -1
- package/lib/module/NativeQuickCrypto/keygen.js +2 -0
- package/lib/module/NativeQuickCrypto/pbkdf2.js +1 -1
- package/lib/module/NativeQuickCrypto/random.js +1 -1
- package/lib/module/NativeQuickCrypto/rsa.js +2 -0
- package/lib/module/NativeQuickCrypto/sig.js +2 -0
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
- package/lib/module/Utils.js +97 -62
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/aes.js +11 -8
- package/lib/module/aes.js.map +1 -1
- package/lib/module/constants.js +2 -0
- package/lib/module/constants.js.map +1 -1
- package/lib/module/ec.js +4 -3
- package/lib/module/ec.js.map +1 -1
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/keygen.js +6 -2
- package/lib/module/keygen.js.map +1 -1
- package/lib/module/keys.js +9 -2
- package/lib/module/keys.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/module/pbkdf2.js +15 -21
- package/lib/module/pbkdf2.js.map +1 -1
- package/lib/module/random.js +8 -8
- package/lib/module/random.js.map +1 -1
- package/lib/module/rsa.js +2 -0
- package/lib/module/rsa.js.map +1 -1
- package/lib/module/sig.js +2 -1
- package/lib/module/sig.js.map +1 -1
- package/lib/module/subtle.js +6 -3
- package/lib/module/subtle.js.map +1 -1
- package/lib/module/webcrypto.js +2 -0
- package/lib/module/webcrypto.js.map +1 -1
- package/lib/typescript/{Cipher.d.ts → src/Cipher.d.ts} +37 -33
- package/lib/typescript/src/Cipher.d.ts.map +1 -0
- package/lib/typescript/src/Hash.d.ts.map +1 -0
- package/lib/typescript/src/Hashnames.d.ts.map +1 -0
- package/lib/typescript/src/Hmac.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/Cipher.d.ts +5 -5
- package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +1 -1
- package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/aes.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/keygen.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/rsa.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/webcrypto.d.ts +1 -1
- package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
- package/lib/typescript/{Utils.d.ts → src/Utils.d.ts} +18 -8
- package/lib/typescript/src/Utils.d.ts.map +1 -0
- package/lib/typescript/src/aes.d.ts.map +1 -0
- package/lib/typescript/src/constants.d.ts.map +1 -0
- package/lib/typescript/src/ec.d.ts.map +1 -0
- package/lib/typescript/{index.d.ts → src/index.d.ts} +5 -30
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/keygen.d.ts.map +1 -0
- package/lib/typescript/{keys.d.ts → src/keys.d.ts} +27 -25
- package/lib/typescript/src/keys.d.ts.map +1 -0
- package/lib/typescript/{pbkdf2.d.ts → src/pbkdf2.d.ts} +3 -4
- package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/{random.d.ts → src/random.d.ts} +5 -5
- package/lib/typescript/src/random.d.ts.map +1 -0
- package/lib/typescript/src/rsa.d.ts.map +1 -0
- package/lib/typescript/{sig.d.ts → src/sig.d.ts} +5 -5
- package/lib/typescript/src/sig.d.ts.map +1 -0
- package/lib/typescript/src/subtle.d.ts.map +1 -0
- package/lib/typescript/src/webcrypto.d.ts.map +1 -0
- package/package.json +23 -46
- package/src/Cipher.ts +270 -212
- package/src/Hash.ts +14 -7
- package/src/Hashnames.ts +5 -2
- package/src/Hmac.ts +6 -5
- package/src/NativeQuickCrypto/Cipher.ts +21 -20
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +15 -11
- package/src/NativeQuickCrypto/aes.ts +1 -1
- package/src/NativeQuickCrypto/hash.ts +1 -1
- package/src/NativeQuickCrypto/hmac.ts +1 -1
- package/src/NativeQuickCrypto/keygen.ts +1 -1
- package/src/NativeQuickCrypto/pbkdf2.ts +2 -2
- package/src/NativeQuickCrypto/random.ts +2 -2
- package/src/NativeQuickCrypto/rsa.ts +1 -1
- package/src/NativeQuickCrypto/sig.ts +3 -1
- package/src/NativeQuickCrypto/webcrypto.ts +5 -5
- package/src/Utils.ts +175 -108
- package/src/aes.ts +29 -29
- package/src/ec.ts +18 -19
- package/src/keygen.ts +7 -7
- package/src/keys.ts +71 -65
- package/src/pbkdf2.ts +24 -48
- package/src/random.ts +37 -43
- package/src/rsa.ts +25 -23
- package/src/sig.ts +12 -13
- package/src/subtle.ts +50 -45
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
- package/lib/typescript/Cipher.d.ts.map +0 -1
- package/lib/typescript/Hash.d.ts.map +0 -1
- package/lib/typescript/Hashnames.d.ts.map +0 -1
- package/lib/typescript/Hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
- package/lib/typescript/Utils.d.ts.map +0 -1
- package/lib/typescript/aes.d.ts.map +0 -1
- package/lib/typescript/constants.d.ts.map +0 -1
- package/lib/typescript/ec.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/keygen.d.ts.map +0 -1
- package/lib/typescript/keys.d.ts.map +0 -1
- package/lib/typescript/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/random.d.ts.map +0 -1
- package/lib/typescript/rsa.d.ts.map +0 -1
- package/lib/typescript/sig.d.ts.map +0 -1
- package/lib/typescript/subtle.d.ts.map +0 -1
- package/lib/typescript/webcrypto.d.ts.map +0 -1
- /package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +0 -0
- /package/lib/typescript/{Hashnames.d.ts → src/Hashnames.d.ts} +0 -0
- /package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/aes.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hash.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hmac.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/keygen.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/rsa.d.ts +0 -0
- /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +0 -0
- /package/lib/typescript/{aes.d.ts → src/aes.d.ts} +0 -0
- /package/lib/typescript/{constants.d.ts → src/constants.d.ts} +0 -0
- /package/lib/typescript/{ec.d.ts → src/ec.d.ts} +0 -0
- /package/lib/typescript/{keygen.d.ts → src/keygen.d.ts} +0 -0
- /package/lib/typescript/{rsa.d.ts → src/rsa.d.ts} +0 -0
- /package/lib/typescript/{subtle.d.ts → src/subtle.d.ts} +0 -0
- /package/lib/typescript/{webcrypto.d.ts → src/webcrypto.d.ts} +0 -0
package/src/Utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
2
|
+
import { Buffer as SBuffer } from 'safe-buffer';
|
|
2
3
|
import type {
|
|
3
4
|
AnyAlgorithm,
|
|
4
5
|
DeriveBitsAlgorithm,
|
|
@@ -13,8 +14,8 @@ import type {
|
|
|
13
14
|
} from './keys';
|
|
14
15
|
import { type CipherKey } from 'crypto'; // @types/node
|
|
15
16
|
|
|
16
|
-
export type BufferLike = ArrayBuffer | Buffer | ArrayBufferView;
|
|
17
|
-
export type BinaryLike = string | ArrayBuffer | Buffer | TypedArray;
|
|
17
|
+
export type BufferLike = ArrayBuffer | Buffer | SBuffer | ArrayBufferView;
|
|
18
|
+
export type BinaryLike = string | ArrayBuffer | Buffer | SBuffer | TypedArray;
|
|
18
19
|
export type BinaryLikeNode = CipherKey | BinaryLike;
|
|
19
20
|
|
|
20
21
|
export type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
|
|
@@ -28,6 +29,44 @@ export type Encoding =
|
|
|
28
29
|
// TODO(osp) should buffer be part of the Encoding type?
|
|
29
30
|
export type CipherEncoding = Encoding | 'buffer';
|
|
30
31
|
|
|
32
|
+
// These are for shortcomings in @types/node
|
|
33
|
+
// Here we use "*Type" instead of "*Types" like node does.
|
|
34
|
+
export type CipherType =
|
|
35
|
+
| 'aes128'
|
|
36
|
+
| 'aes192'
|
|
37
|
+
| 'aes256'
|
|
38
|
+
| CipherCBCType
|
|
39
|
+
| CipherCFBType
|
|
40
|
+
| CipherCTRType
|
|
41
|
+
| CipherDESType
|
|
42
|
+
| CipherECBType
|
|
43
|
+
| CipherGCMType
|
|
44
|
+
| CipherOFBType;
|
|
45
|
+
export type CipherCBCType = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
46
|
+
export type CipherCFBType =
|
|
47
|
+
| 'aes-128-cfb'
|
|
48
|
+
| 'aes-192-cfb'
|
|
49
|
+
| 'aes-256-cfb'
|
|
50
|
+
| 'aes-128-cfb1'
|
|
51
|
+
| 'aes-192-cfb1'
|
|
52
|
+
| 'aes-256-cfb1'
|
|
53
|
+
| 'aes-128-cfb8'
|
|
54
|
+
| 'aes-192-cfb8'
|
|
55
|
+
| 'aes-256-cfb8';
|
|
56
|
+
export type CipherCTRType = 'aes-128-ctr' | 'aes-192-ctr' | 'aes-256-ctr';
|
|
57
|
+
export type CipherDESType =
|
|
58
|
+
| 'des'
|
|
59
|
+
| 'des3'
|
|
60
|
+
| 'des-cbc'
|
|
61
|
+
| 'des-ecb'
|
|
62
|
+
| 'des-ede'
|
|
63
|
+
| 'des-ede-cbc'
|
|
64
|
+
| 'des-ede3'
|
|
65
|
+
| 'des-ede3-cbc';
|
|
66
|
+
export type CipherECBType = 'aes-128-ecb' | 'aes-192-ecb' | 'aes-256-ecb';
|
|
67
|
+
export type CipherGCMType = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
|
|
68
|
+
export type CipherOFBType = 'aes-128-ofb' | 'aes-192-ofb' | 'aes-256-ofb';
|
|
69
|
+
|
|
31
70
|
export type TypedArray =
|
|
32
71
|
| Uint8Array
|
|
33
72
|
| Uint8ClampedArray
|
|
@@ -43,7 +82,7 @@ type DOMName =
|
|
|
43
82
|
| string
|
|
44
83
|
| {
|
|
45
84
|
name: string;
|
|
46
|
-
cause:
|
|
85
|
+
cause: unknown;
|
|
47
86
|
};
|
|
48
87
|
|
|
49
88
|
// Mimics node behavior for default global encoding
|
|
@@ -130,34 +169,40 @@ export const kEmptyObject = Object.freeze(Object.create(null));
|
|
|
130
169
|
// return slowCases(enc);
|
|
131
170
|
// }
|
|
132
171
|
|
|
133
|
-
export function toArrayBuffer(buf: Buffer): ArrayBuffer {
|
|
134
|
-
if (buf?.buffer?.slice) {
|
|
172
|
+
export function toArrayBuffer(buf: Buffer | SBuffer): ArrayBuffer {
|
|
173
|
+
if (Buffer.isBuffer(buf) && buf?.buffer?.slice) {
|
|
135
174
|
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
136
175
|
}
|
|
137
176
|
const ab = new ArrayBuffer(buf.length);
|
|
138
177
|
const view = new Uint8Array(ab);
|
|
139
178
|
for (let i = 0; i < buf.length; ++i) {
|
|
140
|
-
view[i] = buf[i]!;
|
|
179
|
+
view[i] = SBuffer.isBuffer(buf) ? buf.readUInt8(i) : buf[i]!;
|
|
141
180
|
}
|
|
142
181
|
return ab;
|
|
143
182
|
}
|
|
144
183
|
|
|
145
184
|
export function bufferLikeToArrayBuffer(buf: BufferLike): ArrayBuffer {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
185
|
+
if (Buffer.isBuffer(buf)) {
|
|
186
|
+
return buf.buffer;
|
|
187
|
+
}
|
|
188
|
+
if (SBuffer.isBuffer(buf)) {
|
|
189
|
+
return toArrayBuffer(buf);
|
|
190
|
+
}
|
|
191
|
+
if (ArrayBuffer.isView(buf)) {
|
|
192
|
+
return buf.buffer;
|
|
193
|
+
}
|
|
194
|
+
return buf;
|
|
151
195
|
}
|
|
152
196
|
|
|
153
197
|
export function binaryLikeToArrayBuffer(
|
|
154
198
|
input: BinaryLikeNode, // CipherKey adds compat with node types
|
|
155
|
-
encoding: string = 'utf-8'
|
|
199
|
+
encoding: string = 'utf-8',
|
|
156
200
|
): ArrayBuffer {
|
|
201
|
+
// string
|
|
157
202
|
if (typeof input === 'string') {
|
|
158
203
|
if (encoding === 'buffer') {
|
|
159
204
|
throw new Error(
|
|
160
|
-
'Cannot create a buffer from a string with a buffer encoding'
|
|
205
|
+
'Cannot create a buffer from a string with a buffer encoding',
|
|
161
206
|
);
|
|
162
207
|
}
|
|
163
208
|
|
|
@@ -165,43 +210,51 @@ export function binaryLikeToArrayBuffer(
|
|
|
165
210
|
|
|
166
211
|
return buffer.buffer.slice(
|
|
167
212
|
buffer.byteOffset,
|
|
168
|
-
buffer.byteOffset + buffer.byteLength
|
|
213
|
+
buffer.byteOffset + buffer.byteLength,
|
|
169
214
|
);
|
|
170
215
|
}
|
|
171
216
|
|
|
217
|
+
// Buffer
|
|
172
218
|
if (Buffer.isBuffer(input)) {
|
|
173
219
|
return toArrayBuffer(input);
|
|
174
220
|
}
|
|
175
221
|
|
|
222
|
+
// ArrayBufferView
|
|
176
223
|
// TODO add further binary types to BinaryLike, UInt8Array and so for have this array as property
|
|
177
224
|
if (ArrayBuffer.isView(input)) {
|
|
178
225
|
return input.buffer;
|
|
179
226
|
}
|
|
180
227
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
// @ts-expect-error
|
|
185
|
-
const buffer = Buffer.from(input);
|
|
186
|
-
return buffer.buffer.slice(
|
|
187
|
-
buffer.byteOffset,
|
|
188
|
-
buffer.byteOffset + buffer.byteLength
|
|
189
|
-
);
|
|
190
|
-
} catch {
|
|
191
|
-
throw 'error';
|
|
192
|
-
}
|
|
228
|
+
// ArrayBuffer
|
|
229
|
+
if (input instanceof ArrayBuffer) {
|
|
230
|
+
return input;
|
|
193
231
|
}
|
|
194
232
|
|
|
233
|
+
// if (!(input instanceof ArrayBuffer)) {
|
|
234
|
+
// try {
|
|
235
|
+
// // this is a strange fallback case and input is unknown at this point
|
|
236
|
+
// const buffer = Buffer.from(input as unknown as string);
|
|
237
|
+
// return buffer.buffer.slice(
|
|
238
|
+
// buffer.byteOffset,
|
|
239
|
+
// buffer.byteOffset + buffer.byteLength
|
|
240
|
+
// );
|
|
241
|
+
// } catch(e: unknown) {
|
|
242
|
+
// console.log('throwing 1');
|
|
243
|
+
// const err = e as Error;
|
|
244
|
+
// throw new Error(err.message);
|
|
245
|
+
// }
|
|
246
|
+
// }
|
|
247
|
+
|
|
195
248
|
// TODO: handle if input is KeyObject?
|
|
196
249
|
|
|
197
|
-
|
|
250
|
+
throw new Error('input could not be converted to ArrayBuffer');
|
|
198
251
|
}
|
|
199
252
|
|
|
200
253
|
export function ab2str(buf: ArrayBuffer, encoding: string = 'hex') {
|
|
201
254
|
return Buffer.from(buf).toString(encoding);
|
|
202
255
|
}
|
|
203
256
|
|
|
204
|
-
export function validateString(str:
|
|
257
|
+
export function validateString(str: unknown, name?: string): str is string {
|
|
205
258
|
const isString = typeof str === 'string';
|
|
206
259
|
if (!isString) {
|
|
207
260
|
throw new Error(`${name} is not a string`);
|
|
@@ -209,11 +262,11 @@ export function validateString(str: any, name?: string): str is string {
|
|
|
209
262
|
return isString;
|
|
210
263
|
}
|
|
211
264
|
|
|
212
|
-
export function validateFunction(f:
|
|
265
|
+
export function validateFunction(f: unknown): boolean {
|
|
213
266
|
return f !== null && typeof f === 'function';
|
|
214
267
|
}
|
|
215
268
|
|
|
216
|
-
export function isStringOrBuffer(val:
|
|
269
|
+
export function isStringOrBuffer(val: unknown): val is string | ArrayBuffer {
|
|
217
270
|
return (
|
|
218
271
|
typeof val === 'string' ||
|
|
219
272
|
ArrayBuffer.isView(val) ||
|
|
@@ -222,13 +275,13 @@ export function isStringOrBuffer(val: any): val is string | ArrayBuffer {
|
|
|
222
275
|
}
|
|
223
276
|
|
|
224
277
|
export function validateObject<T>(
|
|
225
|
-
value:
|
|
278
|
+
value: unknown,
|
|
226
279
|
name: string,
|
|
227
280
|
options?: {
|
|
228
281
|
allowArray: boolean;
|
|
229
282
|
allowFunction: boolean;
|
|
230
283
|
nullable: boolean;
|
|
231
|
-
} | null
|
|
284
|
+
} | null,
|
|
232
285
|
): value is T {
|
|
233
286
|
const useDefaultOptions = options == null;
|
|
234
287
|
const allowArray = useDefaultOptions ? false : options.allowArray;
|
|
@@ -246,10 +299,11 @@ export function validateObject<T>(
|
|
|
246
299
|
}
|
|
247
300
|
|
|
248
301
|
export function validateInt32(
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
249
303
|
value: any,
|
|
250
304
|
name: string,
|
|
251
305
|
min = -2147483648,
|
|
252
|
-
max = 2147483647
|
|
306
|
+
max = 2147483647,
|
|
253
307
|
) {
|
|
254
308
|
// The defaults for min and max correspond to the limits of 32-bit integers.
|
|
255
309
|
if (typeof value !== 'number') {
|
|
@@ -257,12 +311,12 @@ export function validateInt32(
|
|
|
257
311
|
}
|
|
258
312
|
if (!Number.isInteger(value)) {
|
|
259
313
|
throw new Error(
|
|
260
|
-
`Argument out of range - ${name} out of integer range: ${value}
|
|
314
|
+
`Argument out of range - ${name} out of integer range: ${value}`,
|
|
261
315
|
);
|
|
262
316
|
}
|
|
263
317
|
if (value < min || value > max) {
|
|
264
318
|
throw new Error(
|
|
265
|
-
`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}
|
|
319
|
+
`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`,
|
|
266
320
|
);
|
|
267
321
|
}
|
|
268
322
|
}
|
|
@@ -270,7 +324,7 @@ export function validateInt32(
|
|
|
270
324
|
export function validateUint32(
|
|
271
325
|
value: number,
|
|
272
326
|
name: string,
|
|
273
|
-
positive?: boolean
|
|
327
|
+
positive?: boolean,
|
|
274
328
|
) {
|
|
275
329
|
if (typeof value !== 'number') {
|
|
276
330
|
// throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
|
@@ -279,7 +333,7 @@ export function validateUint32(
|
|
|
279
333
|
if (!Number.isInteger(value)) {
|
|
280
334
|
// throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
|
281
335
|
throw new Error(
|
|
282
|
-
`Argument out of range - ${name} out of integer range: ${value}
|
|
336
|
+
`Argument out of range - ${name} out of integer range: ${value}`,
|
|
283
337
|
);
|
|
284
338
|
}
|
|
285
339
|
const min = positive ? 1 : 0;
|
|
@@ -288,7 +342,7 @@ export function validateUint32(
|
|
|
288
342
|
if (value < min || value > max) {
|
|
289
343
|
// throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
|
|
290
344
|
throw new Error(
|
|
291
|
-
`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}
|
|
345
|
+
`Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`,
|
|
292
346
|
);
|
|
293
347
|
}
|
|
294
348
|
}
|
|
@@ -334,19 +388,19 @@ type SupportedAlgorithm<Type extends string> = {
|
|
|
334
388
|
};
|
|
335
389
|
|
|
336
390
|
type SupportedAlgorithms = {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
391
|
+
digest: SupportedAlgorithm<DigestAlgorithm>;
|
|
392
|
+
generateKey: SupportedAlgorithm<KeyPairAlgorithm | SecretKeyAlgorithm>;
|
|
393
|
+
sign: SupportedAlgorithm<SignVerifyAlgorithm>;
|
|
394
|
+
verify: SupportedAlgorithm<SignVerifyAlgorithm>;
|
|
395
|
+
importKey: SupportedAlgorithm<
|
|
342
396
|
KeyPairAlgorithm | 'PBKDF2' | SecretKeyAlgorithm | 'HKDF'
|
|
343
397
|
>;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
398
|
+
deriveBits: SupportedAlgorithm<DeriveBitsAlgorithm>;
|
|
399
|
+
encrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
|
|
400
|
+
decrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
|
|
347
401
|
'get key length': SupportedAlgorithm<SecretKeyAlgorithm | 'PBKDF2' | 'HKDF'>;
|
|
348
|
-
|
|
349
|
-
|
|
402
|
+
wrapKey: SupportedAlgorithm<'AES-KW'>;
|
|
403
|
+
unwrapKey: SupportedAlgorithm<'AES-KW'>;
|
|
350
404
|
};
|
|
351
405
|
|
|
352
406
|
export type Operation =
|
|
@@ -363,76 +417,76 @@ export type Operation =
|
|
|
363
417
|
| 'unwrapKey';
|
|
364
418
|
|
|
365
419
|
const kSupportedAlgorithms: SupportedAlgorithms = {
|
|
366
|
-
|
|
420
|
+
digest: {
|
|
367
421
|
'SHA-1': null,
|
|
368
422
|
'SHA-256': null,
|
|
369
423
|
'SHA-384': null,
|
|
370
424
|
'SHA-512': null,
|
|
371
425
|
},
|
|
372
|
-
|
|
426
|
+
generateKey: {
|
|
373
427
|
'RSASSA-PKCS1-v1_5': 'RsaHashedKeyGenParams',
|
|
374
428
|
'RSA-PSS': 'RsaHashedKeyGenParams',
|
|
375
429
|
'RSA-OAEP': 'RsaHashedKeyGenParams',
|
|
376
|
-
|
|
377
|
-
|
|
430
|
+
ECDSA: 'EcKeyGenParams',
|
|
431
|
+
ECDH: 'EcKeyGenParams',
|
|
378
432
|
'AES-CTR': 'AesKeyGenParams',
|
|
379
433
|
'AES-CBC': 'AesKeyGenParams',
|
|
380
434
|
'AES-GCM': 'AesKeyGenParams',
|
|
381
435
|
'AES-KW': 'AesKeyGenParams',
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
436
|
+
HMAC: 'HmacKeyGenParams',
|
|
437
|
+
X25519: null,
|
|
438
|
+
Ed25519: null,
|
|
439
|
+
X448: null,
|
|
440
|
+
Ed448: null,
|
|
387
441
|
},
|
|
388
|
-
|
|
442
|
+
sign: {
|
|
389
443
|
'RSASSA-PKCS1-v1_5': null,
|
|
390
444
|
'RSA-PSS': 'RsaPssParams',
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
445
|
+
ECDSA: 'EcdsaParams',
|
|
446
|
+
HMAC: null,
|
|
447
|
+
Ed25519: null,
|
|
448
|
+
Ed448: 'Ed448Params',
|
|
395
449
|
},
|
|
396
|
-
|
|
450
|
+
verify: {
|
|
397
451
|
'RSASSA-PKCS1-v1_5': null,
|
|
398
452
|
'RSA-PSS': 'RsaPssParams',
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
453
|
+
ECDSA: 'EcdsaParams',
|
|
454
|
+
HMAC: null,
|
|
455
|
+
Ed25519: null,
|
|
456
|
+
Ed448: 'Ed448Params',
|
|
403
457
|
},
|
|
404
|
-
|
|
458
|
+
importKey: {
|
|
405
459
|
'RSASSA-PKCS1-v1_5': 'RsaHashedImportParams',
|
|
406
460
|
'RSA-PSS': 'RsaHashedImportParams',
|
|
407
461
|
'RSA-OAEP': 'RsaHashedImportParams',
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
462
|
+
ECDSA: 'EcKeyImportParams',
|
|
463
|
+
ECDH: 'EcKeyImportParams',
|
|
464
|
+
HMAC: 'HmacImportParams',
|
|
465
|
+
HKDF: null,
|
|
466
|
+
PBKDF2: null,
|
|
413
467
|
'AES-CTR': null,
|
|
414
468
|
'AES-CBC': null,
|
|
415
469
|
'AES-GCM': null,
|
|
416
470
|
'AES-KW': null,
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
471
|
+
Ed25519: null,
|
|
472
|
+
X25519: null,
|
|
473
|
+
Ed448: null,
|
|
474
|
+
X448: null,
|
|
421
475
|
},
|
|
422
|
-
|
|
476
|
+
deriveBits: {
|
|
423
477
|
HKDF: 'HkdfParams',
|
|
424
478
|
PBKDF2: 'Pbkdf2Params',
|
|
425
479
|
ECDH: 'EcdhKeyDeriveParams',
|
|
426
480
|
X25519: 'EcdhKeyDeriveParams',
|
|
427
481
|
X448: 'EcdhKeyDeriveParams',
|
|
428
482
|
},
|
|
429
|
-
|
|
483
|
+
encrypt: {
|
|
430
484
|
'RSA-OAEP': 'RsaOaepParams',
|
|
431
485
|
'AES-CBC': 'AesCbcParams',
|
|
432
486
|
'AES-GCM': 'AesGcmParams',
|
|
433
487
|
'AES-CTR': 'AesCtrParams',
|
|
434
488
|
},
|
|
435
|
-
|
|
489
|
+
decrypt: {
|
|
436
490
|
'RSA-OAEP': 'RsaOaepParams',
|
|
437
491
|
'AES-CBC': 'AesCbcParams',
|
|
438
492
|
'AES-GCM': 'AesGcmParams',
|
|
@@ -443,20 +497,20 @@ const kSupportedAlgorithms: SupportedAlgorithms = {
|
|
|
443
497
|
'AES-CTR': 'AesDerivedKeyParams',
|
|
444
498
|
'AES-GCM': 'AesDerivedKeyParams',
|
|
445
499
|
'AES-KW': 'AesDerivedKeyParams',
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
500
|
+
HMAC: 'HmacImportParams',
|
|
501
|
+
HKDF: null,
|
|
502
|
+
PBKDF2: null,
|
|
449
503
|
},
|
|
450
|
-
|
|
504
|
+
wrapKey: {
|
|
451
505
|
'AES-KW': null,
|
|
452
506
|
},
|
|
453
|
-
|
|
507
|
+
unwrapKey: {
|
|
454
508
|
'AES-KW': null,
|
|
455
509
|
},
|
|
456
510
|
};
|
|
457
511
|
|
|
458
512
|
type AlgorithmDictionaries = {
|
|
459
|
-
[key in string]:
|
|
513
|
+
[key in string]: object;
|
|
460
514
|
};
|
|
461
515
|
|
|
462
516
|
const simpleAlgorithmDictionaries: AlgorithmDictionaries = {
|
|
@@ -481,13 +535,16 @@ const simpleAlgorithmDictionaries: AlgorithmDictionaries = {
|
|
|
481
535
|
|
|
482
536
|
export const validateMaxBufferLength = (
|
|
483
537
|
data: BinaryLike | BufferLike,
|
|
484
|
-
name: string
|
|
538
|
+
name: string,
|
|
485
539
|
): void => {
|
|
486
|
-
const length =
|
|
540
|
+
const length =
|
|
541
|
+
typeof data === 'string' || data instanceof SBuffer
|
|
542
|
+
? data.length
|
|
543
|
+
: data.byteLength;
|
|
487
544
|
if (length > kMaxBufferLength) {
|
|
488
545
|
throw lazyDOMException(
|
|
489
546
|
`${name} must be less than ${kMaxBufferLength + 1} bits`,
|
|
490
|
-
'OperationError'
|
|
547
|
+
'OperationError',
|
|
491
548
|
);
|
|
492
549
|
}
|
|
493
550
|
};
|
|
@@ -497,7 +554,7 @@ export const validateMaxBufferLength = (
|
|
|
497
554
|
// https://github.com/denoland/deno/blob/v1.29.1/ext/crypto/00_crypto.js#L195
|
|
498
555
|
export const normalizeAlgorithm = (
|
|
499
556
|
algorithm: SubtleAlgorithm | EncryptDecryptParams | AnyAlgorithm,
|
|
500
|
-
op: Operation
|
|
557
|
+
op: Operation,
|
|
501
558
|
): SubtleAlgorithm | EncryptDecryptParams => {
|
|
502
559
|
if (typeof algorithm === 'string') {
|
|
503
560
|
return normalizeAlgorithm({ name: algorithm }, op);
|
|
@@ -514,19 +571,19 @@ export const normalizeAlgorithm = (
|
|
|
514
571
|
|
|
515
572
|
// 4.
|
|
516
573
|
let algName = algorithm.name;
|
|
517
|
-
|
|
518
|
-
if (algName === undefined) return { name: undefined };
|
|
574
|
+
if (algName === undefined) return { name: 'unknown' };
|
|
519
575
|
|
|
520
576
|
// 5.
|
|
521
577
|
let desiredType: string | null | undefined;
|
|
522
578
|
for (const key in registeredAlgorithms) {
|
|
523
|
-
if (!
|
|
579
|
+
if (!Object.prototype.hasOwnProperty.call(registeredAlgorithms, key)) {
|
|
524
580
|
continue;
|
|
525
581
|
}
|
|
526
582
|
if (key.toUpperCase() === algName.toUpperCase()) {
|
|
527
583
|
algName = key as AnyAlgorithm;
|
|
528
|
-
|
|
529
|
-
|
|
584
|
+
desiredType = (
|
|
585
|
+
registeredAlgorithms as Record<string, typeof desiredType>
|
|
586
|
+
)[algName];
|
|
530
587
|
}
|
|
531
588
|
}
|
|
532
589
|
if (desiredType === undefined)
|
|
@@ -551,7 +608,7 @@ export const normalizeAlgorithm = (
|
|
|
551
608
|
const dictKeys = dict ? Object.keys(dict) : [];
|
|
552
609
|
for (let i = 0; i < dictKeys.length; i++) {
|
|
553
610
|
const member = dictKeys[i] || '';
|
|
554
|
-
if (!
|
|
611
|
+
if (!Object.prototype.hasOwnProperty.call(dict, member)) continue;
|
|
555
612
|
// TODO: implement this? Maybe via typescript?
|
|
556
613
|
// const idlType = dict[member];
|
|
557
614
|
// const idlValue = normalizedAlgorithm[member];
|
|
@@ -581,7 +638,7 @@ export const normalizeAlgorithm = (
|
|
|
581
638
|
export const validateBitLength = (
|
|
582
639
|
length: number,
|
|
583
640
|
name: string,
|
|
584
|
-
required: boolean = false
|
|
641
|
+
required: boolean = false,
|
|
585
642
|
) => {
|
|
586
643
|
if (length !== undefined || required) {
|
|
587
644
|
// validateNumber(length, name);
|
|
@@ -589,7 +646,7 @@ export const validateBitLength = (
|
|
|
589
646
|
if (length % 8) {
|
|
590
647
|
throw lazyDOMException(
|
|
591
648
|
`${name}'s length (${length}) must be a multiple of 8`,
|
|
592
|
-
'InvalidArgument'
|
|
649
|
+
'InvalidArgument',
|
|
593
650
|
);
|
|
594
651
|
}
|
|
595
652
|
}
|
|
@@ -598,12 +655,15 @@ export const validateBitLength = (
|
|
|
598
655
|
export const validateByteLength = (
|
|
599
656
|
buf: BufferLike,
|
|
600
657
|
name: string,
|
|
601
|
-
target: number
|
|
658
|
+
target: number,
|
|
602
659
|
) => {
|
|
603
|
-
if (
|
|
660
|
+
if (
|
|
661
|
+
(SBuffer.isBuffer(buf) && buf.length !== target) ||
|
|
662
|
+
(buf as Buffer | ArrayBuffer | ArrayBufferView).byteLength !== target
|
|
663
|
+
) {
|
|
604
664
|
throw lazyDOMException(
|
|
605
665
|
`${name} must contain exactly ${target} bytes`,
|
|
606
|
-
'OperationError'
|
|
666
|
+
'OperationError',
|
|
607
667
|
);
|
|
608
668
|
}
|
|
609
669
|
};
|
|
@@ -633,7 +693,7 @@ const kKeyOps: {
|
|
|
633
693
|
|
|
634
694
|
export const validateKeyOps = (
|
|
635
695
|
keyOps: KeyUsage[] | undefined,
|
|
636
|
-
usagesSet: KeyUsage[]
|
|
696
|
+
usagesSet: KeyUsage[],
|
|
637
697
|
) => {
|
|
638
698
|
if (keyOps === undefined) return;
|
|
639
699
|
if (!Array.isArray(keyOps)) {
|
|
@@ -646,10 +706,8 @@ export const validateKeyOps = (
|
|
|
646
706
|
// Skipping unknown key ops
|
|
647
707
|
if (op_flag === undefined) continue;
|
|
648
708
|
// Have we seen it already? if so, error
|
|
649
|
-
// eslint-disable-next-line no-bitwise
|
|
650
709
|
if (flags & (1 << op_flag))
|
|
651
710
|
throw lazyDOMException('Duplicate key operation', 'DataError');
|
|
652
|
-
// eslint-disable-next-line no-bitwise
|
|
653
711
|
flags |= 1 << op_flag;
|
|
654
712
|
|
|
655
713
|
// TODO(@jasnell): RFC7517 section 4.3 strong recommends validating
|
|
@@ -662,7 +720,7 @@ export const validateKeyOps = (
|
|
|
662
720
|
if (!keyOps.includes(use)) {
|
|
663
721
|
throw lazyDOMException(
|
|
664
722
|
'Key operations and usage mismatch',
|
|
665
|
-
'DataError'
|
|
723
|
+
'DataError',
|
|
666
724
|
);
|
|
667
725
|
}
|
|
668
726
|
}
|
|
@@ -677,21 +735,30 @@ export const validateKeyOps = (
|
|
|
677
735
|
// https://github.com/chromium/chromium/blob/HEAD/third_party/blink/public/platform/web_crypto_algorithm_params.h, but ported to JavaScript
|
|
678
736
|
// Returns undefined if the conversion was unsuccessful.
|
|
679
737
|
export const bigIntArrayToUnsignedInt = (
|
|
680
|
-
input: Uint8Array
|
|
738
|
+
input: Uint8Array,
|
|
681
739
|
): number | undefined => {
|
|
682
740
|
let result = 0;
|
|
683
741
|
|
|
684
742
|
for (let n = 0; n < input.length; ++n) {
|
|
685
743
|
const n_reversed = input.length - n - 1;
|
|
686
744
|
if (n_reversed >= 4 && input[n]) return; // Too large
|
|
687
|
-
// @ts-
|
|
688
|
-
// eslint-disable-next-line no-bitwise
|
|
745
|
+
// @ts-expect-error - input[n] is possibly undefined
|
|
689
746
|
result |= input[n] << (8 * n_reversed);
|
|
690
747
|
}
|
|
691
748
|
|
|
692
749
|
return result;
|
|
693
750
|
};
|
|
694
751
|
|
|
752
|
+
export function abvToArrayBuffer(buffer: ArrayBufferView): ArrayBuffer {
|
|
753
|
+
if (Buffer.isBuffer(buffer)) {
|
|
754
|
+
return buffer.buffer;
|
|
755
|
+
}
|
|
756
|
+
if (ArrayBuffer.isView(buffer)) {
|
|
757
|
+
return buffer.buffer;
|
|
758
|
+
}
|
|
759
|
+
return buffer;
|
|
760
|
+
}
|
|
761
|
+
|
|
695
762
|
// TODO: these used to be shipped by crypto-browserify in quickcrypto v0.6
|
|
696
763
|
// could instead fetch from OpenSSL if needed and handle breaking changes
|
|
697
764
|
export const getHashes = () => [
|