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.
Files changed (202) hide show
  1. package/README.md +13 -46
  2. package/cpp/Cipher/MGLPublicCipher.h +5 -1
  3. package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
  4. package/cpp/MGLQuickCryptoHostObject.cpp +8 -5
  5. package/lib/commonjs/Cipher.js +113 -111
  6. package/lib/commonjs/Cipher.js.map +1 -1
  7. package/lib/commonjs/Hash.js +8 -5
  8. package/lib/commonjs/Hash.js.map +1 -1
  9. package/lib/commonjs/Hashnames.js +4 -1
  10. package/lib/commonjs/Hashnames.js.map +1 -1
  11. package/lib/commonjs/Hmac.js +5 -3
  12. package/lib/commonjs/Hmac.js.map +1 -1
  13. package/lib/commonjs/NativeQuickCrypto/Cipher.js +7 -7
  14. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  15. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -4
  16. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  17. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  18. package/lib/commonjs/Utils.js +98 -63
  19. package/lib/commonjs/Utils.js.map +1 -1
  20. package/lib/commonjs/aes.js +9 -8
  21. package/lib/commonjs/aes.js.map +1 -1
  22. package/lib/commonjs/ec.js +2 -3
  23. package/lib/commonjs/ec.js.map +1 -1
  24. package/lib/commonjs/index.js +1 -1
  25. package/lib/commonjs/index.js.map +1 -1
  26. package/lib/commonjs/keygen.js +4 -2
  27. package/lib/commonjs/keygen.js.map +1 -1
  28. package/lib/commonjs/keys.js +7 -2
  29. package/lib/commonjs/keys.js.map +1 -1
  30. package/lib/commonjs/package.json +1 -0
  31. package/lib/commonjs/pbkdf2.js +13 -21
  32. package/lib/commonjs/pbkdf2.js.map +1 -1
  33. package/lib/commonjs/random.js +6 -8
  34. package/lib/commonjs/random.js.map +1 -1
  35. package/lib/commonjs/rsa.js.map +1 -1
  36. package/lib/commonjs/sig.js +1 -2
  37. package/lib/commonjs/sig.js.map +1 -1
  38. package/lib/commonjs/subtle.js +4 -3
  39. package/lib/commonjs/subtle.js.map +1 -1
  40. package/lib/module/Cipher.js +114 -110
  41. package/lib/module/Cipher.js.map +1 -1
  42. package/lib/module/Hash.js +9 -4
  43. package/lib/module/Hash.js.map +1 -1
  44. package/lib/module/Hashnames.js +6 -1
  45. package/lib/module/Hashnames.js.map +1 -1
  46. package/lib/module/Hmac.js +6 -2
  47. package/lib/module/Hmac.js.map +1 -1
  48. package/lib/module/NativeQuickCrypto/Cipher.js +9 -7
  49. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  50. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +13 -4
  51. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  52. package/lib/module/NativeQuickCrypto/aes.js +2 -0
  53. package/lib/module/NativeQuickCrypto/hash.js +1 -1
  54. package/lib/module/NativeQuickCrypto/hmac.js +1 -1
  55. package/lib/module/NativeQuickCrypto/keygen.js +2 -0
  56. package/lib/module/NativeQuickCrypto/pbkdf2.js +1 -1
  57. package/lib/module/NativeQuickCrypto/random.js +1 -1
  58. package/lib/module/NativeQuickCrypto/rsa.js +2 -0
  59. package/lib/module/NativeQuickCrypto/sig.js +2 -0
  60. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  61. package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
  62. package/lib/module/Utils.js +97 -62
  63. package/lib/module/Utils.js.map +1 -1
  64. package/lib/module/aes.js +11 -8
  65. package/lib/module/aes.js.map +1 -1
  66. package/lib/module/constants.js +2 -0
  67. package/lib/module/constants.js.map +1 -1
  68. package/lib/module/ec.js +4 -3
  69. package/lib/module/ec.js.map +1 -1
  70. package/lib/module/index.js +2 -0
  71. package/lib/module/index.js.map +1 -1
  72. package/lib/module/keygen.js +6 -2
  73. package/lib/module/keygen.js.map +1 -1
  74. package/lib/module/keys.js +9 -2
  75. package/lib/module/keys.js.map +1 -1
  76. package/lib/module/package.json +1 -0
  77. package/lib/module/pbkdf2.js +15 -21
  78. package/lib/module/pbkdf2.js.map +1 -1
  79. package/lib/module/random.js +8 -8
  80. package/lib/module/random.js.map +1 -1
  81. package/lib/module/rsa.js +2 -0
  82. package/lib/module/rsa.js.map +1 -1
  83. package/lib/module/sig.js +2 -1
  84. package/lib/module/sig.js.map +1 -1
  85. package/lib/module/subtle.js +6 -3
  86. package/lib/module/subtle.js.map +1 -1
  87. package/lib/module/webcrypto.js +2 -0
  88. package/lib/module/webcrypto.js.map +1 -1
  89. package/lib/typescript/{Cipher.d.ts → src/Cipher.d.ts} +37 -33
  90. package/lib/typescript/src/Cipher.d.ts.map +1 -0
  91. package/lib/typescript/src/Hash.d.ts.map +1 -0
  92. package/lib/typescript/src/Hashnames.d.ts.map +1 -0
  93. package/lib/typescript/src/Hmac.d.ts.map +1 -0
  94. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/Cipher.d.ts +5 -5
  95. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
  96. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +1 -1
  97. package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
  98. package/lib/typescript/src/NativeQuickCrypto/aes.d.ts.map +1 -0
  99. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
  100. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
  101. package/lib/typescript/src/NativeQuickCrypto/keygen.d.ts.map +1 -0
  102. package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
  103. package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
  104. package/lib/typescript/src/NativeQuickCrypto/rsa.d.ts.map +1 -0
  105. package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
  106. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/webcrypto.d.ts +1 -1
  107. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
  108. package/lib/typescript/{Utils.d.ts → src/Utils.d.ts} +18 -8
  109. package/lib/typescript/src/Utils.d.ts.map +1 -0
  110. package/lib/typescript/src/aes.d.ts.map +1 -0
  111. package/lib/typescript/src/constants.d.ts.map +1 -0
  112. package/lib/typescript/src/ec.d.ts.map +1 -0
  113. package/lib/typescript/{index.d.ts → src/index.d.ts} +5 -30
  114. package/lib/typescript/src/index.d.ts.map +1 -0
  115. package/lib/typescript/src/keygen.d.ts.map +1 -0
  116. package/lib/typescript/{keys.d.ts → src/keys.d.ts} +27 -25
  117. package/lib/typescript/src/keys.d.ts.map +1 -0
  118. package/lib/typescript/{pbkdf2.d.ts → src/pbkdf2.d.ts} +3 -4
  119. package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
  120. package/lib/typescript/{random.d.ts → src/random.d.ts} +5 -5
  121. package/lib/typescript/src/random.d.ts.map +1 -0
  122. package/lib/typescript/src/rsa.d.ts.map +1 -0
  123. package/lib/typescript/{sig.d.ts → src/sig.d.ts} +5 -5
  124. package/lib/typescript/src/sig.d.ts.map +1 -0
  125. package/lib/typescript/src/subtle.d.ts.map +1 -0
  126. package/lib/typescript/src/webcrypto.d.ts.map +1 -0
  127. package/package.json +23 -46
  128. package/src/Cipher.ts +270 -212
  129. package/src/Hash.ts +14 -7
  130. package/src/Hashnames.ts +5 -2
  131. package/src/Hmac.ts +6 -5
  132. package/src/NativeQuickCrypto/Cipher.ts +21 -20
  133. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +15 -11
  134. package/src/NativeQuickCrypto/aes.ts +1 -1
  135. package/src/NativeQuickCrypto/hash.ts +1 -1
  136. package/src/NativeQuickCrypto/hmac.ts +1 -1
  137. package/src/NativeQuickCrypto/keygen.ts +1 -1
  138. package/src/NativeQuickCrypto/pbkdf2.ts +2 -2
  139. package/src/NativeQuickCrypto/random.ts +2 -2
  140. package/src/NativeQuickCrypto/rsa.ts +1 -1
  141. package/src/NativeQuickCrypto/sig.ts +3 -1
  142. package/src/NativeQuickCrypto/webcrypto.ts +5 -5
  143. package/src/Utils.ts +175 -108
  144. package/src/aes.ts +29 -29
  145. package/src/ec.ts +18 -19
  146. package/src/keygen.ts +7 -7
  147. package/src/keys.ts +71 -65
  148. package/src/pbkdf2.ts +24 -48
  149. package/src/random.ts +37 -43
  150. package/src/rsa.ts +25 -23
  151. package/src/sig.ts +12 -13
  152. package/src/subtle.ts +50 -45
  153. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  154. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  155. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  156. package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  157. package/lib/typescript/Cipher.d.ts.map +0 -1
  158. package/lib/typescript/Hash.d.ts.map +0 -1
  159. package/lib/typescript/Hashnames.d.ts.map +0 -1
  160. package/lib/typescript/Hmac.d.ts.map +0 -1
  161. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  162. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  163. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  164. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  165. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  166. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  167. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  168. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  169. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  170. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  171. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  172. package/lib/typescript/Utils.d.ts.map +0 -1
  173. package/lib/typescript/aes.d.ts.map +0 -1
  174. package/lib/typescript/constants.d.ts.map +0 -1
  175. package/lib/typescript/ec.d.ts.map +0 -1
  176. package/lib/typescript/index.d.ts.map +0 -1
  177. package/lib/typescript/keygen.d.ts.map +0 -1
  178. package/lib/typescript/keys.d.ts.map +0 -1
  179. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  180. package/lib/typescript/random.d.ts.map +0 -1
  181. package/lib/typescript/rsa.d.ts.map +0 -1
  182. package/lib/typescript/sig.d.ts.map +0 -1
  183. package/lib/typescript/subtle.d.ts.map +0 -1
  184. package/lib/typescript/webcrypto.d.ts.map +0 -1
  185. /package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +0 -0
  186. /package/lib/typescript/{Hashnames.d.ts → src/Hashnames.d.ts} +0 -0
  187. /package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +0 -0
  188. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/aes.d.ts +0 -0
  189. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hash.d.ts +0 -0
  190. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hmac.d.ts +0 -0
  191. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/keygen.d.ts +0 -0
  192. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +0 -0
  193. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +0 -0
  194. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/rsa.d.ts +0 -0
  195. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +0 -0
  196. /package/lib/typescript/{aes.d.ts → src/aes.d.ts} +0 -0
  197. /package/lib/typescript/{constants.d.ts → src/constants.d.ts} +0 -0
  198. /package/lib/typescript/{ec.d.ts → src/ec.d.ts} +0 -0
  199. /package/lib/typescript/{keygen.d.ts → src/keygen.d.ts} +0 -0
  200. /package/lib/typescript/{rsa.d.ts → src/rsa.d.ts} +0 -0
  201. /package/lib/typescript/{subtle.d.ts → src/subtle.d.ts} +0 -0
  202. /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?: any;
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;
@@ -200,33 +198,35 @@ export type EncodingOptions = {
200
198
  cipher?: string;
201
199
  passphrase?: string | ArrayBuffer;
202
200
  saltLength?: number;
201
+ oaepHash?: ArrayBuffer;
202
+ oaepLabel?: ArrayBuffer;
203
203
  };
204
204
 
205
205
  export type AsymmetricKeyType = 'rsa' | 'rsa-pss' | 'dsa' | 'ec' | undefined;
206
206
 
207
207
  export type JWK = {
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;
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
- '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;
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,7 +390,7 @@ 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;
@@ -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
- ): any {
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(key: any, encoding?: string) {
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 = `Salt must be a string, a Buffer, a typed array or a DataView`;
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
- return binaryLikeToArrayBuffer(input);
25
- } catch (e: any) {
26
- throw errorMsg;
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: HashAlgorithm,
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
- let digest: HashAlgorithm = 'SHA-1';
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?: HashAlgorithm
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
- let result: ArrayBuffer = nativePbkdf2.pbkdf2Sync(
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
- let result: Buffer | undefined = await pbkdf2Promise(
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);