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/Cipher.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
2
- import Stream from 'readable-stream';
2
+ import Stream, { type TransformOptions } from 'readable-stream';
3
3
  import {
4
4
  type BinaryLike,
5
5
  binaryLikeToArrayBuffer,
@@ -13,6 +13,7 @@ import {
13
13
  validateUint32,
14
14
  validateInt32,
15
15
  type BinaryLikeNode,
16
+ type CipherType,
16
17
  } from './Utils';
17
18
  import { type InternalCipher, KeyVariant } from './NativeQuickCrypto/Cipher';
18
19
  import type {
@@ -35,11 +36,14 @@ import { Buffer as SBuffer } from 'safe-buffer';
35
36
  import { constants } from './constants';
36
37
  import {
37
38
  CryptoKey,
39
+ KeyEncoding,
40
+ KFormatType,
38
41
  parsePrivateKeyEncoding,
39
42
  parsePublicKeyEncoding,
40
43
  preparePrivateKey,
41
44
  preparePublicOrPrivateKey,
42
45
  type CryptoKeyPair,
46
+ type EncodingOptions,
43
47
  type KeyPairType,
44
48
  type NamedCurve,
45
49
  } from './keys';
@@ -59,11 +63,11 @@ const _publicEncrypt = NativeQuickCrypto.publicEncrypt;
59
63
  const _publicDecrypt = NativeQuickCrypto.publicDecrypt;
60
64
  const _privateDecrypt = NativeQuickCrypto.privateDecrypt;
61
65
 
66
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
67
  function getUIntOption(options: Record<string, any>, key: string) {
63
68
  let value;
64
69
  if (options && (value = options[key]) != null) {
65
70
  // >>> Turns any type into a positive integer (also sets the sign bit to 0)
66
- // eslint-disable-next-line no-bitwise
67
71
  if (value >>> 0 !== value) throw new Error(`options.${key}: ${value}`);
68
72
  return value;
69
73
  }
@@ -72,7 +76,7 @@ function getUIntOption(options: Record<string, any>, key: string) {
72
76
 
73
77
  function normalizeEncoding(enc: string) {
74
78
  if (!enc) return 'utf8';
75
- var retried;
79
+ let retried;
76
80
  while (true) {
77
81
  switch (enc) {
78
82
  case 'utf8':
@@ -119,8 +123,8 @@ class CipherCommon extends Stream.Transform {
119
123
  cipherType: string,
120
124
  cipherKey: BinaryLikeNode,
121
125
  isCipher: boolean,
122
- options: Record<string, any> = {},
123
- iv?: BinaryLike | null
126
+ options: Record<string, TransformOptions> = {},
127
+ iv?: BinaryLike | null,
124
128
  ) {
125
129
  super(options);
126
130
  const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey);
@@ -144,7 +148,7 @@ class CipherCommon extends Stream.Transform {
144
148
  update(
145
149
  data: BinaryLike,
146
150
  inputEncoding?: CipherEncoding,
147
- outputEncoding?: CipherEncoding
151
+ outputEncoding?: CipherEncoding,
148
152
  ): ArrayBuffer | string {
149
153
  const defaultEncoding = getDefaultEncoding();
150
154
  inputEncoding = inputEncoding ?? defaultEncoding;
@@ -162,14 +166,14 @@ class CipherCommon extends Stream.Transform {
162
166
  inputEncoding = inputEncoding === 'buffer' ? 'utf8' : inputEncoding;
163
167
  data = binaryLikeToArrayBuffer(data, inputEncoding);
164
168
  } else {
165
- data = binaryLikeToArrayBuffer(data as any, inputEncoding);
169
+ data = binaryLikeToArrayBuffer(data as BinaryLikeNode, inputEncoding);
166
170
  }
167
171
 
168
172
  const ret = this.internal.update(data);
169
173
 
170
174
  if (outputEncoding && outputEncoding !== 'buffer') {
171
175
  this.decoder = getDecoder(this.decoder, outputEncoding);
172
-
176
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
173
177
  return this.decoder!.write(SBuffer.from(ret) as any);
174
178
  }
175
179
 
@@ -183,7 +187,7 @@ class CipherCommon extends Stream.Transform {
183
187
 
184
188
  if (outputEncoding && outputEncoding !== 'buffer') {
185
189
  this.decoder = getDecoder(this.decoder, outputEncoding);
186
-
190
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
187
191
  return this.decoder!.end(SBuffer.from(ret) as any);
188
192
  }
189
193
 
@@ -209,7 +213,7 @@ class CipherCommon extends Stream.Transform {
209
213
  buffer: Buffer,
210
214
  options?: {
211
215
  plaintextLength: number;
212
- }
216
+ },
213
217
  ): this {
214
218
  this.internal.setAAD({
215
219
  data: buffer.buffer,
@@ -232,8 +236,8 @@ class Cipher extends CipherCommon {
232
236
  constructor(
233
237
  cipherType: string,
234
238
  cipherKey: BinaryLikeNode,
235
- options: Record<string, any> = {},
236
- iv?: BinaryLike | null
239
+ options: Record<string, TransformOptions> = {},
240
+ iv?: BinaryLike | null,
237
241
  ) {
238
242
  if (iv != null) {
239
243
  iv = binaryLikeToArrayBuffer(iv);
@@ -246,8 +250,8 @@ class Decipher extends CipherCommon {
246
250
  constructor(
247
251
  cipherType: string,
248
252
  cipherKey: BinaryLikeNode,
249
- options: Record<string, any> = {},
250
- iv?: BinaryLike | null
253
+ options: Record<string, TransformOptions> = {},
254
+ iv?: BinaryLike | null,
251
255
  ) {
252
256
  if (iv != null) {
253
257
  iv = binaryLikeToArrayBuffer(iv);
@@ -260,39 +264,55 @@ class Decipher extends CipherCommon {
260
264
  export function createDecipher(
261
265
  algorithm: CipherCCMTypes,
262
266
  password: BinaryLikeNode,
263
- options: CipherCCMOptions
267
+ options: CipherCCMOptions,
264
268
  ): DecipherCCM;
265
269
  export function createDecipher(
266
270
  algorithm: CipherGCMTypes,
267
271
  password: BinaryLikeNode,
268
- options?: CipherGCMOptions
272
+ options?: CipherGCMOptions,
269
273
  ): DecipherGCM;
274
+ export function createDecipher(
275
+ algorithm: CipherType,
276
+ password: BinaryLikeNode,
277
+ options?: Stream.TransformOptions,
278
+ ): DecipherCCM | DecipherGCM | Decipher;
270
279
  export function createDecipher(
271
280
  algorithm: string,
272
281
  password: BinaryLikeNode,
273
- options?: CipherCCMOptions | CipherGCMOptions | Stream.TransformOptions
282
+ options?: CipherCCMOptions | CipherGCMOptions | Stream.TransformOptions,
274
283
  ): DecipherCCM | DecipherGCM | Decipher {
275
- return new Decipher(algorithm, password, options);
284
+ if (options === undefined) options = {};
285
+ return new Decipher(
286
+ algorithm,
287
+ password,
288
+ options as Record<string, TransformOptions>,
289
+ );
276
290
  }
277
291
 
278
292
  export function createDecipheriv(
279
293
  algorithm: CipherCCMTypes,
280
294
  key: BinaryLikeNode,
281
295
  iv: BinaryLike,
282
- options: CipherCCMOptions
296
+ options: CipherCCMOptions,
283
297
  ): DecipherCCM;
284
298
  export function createDecipheriv(
285
299
  algorithm: CipherOCBTypes,
286
300
  key: BinaryLikeNode,
287
301
  iv: BinaryLike,
288
- options: CipherOCBOptions
302
+ options: CipherOCBOptions,
289
303
  ): DecipherOCB;
290
304
  export function createDecipheriv(
291
305
  algorithm: CipherGCMTypes,
292
306
  key: BinaryLikeNode,
293
307
  iv: BinaryLike,
294
- options?: CipherGCMOptions
308
+ options?: CipherGCMOptions,
295
309
  ): DecipherGCM;
310
+ export function createDecipheriv(
311
+ algorithm: CipherType,
312
+ key: BinaryLikeNode,
313
+ iv: BinaryLike | null,
314
+ options?: Stream.TransformOptions,
315
+ ): DecipherCCM | DecipherOCB | DecipherGCM | Decipher;
296
316
  export function createDecipheriv(
297
317
  algorithm: string,
298
318
  key: BinaryLikeNode,
@@ -301,47 +321,67 @@ export function createDecipheriv(
301
321
  | CipherCCMOptions
302
322
  | CipherOCBOptions
303
323
  | CipherGCMOptions
304
- | Stream.TransformOptions
324
+ | Stream.TransformOptions,
305
325
  ): DecipherCCM | DecipherOCB | DecipherGCM | Decipher {
306
- return new Decipher(algorithm, key, options, iv);
326
+ return new Decipher(
327
+ algorithm,
328
+ key,
329
+ options as Record<string, TransformOptions>,
330
+ iv,
331
+ );
307
332
  }
308
333
 
309
334
  export function createCipher(
310
335
  algorithm: CipherCCMTypes,
311
336
  password: BinaryLikeNode,
312
- options: CipherCCMOptions
337
+ options: CipherCCMOptions,
313
338
  ): CipherCCM;
314
339
  export function createCipher(
315
340
  algorithm: CipherGCMTypes,
316
341
  password: BinaryLikeNode,
317
- options?: CipherGCMOptions
342
+ options?: CipherGCMOptions,
318
343
  ): CipherGCM;
344
+ export function createCipher(
345
+ algorithm: CipherType,
346
+ password: BinaryLikeNode,
347
+ options?: Stream.TransformOptions,
348
+ ): CipherCCM | CipherGCM | Cipher;
319
349
  export function createCipher(
320
350
  algorithm: string,
321
351
  password: BinaryLikeNode,
322
- options?: CipherGCMOptions | CipherCCMOptions | Stream.TransformOptions
352
+ options?: CipherGCMOptions | CipherCCMOptions | Stream.TransformOptions,
323
353
  ): CipherCCM | CipherGCM | Cipher {
324
- return new Cipher(algorithm, password, options);
354
+ return new Cipher(
355
+ algorithm,
356
+ password,
357
+ options as Record<string, TransformOptions>,
358
+ );
325
359
  }
326
360
 
327
361
  export function createCipheriv(
328
362
  algorithm: CipherCCMTypes,
329
363
  key: BinaryLikeNode,
330
364
  iv: BinaryLike,
331
- options: CipherCCMOptions
365
+ options: CipherCCMOptions,
332
366
  ): CipherCCM;
333
367
  export function createCipheriv(
334
368
  algorithm: CipherOCBTypes,
335
369
  key: BinaryLikeNode,
336
370
  iv: BinaryLike,
337
- options: CipherOCBOptions
371
+ options: CipherOCBOptions,
338
372
  ): CipherOCB;
339
373
  export function createCipheriv(
340
374
  algorithm: CipherGCMTypes,
341
375
  key: BinaryLikeNode,
342
376
  iv: BinaryLike,
343
- options?: CipherGCMOptions
377
+ options?: CipherGCMOptions,
344
378
  ): CipherGCM;
379
+ export function createCipheriv(
380
+ algorithm: CipherType,
381
+ key: BinaryLikeNode,
382
+ iv: BinaryLike | null,
383
+ options?: Stream.TransformOptions,
384
+ ): CipherCCM | CipherOCB | CipherGCM | Cipher;
345
385
  export function createCipheriv(
346
386
  algorithm: string,
347
387
  key: BinaryLikeNode,
@@ -350,41 +390,34 @@ export function createCipheriv(
350
390
  | CipherCCMOptions
351
391
  | CipherOCBOptions
352
392
  | CipherGCMOptions
353
- | Stream.TransformOptions
393
+ | Stream.TransformOptions,
354
394
  ): CipherCCM | CipherOCB | CipherGCM | Cipher {
355
- return new Cipher(algorithm, key, options, iv);
395
+ return new Cipher(
396
+ algorithm,
397
+ key,
398
+ options as Record<string, TransformOptions>,
399
+ iv,
400
+ );
356
401
  }
357
402
 
358
403
  // RSA Functions
359
404
  // Follows closely the model implemented in node
360
405
 
361
- // TODO(osp) types...
362
406
  function rsaFunctionFor(
363
407
  method: (
364
408
  data: ArrayBuffer,
365
- format: number,
366
- type: any,
367
- passphrase: any,
409
+ format: KFormatType,
410
+ type: KeyEncoding | undefined,
411
+ passphrase: string | ArrayBuffer | undefined,
368
412
  buffer: ArrayBuffer,
369
413
  padding: number,
370
- oaepHash: any,
371
- oaepLabel: any
414
+ oaepHash: ArrayBuffer | undefined,
415
+ oaepLabel: ArrayBuffer | undefined,
372
416
  ) => Buffer,
373
417
  defaultPadding: number,
374
- keyType: 'public' | 'private'
418
+ keyType: 'public' | 'private',
375
419
  ) {
376
- return (
377
- options: {
378
- key: any;
379
- encoding?: string;
380
- format?: any;
381
- padding?: any;
382
- oaepHash?: any;
383
- oaepLabel?: any;
384
- passphrase?: string;
385
- },
386
- buffer: BinaryLike
387
- ) => {
420
+ return (options: EncodingOptions, buffer: BinaryLike) => {
388
421
  const { format, type, data, passphrase } =
389
422
  keyType === 'private'
390
423
  ? preparePrivateKey(options)
@@ -405,7 +438,7 @@ function rsaFunctionFor(
405
438
  buffer,
406
439
  padding,
407
440
  oaepHash,
408
- oaepLabel
441
+ oaepLabel,
409
442
  );
410
443
 
411
444
  return Buffer.from(rawRes);
@@ -415,19 +448,19 @@ function rsaFunctionFor(
415
448
  export const publicEncrypt = rsaFunctionFor(
416
449
  _publicEncrypt,
417
450
  constants.RSA_PKCS1_OAEP_PADDING,
418
- 'public'
451
+ 'public',
419
452
  );
420
453
  export const publicDecrypt = rsaFunctionFor(
421
454
  _publicDecrypt,
422
455
  constants.RSA_PKCS1_PADDING,
423
- 'public'
456
+ 'public',
424
457
  );
425
458
  // const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,
426
459
  // 'private');
427
460
  export const privateDecrypt = rsaFunctionFor(
428
461
  _privateDecrypt,
429
462
  constants.RSA_PKCS1_OAEP_PADDING,
430
- 'private'
463
+ 'private',
431
464
  );
432
465
 
433
466
  // _ _ __ _____ _
@@ -450,11 +483,11 @@ export type GenerateKeyPairOptions = {
450
483
  primeLength?: number; // Prime length in bits (DH).
451
484
  generator?: number; // Custom generator (DH). Default: 2.
452
485
  groupName?: string; // Diffie-Hellman group name (DH). See crypto.getDiffieHellman().
453
- publicKeyEncoding?: any; // See keyObject.export().
454
- privateKeyEncoding?: any; // See keyObject.export().
486
+ publicKeyEncoding?: EncodingOptions; // See keyObject.export().
487
+ privateKeyEncoding?: EncodingOptions; // See keyObject.export().
455
488
  paramEncoding?: string;
456
- hash?: any;
457
- mgf1Hash?: any;
489
+ hash?: string;
490
+ mgf1Hash?: string;
458
491
  };
459
492
 
460
493
  export type KeyPairKey = Buffer | KeyObjectHandle | CryptoKey | undefined;
@@ -468,7 +501,7 @@ export type GenerateKeyPairReturn = [
468
501
  export type GenerateKeyPairCallback = (
469
502
  error?: Error,
470
503
  publicKey?: KeyPairKey,
471
- privateKey?: KeyPairKey
504
+ privateKey?: KeyPairKey,
472
505
  ) => GenerateKeyPairReturn | void;
473
506
 
474
507
  export type KeyPair = {
@@ -480,7 +513,7 @@ export type GenerateKeyPairPromiseReturn = [error?: Error, keypair?: KeyPair];
480
513
 
481
514
  function parseKeyEncoding(
482
515
  keyType: string,
483
- options: GenerateKeyPairOptions = kEmptyObject
516
+ options: GenerateKeyPairOptions = kEmptyObject,
484
517
  ) {
485
518
  const { publicKeyEncoding, privateKeyEncoding } = options;
486
519
 
@@ -491,12 +524,12 @@ function parseKeyEncoding(
491
524
  ({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(
492
525
  publicKeyEncoding,
493
526
  keyType,
494
- 'publicKeyEncoding'
527
+ 'publicKeyEncoding',
495
528
  ));
496
529
  } else {
497
530
  throw new Error(
498
531
  'Invalid argument options.publicKeyEncoding',
499
- publicKeyEncoding
532
+ publicKeyEncoding,
500
533
  );
501
534
  }
502
535
 
@@ -512,12 +545,12 @@ function parseKeyEncoding(
512
545
  } = parsePrivateKeyEncoding(
513
546
  privateKeyEncoding,
514
547
  keyType,
515
- 'privateKeyEncoding'
548
+ 'privateKeyEncoding',
516
549
  ));
517
550
  } else {
518
551
  throw new Error(
519
552
  'Invalid argument options.privateKeyEncoding',
520
- publicKeyEncoding
553
+ publicKeyEncoding as ErrorOptions,
521
554
  );
522
555
  }
523
556
 
@@ -537,8 +570,8 @@ function parseKeyEncoding(
537
570
  function internalGenerateKeyPair(
538
571
  isAsync: boolean,
539
572
  type: KeyPairType,
540
- options: GenerateKeyPairOptions | undefined,
541
- callback?: GenerateKeyPairCallback
573
+ options?: GenerateKeyPairOptions,
574
+ callback?: GenerateKeyPairCallback,
542
575
  ): GenerateKeyPairReturn | void {
543
576
  const encoding = parseKeyEncoding(type, options);
544
577
 
@@ -549,99 +582,12 @@ function internalGenerateKeyPair(
549
582
  case 'rsa-pss':
550
583
  // fallthrough
551
584
  case 'rsa':
552
- validateObject<GenerateKeyPairOptions>(options, 'options');
553
- const { modulusLength } = options!;
554
- validateUint32(modulusLength as number, 'options.modulusLength');
555
- let { publicExponent } = options!;
556
- if (publicExponent == null) {
557
- publicExponent = 0x10001;
558
- } else {
559
- validateUint32(publicExponent, 'options.publicExponent');
560
- }
561
-
562
- if (type === 'rsa') {
563
- if (isAsync) {
564
- NativeQuickCrypto.generateKeyPair(
565
- KeyVariant.RSA_SSA_PKCS1_v1_5, // Used also for RSA-OAEP
566
- modulusLength as number,
567
- publicExponent,
568
- ...encoding
569
- )
570
- .then(([err, publicKey, privateKey]) => {
571
- if (publicKey instanceof Buffer) {
572
- publicKey = Buffer.from(publicKey);
573
- }
574
- if (privateKey instanceof Buffer) {
575
- privateKey = Buffer.from(privateKey);
576
- }
577
- callback!(err, publicKey, privateKey);
578
- })
579
- .catch((err) => {
580
- callback!(err, undefined, undefined);
581
- });
582
- } else {
583
- let [err, publicKey, privateKey] =
584
- NativeQuickCrypto.generateKeyPairSync(
585
- KeyVariant.RSA_SSA_PKCS1_v1_5,
586
- modulusLength as number,
587
- publicExponent,
588
- ...encoding
589
- );
590
-
591
- if (publicKey instanceof Buffer) {
592
- publicKey = Buffer.from(publicKey);
593
- }
594
- if (privateKey instanceof Buffer) {
595
- privateKey = Buffer.from(privateKey);
596
- }
597
-
598
- return [err, publicKey, privateKey];
599
- }
600
- }
601
-
602
- const { hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength } =
603
- options!;
604
-
605
- // // We don't have a process object on RN
606
- // // const pendingDeprecation = getOptionValue('--pending-deprecation');
607
-
608
- if (saltLength !== undefined)
609
- validateInt32(saltLength, 'options.saltLength', 0);
610
- if (hashAlgorithm !== undefined)
611
- validateString(hashAlgorithm, 'options.hashAlgorithm');
612
- if (mgf1HashAlgorithm !== undefined)
613
- validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
614
- if (hash !== undefined) {
615
- // pendingDeprecation && process.emitWarning(
616
- // '"options.hash" is deprecated, ' +
617
- // 'use "options.hashAlgorithm" instead.',
618
- // 'DeprecationWarning',
619
- // 'DEP0154');
620
- validateString(hash, 'options.hash');
621
- if (hashAlgorithm && hash !== hashAlgorithm) {
622
- throw new Error(`Invalid Argument options.hash ${hash}`);
623
- }
624
- }
625
- if (mgf1Hash !== undefined) {
626
- // pendingDeprecation && process.emitWarning(
627
- // '"options.mgf1Hash" is deprecated, ' +
628
- // 'use "options.mgf1HashAlgorithm" instead.',
629
- // 'DeprecationWarning',
630
- // 'DEP0154');
631
- validateString(mgf1Hash, 'options.mgf1Hash');
632
- if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
633
- throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);
634
- }
635
- }
636
-
637
- return NativeQuickCrypto.generateKeyPairSync(
638
- KeyVariant.RSA_PSS,
639
- modulusLength as number,
640
- publicExponent,
641
- hashAlgorithm || hash,
642
- mgf1HashAlgorithm || mgf1Hash,
643
- saltLength,
644
- ...encoding
585
+ return internalRsaGenerateKeyPair(
586
+ isAsync,
587
+ type,
588
+ options,
589
+ callback,
590
+ encoding,
645
591
  );
646
592
 
647
593
  // case 'dsa': {
@@ -662,54 +608,13 @@ function internalGenerateKeyPair(
662
608
  // }
663
609
 
664
610
  case 'ec':
665
- validateObject<GenerateKeyPairOptions>(options, 'options');
666
- const { namedCurve } = options!;
667
- validateString(namedCurve, 'options.namedCurve');
668
- let paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
669
- const { paramEncoding } = options!;
670
- if (paramEncoding == null || paramEncoding === 'named')
671
- paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
672
- else if (paramEncoding === 'explicit')
673
- paramEncodingFlag = ECCurve.OPENSSL_EC_EXPLICIT_CURVE;
674
- else
675
- throw new Error(
676
- `Invalid Argument options.paramEncoding ${paramEncoding}`
677
- );
678
-
679
- if (isAsync) {
680
- NativeQuickCrypto.generateKeyPair(
681
- KeyVariant.EC,
682
- namedCurve as NamedCurve,
683
- paramEncodingFlag,
684
- ...encoding
685
- )
686
- .then(([err, publicKey, privateKey]) => {
687
- if (publicKey instanceof Buffer) {
688
- publicKey = Buffer.from(publicKey);
689
- }
690
- if (privateKey instanceof Buffer) {
691
- privateKey = Buffer.from(privateKey);
692
- }
693
- callback?.(err, publicKey, privateKey);
694
- })
695
- .catch((err) => {
696
- callback?.(err, undefined, undefined);
697
- });
698
- }
699
-
700
- let [err, publicKey, privateKey] = NativeQuickCrypto.generateKeyPairSync(
701
- KeyVariant.EC,
702
- namedCurve as NamedCurve,
703
- paramEncodingFlag,
704
- ...encoding
611
+ return internalEcGenerateKeyPair(
612
+ isAsync,
613
+ type,
614
+ options,
615
+ callback,
616
+ encoding,
705
617
  );
706
- if (publicKey instanceof Buffer) {
707
- publicKey = Buffer.from(publicKey);
708
- }
709
- if (privateKey instanceof Buffer) {
710
- privateKey = Buffer.from(privateKey);
711
- }
712
- return [err, publicKey, privateKey];
713
618
 
714
619
  // case 'ed25519':
715
620
  // case 'ed448':
@@ -782,10 +687,162 @@ function internalGenerateKeyPair(
782
687
  return [err, undefined, undefined];
783
688
  }
784
689
 
690
+ const internalRsaGenerateKeyPair = (
691
+ isAsync: boolean,
692
+ type: KeyPairType,
693
+ options: GenerateKeyPairOptions | undefined,
694
+ callback: GenerateKeyPairCallback | undefined,
695
+ encoding: (string | ArrayBuffer | KFormatType | KeyEncoding | undefined)[],
696
+ ): GenerateKeyPairReturn | void => {
697
+ validateObject<GenerateKeyPairOptions>(options, 'options');
698
+ const { modulusLength } = options!;
699
+ validateUint32(modulusLength as number, 'options.modulusLength');
700
+ let { publicExponent } = options!;
701
+ if (publicExponent == null) {
702
+ publicExponent = 0x10001;
703
+ } else {
704
+ validateUint32(publicExponent, 'options.publicExponent');
705
+ }
706
+
707
+ if (type === 'rsa') {
708
+ if (isAsync) {
709
+ NativeQuickCrypto.generateKeyPair(
710
+ KeyVariant.RSA_SSA_PKCS1_v1_5, // Used also for RSA-OAEP
711
+ modulusLength as number,
712
+ publicExponent,
713
+ ...encoding,
714
+ )
715
+ .then(([err, publicKey, privateKey]) => {
716
+ if (publicKey instanceof Buffer) {
717
+ publicKey = Buffer.from(publicKey);
718
+ }
719
+ if (privateKey instanceof Buffer) {
720
+ privateKey = Buffer.from(privateKey);
721
+ }
722
+ callback!(err, publicKey, privateKey);
723
+ })
724
+ .catch((err) => {
725
+ callback!(err, undefined, undefined);
726
+ });
727
+ } else {
728
+ const [err, publicKey, privateKey] =
729
+ NativeQuickCrypto.generateKeyPairSync(
730
+ KeyVariant.RSA_SSA_PKCS1_v1_5,
731
+ modulusLength as number,
732
+ publicExponent,
733
+ ...encoding,
734
+ );
735
+
736
+ const pub =
737
+ publicKey instanceof Buffer ? Buffer.from(publicKey) : publicKey;
738
+ const priv =
739
+ privateKey instanceof Buffer ? Buffer.from(privateKey) : privateKey;
740
+ return [err, pub, priv];
741
+ }
742
+ }
743
+
744
+ const { hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength } =
745
+ options!;
746
+
747
+ // // We don't have a process object on RN
748
+ // // const pendingDeprecation = getOptionValue('--pending-deprecation');
749
+
750
+ if (saltLength !== undefined)
751
+ validateInt32(saltLength, 'options.saltLength', 0);
752
+ if (hashAlgorithm !== undefined)
753
+ validateString(hashAlgorithm, 'options.hashAlgorithm');
754
+ if (mgf1HashAlgorithm !== undefined)
755
+ validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
756
+ if (hash !== undefined) {
757
+ // pendingDeprecation && process.emitWarning(
758
+ // '"options.hash" is deprecated, ' +
759
+ // 'use "options.hashAlgorithm" instead.',
760
+ // 'DeprecationWarning',
761
+ // 'DEP0154');
762
+ validateString(hash, 'options.hash');
763
+ if (hashAlgorithm && hash !== hashAlgorithm) {
764
+ throw new Error(`Invalid Argument options.hash ${hash}`);
765
+ }
766
+ }
767
+ if (mgf1Hash !== undefined) {
768
+ // pendingDeprecation && process.emitWarning(
769
+ // '"options.mgf1Hash" is deprecated, ' +
770
+ // 'use "options.mgf1HashAlgorithm" instead.',
771
+ // 'DeprecationWarning',
772
+ // 'DEP0154');
773
+ validateString(mgf1Hash, 'options.mgf1Hash');
774
+ if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
775
+ throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);
776
+ }
777
+ }
778
+
779
+ return NativeQuickCrypto.generateKeyPairSync(
780
+ KeyVariant.RSA_PSS,
781
+ modulusLength as number,
782
+ publicExponent,
783
+ hashAlgorithm || hash,
784
+ mgf1HashAlgorithm || mgf1Hash,
785
+ saltLength,
786
+ ...encoding,
787
+ );
788
+ };
789
+
790
+ const internalEcGenerateKeyPair = (
791
+ isAsync: boolean,
792
+ _type: KeyPairType,
793
+ options: GenerateKeyPairOptions | undefined,
794
+ callback: GenerateKeyPairCallback | undefined,
795
+ encoding: (string | ArrayBuffer | KFormatType | KeyEncoding | undefined)[],
796
+ ): GenerateKeyPairReturn | void => {
797
+ validateObject<GenerateKeyPairOptions>(options, 'options');
798
+ const { namedCurve } = options!;
799
+ validateString(namedCurve, 'options.namedCurve');
800
+ let paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
801
+ const { paramEncoding } = options!;
802
+ if (paramEncoding == null || paramEncoding === 'named')
803
+ paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
804
+ else if (paramEncoding === 'explicit')
805
+ paramEncodingFlag = ECCurve.OPENSSL_EC_EXPLICIT_CURVE;
806
+ else
807
+ throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);
808
+
809
+ if (isAsync) {
810
+ NativeQuickCrypto.generateKeyPair(
811
+ KeyVariant.EC,
812
+ namedCurve as NamedCurve,
813
+ paramEncodingFlag,
814
+ ...encoding,
815
+ )
816
+ .then(([err, publicKey, privateKey]) => {
817
+ if (publicKey instanceof Buffer) {
818
+ publicKey = Buffer.from(publicKey);
819
+ }
820
+ if (privateKey instanceof Buffer) {
821
+ privateKey = Buffer.from(privateKey);
822
+ }
823
+ callback?.(err, publicKey, privateKey);
824
+ })
825
+ .catch((err) => {
826
+ callback?.(err, undefined, undefined);
827
+ });
828
+ }
829
+
830
+ const [err, publicKey, privateKey] = NativeQuickCrypto.generateKeyPairSync(
831
+ KeyVariant.EC,
832
+ namedCurve as NamedCurve,
833
+ paramEncodingFlag,
834
+ ...encoding,
835
+ );
836
+ const pub = publicKey instanceof Buffer ? Buffer.from(publicKey) : publicKey;
837
+ const priv =
838
+ privateKey instanceof Buffer ? Buffer.from(privateKey) : privateKey;
839
+ return [err, pub, priv];
840
+ };
841
+
785
842
  export const generateKeyPair = (
786
843
  type: KeyPairType,
787
844
  options: GenerateKeyPairOptions,
788
- callback: GenerateKeyPairCallback
845
+ callback: GenerateKeyPairCallback,
789
846
  ): void => {
790
847
  validateFunction(callback);
791
848
  internalGenerateKeyPair(true, type, options, callback);
@@ -795,7 +852,7 @@ export const generateKeyPair = (
795
852
  // (attempted to use util.promisify, to no avail)
796
853
  export const generateKeyPairPromise = (
797
854
  type: KeyPairType,
798
- options: GenerateKeyPairOptions
855
+ options: GenerateKeyPairOptions,
799
856
  ): Promise<GenerateKeyPairPromiseReturn> => {
800
857
  return new Promise((resolve, reject) => {
801
858
  generateKeyPair(type, options, (err, publicKey, privateKey) => {
@@ -812,17 +869,18 @@ export const generateKeyPairPromise = (
812
869
  export function generateKeyPairSync(type: KeyPairType): CryptoKeyPair;
813
870
  export function generateKeyPairSync(
814
871
  type: KeyPairType,
815
- options: GenerateKeyPairOptions
872
+ options: GenerateKeyPairOptions,
816
873
  ): CryptoKeyPair;
817
874
  export function generateKeyPairSync(
818
875
  type: KeyPairType,
819
- options?: GenerateKeyPairOptions
876
+ options?: GenerateKeyPairOptions,
820
877
  ): CryptoKeyPair {
878
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
821
879
  const [_, publicKey, privateKey] = internalGenerateKeyPair(
822
880
  false,
823
881
  type,
824
882
  options,
825
- undefined
883
+ undefined,
826
884
  )!;
827
885
 
828
886
  return {