react-native-quick-crypto 0.7.3 → 0.7.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/README.md +13 -46
- package/android/build.gradle +1 -1
- package/android/gradle.properties +1 -1
- package/lib/commonjs/Cipher.js +114 -112
- 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 +7 -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/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 +113 -111
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hash.js +7 -4
- package/lib/module/Hash.js.map +1 -1
- package/lib/module/Hashnames.js +7 -1
- package/lib/module/Hashnames.js.map +1 -1
- package/lib/module/Hmac.js +4 -2
- package/lib/module/Hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +7 -7
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -4
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/Utils.js +95 -62
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/aes.js +9 -8
- package/lib/module/aes.js.map +1 -1
- package/lib/module/ec.js +2 -3
- package/lib/module/ec.js.map +1 -1
- package/lib/module/keygen.js +4 -2
- package/lib/module/keygen.js.map +1 -1
- package/lib/module/keys.js +7 -2
- package/lib/module/keys.js.map +1 -1
- package/lib/module/pbkdf2.js +13 -21
- package/lib/module/pbkdf2.js.map +1 -1
- package/lib/module/random.js +6 -8
- package/lib/module/random.js.map +1 -1
- package/lib/module/rsa.js.map +1 -1
- package/lib/module/sig.js +0 -1
- package/lib/module/sig.js.map +1 -1
- package/lib/module/subtle.js +4 -3
- package/lib/module/subtle.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/{Hashnames.d.ts → src/Hashnames.d.ts} +2 -2
- 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 +3 -2
- 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} +9 -34
- 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} +30 -28
- 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 +14 -41
- package/src/Cipher.ts +272 -214
- package/src/Hash.ts +14 -7
- package/src/Hashnames.ts +10 -4
- 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 +6 -5
- package/src/Utils.ts +181 -108
- package/src/aes.ts +29 -29
- package/src/ec.ts +18 -19
- package/src/keygen.ts +7 -7
- package/src/keys.ts +73 -67
- 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/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/{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/keys.ts
CHANGED
|
@@ -20,25 +20,22 @@ export type NamedCurve = 'P-256' | 'P-384' | 'P-521';
|
|
|
20
20
|
export type ImportFormat = 'raw' | 'pkcs8' | 'spki' | 'jwk';
|
|
21
21
|
|
|
22
22
|
export type AnyAlgorithm =
|
|
23
|
+
| DigestAlgorithm
|
|
23
24
|
| HashAlgorithm
|
|
24
25
|
| KeyPairAlgorithm
|
|
25
26
|
| SecretKeyAlgorithm
|
|
26
27
|
| SignVerifyAlgorithm
|
|
27
28
|
| DeriveBitsAlgorithm
|
|
28
29
|
| EncryptDecryptAlgorithm
|
|
30
|
+
| AESAlgorithm
|
|
29
31
|
| 'PBKDF2'
|
|
30
|
-
| 'HKDF'
|
|
31
|
-
|
|
32
|
-
export type HashAlgorithm =
|
|
33
|
-
| 'SHA-1'
|
|
34
|
-
| 'SHA-224'
|
|
35
|
-
| 'SHA-256'
|
|
36
|
-
| 'SHA-384'
|
|
37
|
-
| 'SHA-512'
|
|
38
|
-
| 'RIPEMD-160';
|
|
32
|
+
| 'HKDF'
|
|
33
|
+
| 'unknown';
|
|
39
34
|
|
|
40
35
|
export type DigestAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
|
|
41
36
|
|
|
37
|
+
export type HashAlgorithm = DigestAlgorithm | 'SHA-224' | 'RIPEMD-160';
|
|
38
|
+
|
|
42
39
|
export type KeyPairType = 'rsa' | 'rsa-pss' | 'ec';
|
|
43
40
|
|
|
44
41
|
export type RSAKeyPairAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP';
|
|
@@ -126,7 +123,7 @@ export type SubtleAlgorithm = {
|
|
|
126
123
|
namedCurve?: NamedCurve;
|
|
127
124
|
length?: number;
|
|
128
125
|
modulusLength?: number;
|
|
129
|
-
publicExponent?:
|
|
126
|
+
publicExponent?: number | Uint8Array;
|
|
130
127
|
};
|
|
131
128
|
|
|
132
129
|
export type KeyUsage =
|
|
@@ -191,6 +188,7 @@ export enum KeyEncoding {
|
|
|
191
188
|
export type DSAEncoding = 'der' | 'ieee-p1363';
|
|
192
189
|
|
|
193
190
|
export type EncodingOptions = {
|
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
194
192
|
key?: any;
|
|
195
193
|
type?: KType;
|
|
196
194
|
encoding?: string;
|
|
@@ -198,35 +196,37 @@ export type EncodingOptions = {
|
|
|
198
196
|
format?: KFormat;
|
|
199
197
|
padding?: number;
|
|
200
198
|
cipher?: string;
|
|
201
|
-
passphrase?:
|
|
199
|
+
passphrase?: BinaryLike;
|
|
202
200
|
saltLength?: number;
|
|
201
|
+
oaepHash?: string;
|
|
202
|
+
oaepLabel?: BinaryLike;
|
|
203
203
|
};
|
|
204
204
|
|
|
205
205
|
export type AsymmetricKeyType = 'rsa' | 'rsa-pss' | 'dsa' | 'ec' | undefined;
|
|
206
206
|
|
|
207
207
|
export type JWK = {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
208
|
+
kty?: 'AES' | 'RSA' | 'EC' | 'oct';
|
|
209
|
+
use?: 'sig' | 'enc';
|
|
210
|
+
key_ops?: KeyUsage[];
|
|
211
|
+
alg?: string; // TODO: enumerate these (RFC-7517)
|
|
212
|
+
crv?: string;
|
|
213
|
+
kid?: string;
|
|
214
|
+
x5u?: string;
|
|
215
|
+
x5c?: string[];
|
|
216
|
+
x5t?: string;
|
|
217
217
|
'x5t#256'?: string;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
218
|
+
n?: string;
|
|
219
|
+
e?: string;
|
|
220
|
+
d?: string;
|
|
221
|
+
p?: string;
|
|
222
|
+
q?: string;
|
|
223
|
+
x?: string;
|
|
224
|
+
y?: string;
|
|
225
|
+
k?: string;
|
|
226
|
+
dp?: string;
|
|
227
|
+
dq?: string;
|
|
228
|
+
qi?: string;
|
|
229
|
+
ext?: boolean;
|
|
230
230
|
};
|
|
231
231
|
|
|
232
232
|
const encodingNames = {
|
|
@@ -257,7 +257,7 @@ function option(name: string, objName: string | undefined) {
|
|
|
257
257
|
function parseKeyFormat(
|
|
258
258
|
formatStr: string | undefined,
|
|
259
259
|
defaultFormat: KFormatType | undefined,
|
|
260
|
-
optionName?: string
|
|
260
|
+
optionName?: string,
|
|
261
261
|
) {
|
|
262
262
|
if (formatStr === undefined && defaultFormat !== undefined)
|
|
263
263
|
return defaultFormat;
|
|
@@ -273,14 +273,14 @@ function parseKeyType(
|
|
|
273
273
|
required: boolean,
|
|
274
274
|
keyType: string | undefined,
|
|
275
275
|
isPublic: boolean | undefined,
|
|
276
|
-
optionName: string
|
|
276
|
+
optionName: string,
|
|
277
277
|
): KeyEncoding | undefined {
|
|
278
278
|
if (typeStr === undefined && !required) {
|
|
279
279
|
return undefined;
|
|
280
280
|
} else if (typeStr === 'pkcs1') {
|
|
281
281
|
if (keyType !== undefined && keyType !== 'rsa') {
|
|
282
282
|
throw new Error(
|
|
283
|
-
`Crypto incompatible key options: ${typeStr} can only be used for RSA keys
|
|
283
|
+
`Crypto incompatible key options: ${typeStr} can only be used for RSA keys`,
|
|
284
284
|
);
|
|
285
285
|
}
|
|
286
286
|
return KeyEncoding.kKeyEncodingPKCS1;
|
|
@@ -291,7 +291,7 @@ function parseKeyType(
|
|
|
291
291
|
} else if (typeStr === 'sec1' && isPublic !== true) {
|
|
292
292
|
if (keyType !== undefined && keyType !== 'ec') {
|
|
293
293
|
throw new Error(
|
|
294
|
-
`Incompatible key options ${typeStr} can only be used for EC keys
|
|
294
|
+
`Incompatible key options ${typeStr} can only be used for EC keys`,
|
|
295
295
|
);
|
|
296
296
|
}
|
|
297
297
|
return KeyEncoding.kKeyEncodingSEC1;
|
|
@@ -304,7 +304,7 @@ function parseKeyFormatAndType(
|
|
|
304
304
|
enc: EncodingOptions,
|
|
305
305
|
keyType?: string,
|
|
306
306
|
isPublic?: boolean,
|
|
307
|
-
objName?: string
|
|
307
|
+
objName?: string,
|
|
308
308
|
) {
|
|
309
309
|
const { format: formatStr, type: typeStr } = enc;
|
|
310
310
|
|
|
@@ -312,7 +312,7 @@ function parseKeyFormatAndType(
|
|
|
312
312
|
const format = parseKeyFormat(
|
|
313
313
|
formatStr,
|
|
314
314
|
isInput ? KFormatType.kKeyFormatPEM : undefined,
|
|
315
|
-
option('format', objName)
|
|
315
|
+
option('format', objName),
|
|
316
316
|
);
|
|
317
317
|
|
|
318
318
|
const isRequired =
|
|
@@ -324,7 +324,7 @@ function parseKeyFormatAndType(
|
|
|
324
324
|
isRequired,
|
|
325
325
|
keyType,
|
|
326
326
|
isPublic,
|
|
327
|
-
option('type', objName)
|
|
327
|
+
option('type', objName),
|
|
328
328
|
);
|
|
329
329
|
return { format, type };
|
|
330
330
|
}
|
|
@@ -333,7 +333,7 @@ function parseKeyEncoding(
|
|
|
333
333
|
enc: EncodingOptions,
|
|
334
334
|
keyType?: string,
|
|
335
335
|
isPublic?: boolean,
|
|
336
|
-
objName?: string
|
|
336
|
+
objName?: string,
|
|
337
337
|
) {
|
|
338
338
|
// validateObject(enc, 'options');
|
|
339
339
|
|
|
@@ -343,7 +343,7 @@ function parseKeyEncoding(
|
|
|
343
343
|
enc,
|
|
344
344
|
keyType,
|
|
345
345
|
isPublic,
|
|
346
|
-
objName
|
|
346
|
+
objName,
|
|
347
347
|
);
|
|
348
348
|
|
|
349
349
|
let cipher, passphrase, encoding;
|
|
@@ -354,7 +354,7 @@ function parseKeyEncoding(
|
|
|
354
354
|
if (cipher != null) {
|
|
355
355
|
if (typeof cipher !== 'string')
|
|
356
356
|
throw new Error(
|
|
357
|
-
`Invalid argument ${option('cipher', objName)}: ${cipher}
|
|
357
|
+
`Invalid argument ${option('cipher', objName)}: ${cipher}`,
|
|
358
358
|
);
|
|
359
359
|
if (
|
|
360
360
|
format === KFormatType.kKeyFormatDER &&
|
|
@@ -362,12 +362,12 @@ function parseKeyEncoding(
|
|
|
362
362
|
type === KeyEncoding.kKeyEncodingSEC1)
|
|
363
363
|
) {
|
|
364
364
|
throw new Error(
|
|
365
|
-
`Incompatible key options ${encodingNames[type]} does not support encryption
|
|
365
|
+
`Incompatible key options ${encodingNames[type]} does not support encryption`,
|
|
366
366
|
);
|
|
367
367
|
}
|
|
368
368
|
} else if (passphrase !== undefined) {
|
|
369
369
|
throw new Error(
|
|
370
|
-
`invalid argument ${option('cipher', objName)}: ${cipher}
|
|
370
|
+
`invalid argument ${option('cipher', objName)}: ${cipher}`,
|
|
371
371
|
);
|
|
372
372
|
}
|
|
373
373
|
}
|
|
@@ -377,7 +377,7 @@ function parseKeyEncoding(
|
|
|
377
377
|
(!isInput && cipher != null && !isStringOrBuffer(passphrase))
|
|
378
378
|
) {
|
|
379
379
|
throw new Error(
|
|
380
|
-
`Invalid argument value ${option('passphrase', objName)}: ${passphrase}
|
|
380
|
+
`Invalid argument value ${option('passphrase', objName)}: ${passphrase}`,
|
|
381
381
|
);
|
|
382
382
|
}
|
|
383
383
|
}
|
|
@@ -390,12 +390,12 @@ function parseKeyEncoding(
|
|
|
390
390
|
|
|
391
391
|
function prepareAsymmetricKey(
|
|
392
392
|
key: BinaryLike | EncodingOptions,
|
|
393
|
-
ctx: KeyInputContext
|
|
393
|
+
ctx: KeyInputContext,
|
|
394
394
|
): {
|
|
395
395
|
format: KFormatType;
|
|
396
396
|
data: ArrayBuffer;
|
|
397
397
|
type?: KeyEncoding;
|
|
398
|
-
passphrase?:
|
|
398
|
+
passphrase?: BinaryLike;
|
|
399
399
|
} {
|
|
400
400
|
// TODO(osp) check, KeyObject some node object
|
|
401
401
|
// if (isKeyObject(key)) {
|
|
@@ -412,7 +412,7 @@ function prepareAsymmetricKey(
|
|
|
412
412
|
data: binaryLikeToArrayBuffer(key),
|
|
413
413
|
};
|
|
414
414
|
} else if (typeof key === 'object') {
|
|
415
|
-
const { key: data, encoding } = key;
|
|
415
|
+
const { key: data, encoding } = key as EncodingOptions;
|
|
416
416
|
// // The 'key' property can be a KeyObject as well to allow specifying
|
|
417
417
|
// // additional options such as padding along with the key.
|
|
418
418
|
// if (isKeyObject(data)) {
|
|
@@ -425,7 +425,7 @@ function prepareAsymmetricKey(
|
|
|
425
425
|
// Either PEM or DER using PKCS#1 or SPKI.
|
|
426
426
|
if (!isStringOrBuffer(data)) {
|
|
427
427
|
throw new Error(
|
|
428
|
-
'prepareAsymmetricKey: key is not a string or ArrayBuffer'
|
|
428
|
+
'prepareAsymmetricKey: key is not a string or ArrayBuffer',
|
|
429
429
|
);
|
|
430
430
|
}
|
|
431
431
|
|
|
@@ -437,7 +437,7 @@ function prepareAsymmetricKey(
|
|
|
437
437
|
|
|
438
438
|
return {
|
|
439
439
|
data: binaryLikeToArrayBuffer(data, encoding),
|
|
440
|
-
...parseKeyEncoding(key, undefined, isPublic),
|
|
440
|
+
...parseKeyEncoding(key as EncodingOptions, undefined, isPublic),
|
|
441
441
|
};
|
|
442
442
|
}
|
|
443
443
|
|
|
@@ -460,7 +460,7 @@ export function preparePublicOrPrivateKey(key: BinaryLike | EncodingOptions) {
|
|
|
460
460
|
export function parsePublicKeyEncoding(
|
|
461
461
|
enc: EncodingOptions,
|
|
462
462
|
keyType: string | undefined,
|
|
463
|
-
objName?: string
|
|
463
|
+
objName?: string,
|
|
464
464
|
) {
|
|
465
465
|
return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);
|
|
466
466
|
}
|
|
@@ -471,7 +471,7 @@ export function parsePublicKeyEncoding(
|
|
|
471
471
|
export function parsePrivateKeyEncoding(
|
|
472
472
|
enc: EncodingOptions,
|
|
473
473
|
keyType: string | undefined,
|
|
474
|
-
objName?: string
|
|
474
|
+
objName?: string,
|
|
475
475
|
) {
|
|
476
476
|
return parseKeyEncoding(enc, keyType, false, objName);
|
|
477
477
|
}
|
|
@@ -502,25 +502,25 @@ export function parsePrivateKeyEncoding(
|
|
|
502
502
|
function prepareSecretKey(
|
|
503
503
|
key: BinaryLike,
|
|
504
504
|
encoding?: string,
|
|
505
|
-
bufferOnly = false
|
|
506
|
-
):
|
|
505
|
+
bufferOnly = false,
|
|
506
|
+
): ArrayBuffer {
|
|
507
507
|
try {
|
|
508
508
|
if (!bufferOnly) {
|
|
509
509
|
// TODO: maybe use `key.constructor.name === 'KeyObject'` ?
|
|
510
510
|
if (key instanceof KeyObject) {
|
|
511
511
|
if (key.type !== 'secret')
|
|
512
512
|
throw new Error(
|
|
513
|
-
`invalid KeyObject type: ${key.type}, expected 'secret'
|
|
513
|
+
`invalid KeyObject type: ${key.type}, expected 'secret'`,
|
|
514
514
|
);
|
|
515
|
-
return key.handle;
|
|
515
|
+
return key.handle.export();
|
|
516
516
|
}
|
|
517
517
|
// TODO: maybe use `key.constructor.name === 'CryptoKey'` ?
|
|
518
518
|
else if (key instanceof CryptoKey) {
|
|
519
519
|
if (key.type !== 'secret')
|
|
520
520
|
throw new Error(
|
|
521
|
-
`invalid CryptoKey type: ${key.type}, expected 'secret'
|
|
521
|
+
`invalid CryptoKey type: ${key.type}, expected 'secret'`,
|
|
522
522
|
);
|
|
523
|
-
return key.keyObject.handle;
|
|
523
|
+
return key.keyObject.handle.export();
|
|
524
524
|
}
|
|
525
525
|
}
|
|
526
526
|
|
|
@@ -532,12 +532,15 @@ function prepareSecretKey(
|
|
|
532
532
|
} catch (error) {
|
|
533
533
|
throw new Error(
|
|
534
534
|
'Invalid argument type for "key". Need ArrayBuffer, TypedArray, KeyObject, CryptoKey, string',
|
|
535
|
-
{ cause: error }
|
|
535
|
+
{ cause: error },
|
|
536
536
|
);
|
|
537
537
|
}
|
|
538
538
|
}
|
|
539
539
|
|
|
540
|
-
export function createSecretKey(
|
|
540
|
+
export function createSecretKey(
|
|
541
|
+
key: BinaryLike,
|
|
542
|
+
encoding?: string,
|
|
543
|
+
): SecretKeyObject {
|
|
541
544
|
const k = prepareSecretKey(key, encoding, true);
|
|
542
545
|
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
543
546
|
handle.init(KeyType.Secret, k);
|
|
@@ -545,11 +548,11 @@ export function createSecretKey(key: any, encoding?: string) {
|
|
|
545
548
|
}
|
|
546
549
|
|
|
547
550
|
export function createPublicKey(
|
|
548
|
-
key: BinaryLike | EncodingOptions
|
|
551
|
+
key: BinaryLike | EncodingOptions,
|
|
549
552
|
): PublicKeyObject {
|
|
550
553
|
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
551
554
|
key,
|
|
552
|
-
KeyInputContext.kCreatePublic
|
|
555
|
+
KeyInputContext.kCreatePublic,
|
|
553
556
|
);
|
|
554
557
|
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
555
558
|
if (format === KFormatType.kKeyFormatJWK) {
|
|
@@ -561,11 +564,11 @@ export function createPublicKey(
|
|
|
561
564
|
}
|
|
562
565
|
|
|
563
566
|
export const createPrivateKey = (
|
|
564
|
-
key: BinaryLike | EncodingOptions
|
|
567
|
+
key: BinaryLike | EncodingOptions,
|
|
565
568
|
): PrivateKeyObject => {
|
|
566
569
|
const { format, type, data, passphrase } = prepareAsymmetricKey(
|
|
567
570
|
key,
|
|
568
|
-
KeyInputContext.kCreatePrivate
|
|
571
|
+
KeyInputContext.kCreatePrivate,
|
|
569
572
|
);
|
|
570
573
|
const handle = NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
571
574
|
if (format === KFormatType.kKeyFormatJWK) {
|
|
@@ -590,7 +593,7 @@ export class CryptoKey {
|
|
|
590
593
|
keyObject: KeyObject,
|
|
591
594
|
keyAlgorithm: SubtleAlgorithm,
|
|
592
595
|
keyUsages: KeyUsage[],
|
|
593
|
-
keyExtractable: boolean
|
|
596
|
+
keyExtractable: boolean,
|
|
594
597
|
) {
|
|
595
598
|
this.keyObject = keyObject;
|
|
596
599
|
this.keyAlgorithm = keyAlgorithm;
|
|
@@ -598,6 +601,7 @@ export class CryptoKey {
|
|
|
598
601
|
this.keyExtractable = keyExtractable;
|
|
599
602
|
}
|
|
600
603
|
|
|
604
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
|
|
601
605
|
inspect(_depth: number, _options: any): any {
|
|
602
606
|
throw new Error('CryptoKey.inspect is not implemented');
|
|
603
607
|
// if (depth < 0) return this;
|
|
@@ -639,6 +643,7 @@ export class CryptoKey {
|
|
|
639
643
|
class KeyObject {
|
|
640
644
|
handle: KeyObjectHandle;
|
|
641
645
|
type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
|
|
646
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
642
647
|
export(_options?: EncodingOptions): ArrayBuffer {
|
|
643
648
|
return new ArrayBuffer(0);
|
|
644
649
|
}
|
|
@@ -755,7 +760,7 @@ export class PublicKeyObject extends AsymmetricKeyObject {
|
|
|
755
760
|
}
|
|
756
761
|
const { format, type } = parsePublicKeyEncoding(
|
|
757
762
|
options,
|
|
758
|
-
this.asymmetricKeyType
|
|
763
|
+
this.asymmetricKeyType,
|
|
759
764
|
);
|
|
760
765
|
return this.handle.export(format, type);
|
|
761
766
|
}
|
|
@@ -776,12 +781,13 @@ export class PrivateKeyObject extends AsymmetricKeyObject {
|
|
|
776
781
|
}
|
|
777
782
|
const { format, type, cipher, passphrase } = parsePrivateKeyEncoding(
|
|
778
783
|
options,
|
|
779
|
-
this.asymmetricKeyType
|
|
784
|
+
this.asymmetricKeyType,
|
|
780
785
|
);
|
|
781
786
|
return this.handle.export(format, type, cipher, passphrase);
|
|
782
787
|
}
|
|
783
788
|
}
|
|
784
789
|
|
|
790
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
785
791
|
export const isCryptoKey = (obj: any): boolean => {
|
|
786
792
|
return obj !== null && obj?.keyObject !== undefined;
|
|
787
793
|
};
|
package/src/pbkdf2.ts
CHANGED
|
@@ -13,7 +13,8 @@ import { promisify } from 'util';
|
|
|
13
13
|
|
|
14
14
|
const WRONG_PASS =
|
|
15
15
|
'Password must be a string, a Buffer, a typed array or a DataView';
|
|
16
|
-
const WRONG_SALT =
|
|
16
|
+
const WRONG_SALT =
|
|
17
|
+
'Salt must be a string, a Buffer, a typed array or a DataView';
|
|
17
18
|
|
|
18
19
|
type Password = BinaryLike;
|
|
19
20
|
type Salt = BinaryLike;
|
|
@@ -21,64 +22,38 @@ type Pbkdf2Callback = (err: Error | null, derivedKey?: Buffer) => void;
|
|
|
21
22
|
|
|
22
23
|
function sanitizeInput(input: BinaryLike, errorMsg: string): ArrayBuffer {
|
|
23
24
|
try {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const ab = binaryLikeToArrayBuffer(input);
|
|
26
|
+
console.log('sanitized input', ab);
|
|
27
|
+
return ab;
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
29
|
+
} catch (_e: unknown) {
|
|
30
|
+
throw new Error(errorMsg);
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
|
|
30
|
-
const nativePbkdf2 = NativeQuickCrypto.pbkdf2;
|
|
31
|
-
|
|
32
34
|
export function pbkdf2(
|
|
33
35
|
password: Password,
|
|
34
36
|
salt: Salt,
|
|
35
37
|
iterations: number,
|
|
36
38
|
keylen: number,
|
|
37
|
-
digest:
|
|
38
|
-
callback: Pbkdf2Callback
|
|
39
|
-
): void;
|
|
40
|
-
export function pbkdf2(
|
|
41
|
-
password: Password,
|
|
42
|
-
salt: Salt,
|
|
43
|
-
iterations: number,
|
|
44
|
-
keylen: number,
|
|
45
|
-
callback: Pbkdf2Callback
|
|
46
|
-
): void;
|
|
47
|
-
export function pbkdf2(
|
|
48
|
-
password: Password,
|
|
49
|
-
salt: Salt,
|
|
50
|
-
iterations: number,
|
|
51
|
-
keylen: number,
|
|
52
|
-
arg0?: unknown,
|
|
53
|
-
arg1?: unknown
|
|
39
|
+
digest: string,
|
|
40
|
+
callback: Pbkdf2Callback,
|
|
54
41
|
): void {
|
|
55
|
-
|
|
56
|
-
let callback: undefined | Pbkdf2Callback;
|
|
57
|
-
if (typeof arg0 === 'string') {
|
|
58
|
-
digest = arg0 as HashAlgorithm;
|
|
59
|
-
if (typeof arg1 === 'function') {
|
|
60
|
-
callback = arg1 as Pbkdf2Callback;
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
if (typeof arg0 === 'function') {
|
|
64
|
-
callback = arg0 as Pbkdf2Callback;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (callback === undefined) {
|
|
42
|
+
if (callback === undefined || typeof callback !== 'function') {
|
|
68
43
|
throw new Error('No callback provided to pbkdf2');
|
|
69
44
|
}
|
|
70
|
-
|
|
71
45
|
const sanitizedPassword = sanitizeInput(password, WRONG_PASS);
|
|
72
46
|
const sanitizedSalt = sanitizeInput(salt, WRONG_SALT);
|
|
73
47
|
const normalizedDigest = normalizeHashName(digest, HashContext.Node);
|
|
74
48
|
|
|
49
|
+
const nativePbkdf2 = NativeQuickCrypto.pbkdf2;
|
|
75
50
|
nativePbkdf2
|
|
76
51
|
.pbkdf2(
|
|
77
52
|
sanitizedPassword,
|
|
78
53
|
sanitizedSalt,
|
|
79
54
|
iterations,
|
|
80
55
|
keylen,
|
|
81
|
-
normalizedDigest
|
|
56
|
+
normalizedDigest,
|
|
82
57
|
)
|
|
83
58
|
.then(
|
|
84
59
|
(res: ArrayBuffer) => {
|
|
@@ -86,7 +61,7 @@ export function pbkdf2(
|
|
|
86
61
|
},
|
|
87
62
|
(e: Error) => {
|
|
88
63
|
callback!(e);
|
|
89
|
-
}
|
|
64
|
+
},
|
|
90
65
|
);
|
|
91
66
|
}
|
|
92
67
|
|
|
@@ -95,18 +70,19 @@ export function pbkdf2Sync(
|
|
|
95
70
|
salt: Salt,
|
|
96
71
|
iterations: number,
|
|
97
72
|
keylen: number,
|
|
98
|
-
digest?:
|
|
73
|
+
digest?: string,
|
|
99
74
|
): ArrayBuffer {
|
|
100
75
|
const sanitizedPassword = sanitizeInput(password, WRONG_PASS);
|
|
101
76
|
const sanitizedSalt = sanitizeInput(salt, WRONG_SALT);
|
|
102
|
-
|
|
103
77
|
const algo = digest ? normalizeHashName(digest, HashContext.Node) : 'sha1';
|
|
104
|
-
|
|
78
|
+
|
|
79
|
+
const nativePbkdf2 = NativeQuickCrypto.pbkdf2;
|
|
80
|
+
const result: ArrayBuffer = nativePbkdf2.pbkdf2Sync(
|
|
105
81
|
sanitizedPassword,
|
|
106
82
|
sanitizedSalt,
|
|
107
83
|
iterations,
|
|
108
84
|
keylen,
|
|
109
|
-
algo
|
|
85
|
+
algo,
|
|
110
86
|
);
|
|
111
87
|
|
|
112
88
|
return Buffer.from(result);
|
|
@@ -120,14 +96,14 @@ const pbkdf2WithDigest = (
|
|
|
120
96
|
iterations: number,
|
|
121
97
|
keylen: number,
|
|
122
98
|
digest: HashAlgorithm,
|
|
123
|
-
callback: Pbkdf2Callback
|
|
99
|
+
callback: Pbkdf2Callback,
|
|
124
100
|
) => pbkdf2(password, salt, iterations, keylen, digest, callback);
|
|
125
101
|
|
|
126
102
|
const pbkdf2Promise = promisify(pbkdf2WithDigest);
|
|
127
103
|
export async function pbkdf2DeriveBits(
|
|
128
104
|
algorithm: SubtleAlgorithm,
|
|
129
105
|
baseKey: CryptoKey,
|
|
130
|
-
length: number
|
|
106
|
+
length: number,
|
|
131
107
|
): Promise<ArrayBuffer> {
|
|
132
108
|
const { iterations, hash, salt } = algorithm;
|
|
133
109
|
const normalizedHash = normalizeHashName(hash);
|
|
@@ -152,17 +128,17 @@ export async function pbkdf2DeriveBits(
|
|
|
152
128
|
|
|
153
129
|
const sanitizedPassword = sanitizeInput(raw, WRONG_PASS);
|
|
154
130
|
const sanitizedSalt = sanitizeInput(salt, WRONG_SALT);
|
|
155
|
-
|
|
131
|
+
const result: Buffer | undefined = await pbkdf2Promise(
|
|
156
132
|
sanitizedPassword,
|
|
157
133
|
sanitizedSalt,
|
|
158
134
|
iterations,
|
|
159
135
|
length / 8,
|
|
160
|
-
normalizedHash as HashAlgorithm
|
|
136
|
+
normalizedHash as HashAlgorithm,
|
|
161
137
|
);
|
|
162
138
|
if (!result) {
|
|
163
139
|
throw lazyDOMException(
|
|
164
140
|
'received bad result from pbkdf2()',
|
|
165
|
-
'OperationError'
|
|
141
|
+
'OperationError',
|
|
166
142
|
);
|
|
167
143
|
}
|
|
168
144
|
return bufferLikeToArrayBuffer(result);
|