react-native-quick-crypto 0.7.2 → 1.0.0-beta.1

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 (304) hide show
  1. package/QuickCrypto.podspec +34 -0
  2. package/android/CMakeLists.txt +32 -64
  3. package/android/build.gradle +52 -125
  4. package/android/gradle.properties +3 -3
  5. package/android/src/main/cpp/cpp-adapter.cpp +9 -54
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
  7. package/ios/QuickCryptoOnLoad.mm +19 -0
  8. package/lib/commonjs/index.js +39 -39
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/package.json +1 -0
  11. package/lib/commonjs/random.js +26 -12
  12. package/lib/commonjs/random.js.map +1 -1
  13. package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
  14. package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
  15. package/lib/commonjs/utils/conversion.js +22 -0
  16. package/lib/commonjs/utils/conversion.js.map +1 -0
  17. package/lib/commonjs/utils/index.js +12 -0
  18. package/lib/commonjs/utils/index.js.map +1 -0
  19. package/lib/commonjs/utils/types.js +2 -0
  20. package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
  21. package/lib/module/index.js +40 -38
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/package.json +1 -0
  24. package/lib/module/random.js +28 -12
  25. package/lib/module/random.js.map +1 -1
  26. package/lib/module/specs/random.nitro.js +4 -0
  27. package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
  28. package/lib/module/utils/conversion.js +16 -0
  29. package/lib/module/utils/conversion.js.map +1 -0
  30. package/lib/module/utils/index.js +8 -0
  31. package/lib/module/utils/index.js.map +1 -0
  32. package/lib/module/utils/types.js +2 -0
  33. package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
  34. package/lib/tsconfig.tsbuildinfo +1 -0
  35. package/lib/typescript/index.d.ts +10 -141
  36. package/lib/typescript/index.d.ts.map +1 -1
  37. package/lib/typescript/random.d.ts +4 -5
  38. package/lib/typescript/random.d.ts.map +1 -1
  39. package/lib/typescript/specs/random.nitro.d.ts +9 -0
  40. package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
  41. package/lib/typescript/utils/conversion.d.ts +4 -0
  42. package/lib/typescript/utils/conversion.d.ts.map +1 -0
  43. package/lib/typescript/utils/index.d.ts +6 -0
  44. package/lib/typescript/utils/index.d.ts.map +1 -0
  45. package/lib/typescript/utils/types.d.ts +5 -0
  46. package/lib/typescript/utils/types.d.ts.map +1 -0
  47. package/package.json +58 -108
  48. package/src/index.ts +38 -48
  49. package/src/random.ts +51 -48
  50. package/src/specs/random.nitro.ts +14 -0
  51. package/src/utils/conversion.ts +16 -0
  52. package/src/utils/index.ts +6 -0
  53. package/src/utils/types.ts +15 -0
  54. package/LICENSE +0 -27
  55. package/README.md +0 -203
  56. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
  57. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
  58. package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
  59. package/cpp/Cipher/MGLCipherHostObject.h +0 -90
  60. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
  61. package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
  62. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
  63. package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
  64. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
  65. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
  66. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
  67. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
  68. package/cpp/Cipher/MGLPublicCipher.h +0 -120
  69. package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
  70. package/cpp/Cipher/MGLRsa.cpp +0 -573
  71. package/cpp/Cipher/MGLRsa.h +0 -109
  72. package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
  73. package/cpp/HMAC/MGLHmacHostObject.h +0 -39
  74. package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
  75. package/cpp/HMAC/MGLHmacInstaller.h +0 -20
  76. package/cpp/Hash/MGLHashHostObject.cpp +0 -147
  77. package/cpp/Hash/MGLHashHostObject.h +0 -53
  78. package/cpp/Hash/MGLHashInstaller.cpp +0 -44
  79. package/cpp/Hash/MGLHashInstaller.h +0 -20
  80. package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
  81. package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
  82. package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
  83. package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
  84. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
  85. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
  86. package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
  87. package/cpp/JSIUtils/MGLTypedArray.h +0 -160
  88. package/cpp/MGLKeys.cpp +0 -1426
  89. package/cpp/MGLKeys.h +0 -203
  90. package/cpp/MGLQuickCryptoHostObject.cpp +0 -124
  91. package/cpp/MGLQuickCryptoHostObject.h +0 -30
  92. package/cpp/Random/MGLRandomHostObject.cpp +0 -96
  93. package/cpp/Random/MGLRandomHostObject.h +0 -27
  94. package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
  95. package/cpp/Sig/MGLSignHostObjects.h +0 -128
  96. package/cpp/Sig/MGLSignInstaller.cpp +0 -24
  97. package/cpp/Sig/MGLSignInstaller.h +0 -29
  98. package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
  99. package/cpp/Sig/MGLVerifyInstaller.h +0 -22
  100. package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
  101. package/cpp/Utils/MGLDispatchQueue.h +0 -55
  102. package/cpp/Utils/MGLUtils.cpp +0 -295
  103. package/cpp/Utils/MGLUtils.h +0 -358
  104. package/cpp/Utils/base64.h +0 -320
  105. package/cpp/Utils/logs.h +0 -38
  106. package/cpp/Utils/node.h +0 -13
  107. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
  108. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
  109. package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
  110. package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
  111. package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
  112. package/cpp/webcrypto/MGLWebCrypto.h +0 -32
  113. package/cpp/webcrypto/crypto_aes.cpp +0 -516
  114. package/cpp/webcrypto/crypto_aes.h +0 -79
  115. package/cpp/webcrypto/crypto_ec.cpp +0 -424
  116. package/cpp/webcrypto/crypto_ec.h +0 -78
  117. package/cpp/webcrypto/crypto_keygen.cpp +0 -86
  118. package/cpp/webcrypto/crypto_keygen.h +0 -38
  119. package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
  120. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  121. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  122. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  123. package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  124. package/ios/QuickCryptoModule.h +0 -5
  125. package/ios/QuickCryptoModule.mm +0 -42
  126. package/lib/commonjs/Cipher.js +0 -502
  127. package/lib/commonjs/Cipher.js.map +0 -1
  128. package/lib/commonjs/Hash.js +0 -99
  129. package/lib/commonjs/Hash.js.map +0 -1
  130. package/lib/commonjs/Hashnames.js +0 -87
  131. package/lib/commonjs/Hashnames.js.map +0 -1
  132. package/lib/commonjs/Hmac.js +0 -79
  133. package/lib/commonjs/Hmac.js.map +0 -1
  134. package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
  135. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
  136. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
  137. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  138. package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
  139. package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
  140. package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
  141. package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
  142. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
  143. package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
  144. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
  145. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
  146. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
  147. package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
  148. package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
  149. package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
  150. package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
  151. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
  152. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
  153. package/lib/commonjs/Utils.js +0 -608
  154. package/lib/commonjs/Utils.js.map +0 -1
  155. package/lib/commonjs/aes.js +0 -281
  156. package/lib/commonjs/aes.js.map +0 -1
  157. package/lib/commonjs/constants.js +0 -85
  158. package/lib/commonjs/constants.js.map +0 -1
  159. package/lib/commonjs/ec.js +0 -276
  160. package/lib/commonjs/ec.js.map +0 -1
  161. package/lib/commonjs/keygen.js +0 -56
  162. package/lib/commonjs/keygen.js.map +0 -1
  163. package/lib/commonjs/keys.js +0 -492
  164. package/lib/commonjs/keys.js.map +0 -1
  165. package/lib/commonjs/pbkdf2.js +0 -90
  166. package/lib/commonjs/pbkdf2.js.map +0 -1
  167. package/lib/commonjs/rsa.js +0 -248
  168. package/lib/commonjs/rsa.js.map +0 -1
  169. package/lib/commonjs/sig.js +0 -129
  170. package/lib/commonjs/sig.js.map +0 -1
  171. package/lib/commonjs/subtle.js +0 -448
  172. package/lib/commonjs/subtle.js.map +0 -1
  173. package/lib/commonjs/webcrypto.js +0 -14
  174. package/lib/commonjs/webcrypto.js.map +0 -1
  175. package/lib/module/Cipher.js +0 -489
  176. package/lib/module/Cipher.js.map +0 -1
  177. package/lib/module/Hash.js +0 -91
  178. package/lib/module/Hash.js.map +0 -1
  179. package/lib/module/Hashnames.js +0 -83
  180. package/lib/module/Hashnames.js.map +0 -1
  181. package/lib/module/Hmac.js +0 -72
  182. package/lib/module/Hmac.js.map +0 -1
  183. package/lib/module/NativeQuickCrypto/Cipher.js +0 -24
  184. package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
  185. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -47
  186. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  187. package/lib/module/NativeQuickCrypto/aes.js +0 -2
  188. package/lib/module/NativeQuickCrypto/hash.js +0 -2
  189. package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
  190. package/lib/module/NativeQuickCrypto/hmac.js +0 -2
  191. package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
  192. package/lib/module/NativeQuickCrypto/keygen.js +0 -2
  193. package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
  194. package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
  195. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
  196. package/lib/module/NativeQuickCrypto/random.js +0 -2
  197. package/lib/module/NativeQuickCrypto/random.js.map +0 -1
  198. package/lib/module/NativeQuickCrypto/rsa.js +0 -2
  199. package/lib/module/NativeQuickCrypto/sig.js +0 -15
  200. package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
  201. package/lib/module/NativeQuickCrypto/webcrypto.js +0 -2
  202. package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
  203. package/lib/module/Utils.js +0 -537
  204. package/lib/module/Utils.js.map +0 -1
  205. package/lib/module/aes.js +0 -272
  206. package/lib/module/aes.js.map +0 -1
  207. package/lib/module/constants.js +0 -79
  208. package/lib/module/constants.js.map +0 -1
  209. package/lib/module/ec.js +0 -265
  210. package/lib/module/ec.js.map +0 -1
  211. package/lib/module/keygen.js +0 -47
  212. package/lib/module/keygen.js.map +0 -1
  213. package/lib/module/keys.js +0 -475
  214. package/lib/module/keys.js.map +0 -1
  215. package/lib/module/pbkdf2.js +0 -82
  216. package/lib/module/pbkdf2.js.map +0 -1
  217. package/lib/module/rsa.js +0 -240
  218. package/lib/module/rsa.js.map +0 -1
  219. package/lib/module/sig.js +0 -122
  220. package/lib/module/sig.js.map +0 -1
  221. package/lib/module/subtle.js +0 -441
  222. package/lib/module/subtle.js.map +0 -1
  223. package/lib/module/webcrypto.js +0 -8
  224. package/lib/module/webcrypto.js.map +0 -1
  225. package/lib/typescript/Cipher.d.ts +0 -81
  226. package/lib/typescript/Cipher.d.ts.map +0 -1
  227. package/lib/typescript/Hash.d.ts +0 -44
  228. package/lib/typescript/Hash.d.ts.map +0 -1
  229. package/lib/typescript/Hashnames.d.ts +0 -11
  230. package/lib/typescript/Hashnames.d.ts.map +0 -1
  231. package/lib/typescript/Hmac.d.ts +0 -37
  232. package/lib/typescript/Hmac.d.ts.map +0 -1
  233. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
  234. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  235. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
  236. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  237. package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
  238. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  239. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
  240. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  241. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
  242. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  243. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
  244. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  245. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
  246. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  247. package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
  248. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  249. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
  250. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  251. package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
  252. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  253. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
  254. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  255. package/lib/typescript/Utils.d.ts +0 -48
  256. package/lib/typescript/Utils.d.ts.map +0 -1
  257. package/lib/typescript/aes.d.ts +0 -22
  258. package/lib/typescript/aes.d.ts.map +0 -1
  259. package/lib/typescript/constants.d.ts +0 -76
  260. package/lib/typescript/constants.d.ts.map +0 -1
  261. package/lib/typescript/ec.d.ts +0 -7
  262. package/lib/typescript/ec.d.ts.map +0 -1
  263. package/lib/typescript/keygen.d.ts +0 -6
  264. package/lib/typescript/keygen.d.ts.map +0 -1
  265. package/lib/typescript/keys.d.ts +0 -206
  266. package/lib/typescript/keys.d.ts.map +0 -1
  267. package/lib/typescript/pbkdf2.d.ts +0 -12
  268. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  269. package/lib/typescript/rsa.d.ts +0 -12
  270. package/lib/typescript/rsa.d.ts.map +0 -1
  271. package/lib/typescript/sig.d.ts +0 -21
  272. package/lib/typescript/sig.d.ts.map +0 -1
  273. package/lib/typescript/subtle.d.ts +0 -15
  274. package/lib/typescript/subtle.d.ts.map +0 -1
  275. package/lib/typescript/webcrypto.d.ts +0 -9
  276. package/lib/typescript/webcrypto.d.ts.map +0 -1
  277. package/react-native-quick-crypto.podspec +0 -40
  278. package/src/Cipher.ts +0 -832
  279. package/src/Hash.ts +0 -132
  280. package/src/Hashnames.ts +0 -93
  281. package/src/Hmac.ts +0 -106
  282. package/src/NativeQuickCrypto/Cipher.ts +0 -102
  283. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
  284. package/src/NativeQuickCrypto/aes.ts +0 -14
  285. package/src/NativeQuickCrypto/hash.ts +0 -10
  286. package/src/NativeQuickCrypto/hmac.ts +0 -9
  287. package/src/NativeQuickCrypto/keygen.ts +0 -7
  288. package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
  289. package/src/NativeQuickCrypto/random.ts +0 -12
  290. package/src/NativeQuickCrypto/rsa.ts +0 -12
  291. package/src/NativeQuickCrypto/sig.ts +0 -44
  292. package/src/NativeQuickCrypto/webcrypto.ts +0 -72
  293. package/src/Utils.ts +0 -777
  294. package/src/aes.ts +0 -402
  295. package/src/constants.ts +0 -79
  296. package/src/ec.ts +0 -375
  297. package/src/keygen.ts +0 -80
  298. package/src/keys.ts +0 -787
  299. package/src/pbkdf2.ts +0 -169
  300. package/src/rsa.ts +0 -370
  301. package/src/sig.ts +0 -164
  302. package/src/subtle.ts +0 -639
  303. package/src/webcrypto.ts +0 -8
  304. /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
package/src/Cipher.ts DELETED
@@ -1,832 +0,0 @@
1
- import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
2
- import Stream from 'readable-stream';
3
- import {
4
- type BinaryLike,
5
- binaryLikeToArrayBuffer,
6
- type CipherEncoding,
7
- type Encoding,
8
- getDefaultEncoding,
9
- kEmptyObject,
10
- validateFunction,
11
- validateObject,
12
- validateString,
13
- validateUint32,
14
- validateInt32,
15
- type BinaryLikeNode,
16
- } from './Utils';
17
- import { type InternalCipher, KeyVariant } from './NativeQuickCrypto/Cipher';
18
- import type {
19
- CipherCCMOptions,
20
- CipherCCMTypes,
21
- CipherGCMTypes,
22
- CipherGCMOptions,
23
- CipherOCBOptions,
24
- CipherOCBTypes,
25
- DecipherGCM,
26
- DecipherOCB,
27
- DecipherCCM,
28
- CipherCCM,
29
- CipherOCB,
30
- CipherGCM,
31
- } from 'crypto'; // @types/node
32
- import { StringDecoder } from 'string_decoder';
33
- import { Buffer } from '@craftzdog/react-native-buffer';
34
- import { Buffer as SBuffer } from 'safe-buffer';
35
- import { constants } from './constants';
36
- import {
37
- CryptoKey,
38
- parsePrivateKeyEncoding,
39
- parsePublicKeyEncoding,
40
- preparePrivateKey,
41
- preparePublicOrPrivateKey,
42
- type CryptoKeyPair,
43
- type KeyPairType,
44
- type NamedCurve,
45
- } from './keys';
46
- import type { KeyObjectHandle } from './NativeQuickCrypto/webcrypto';
47
-
48
- export enum ECCurve {
49
- OPENSSL_EC_EXPLICIT_CURVE,
50
- OPENSSL_EC_NAMED_CURVE,
51
- }
52
-
53
- // make sure that nextTick is there
54
- global.process.nextTick = setImmediate;
55
-
56
- const createInternalCipher = NativeQuickCrypto.createCipher;
57
- const createInternalDecipher = NativeQuickCrypto.createDecipher;
58
- const _publicEncrypt = NativeQuickCrypto.publicEncrypt;
59
- const _publicDecrypt = NativeQuickCrypto.publicDecrypt;
60
- const _privateDecrypt = NativeQuickCrypto.privateDecrypt;
61
-
62
- function getUIntOption(options: Record<string, any>, key: string) {
63
- let value;
64
- if (options && (value = options[key]) != null) {
65
- // >>> Turns any type into a positive integer (also sets the sign bit to 0)
66
- // eslint-disable-next-line no-bitwise
67
- if (value >>> 0 !== value) throw new Error(`options.${key}: ${value}`);
68
- return value;
69
- }
70
- return -1;
71
- }
72
-
73
- function normalizeEncoding(enc: string) {
74
- if (!enc) return 'utf8';
75
- var retried;
76
- while (true) {
77
- switch (enc) {
78
- case 'utf8':
79
- case 'utf-8':
80
- return 'utf8';
81
- case 'ucs2':
82
- case 'ucs-2':
83
- case 'utf16le':
84
- case 'utf-16le':
85
- return 'utf16le';
86
- case 'latin1':
87
- case 'binary':
88
- return 'latin1';
89
- case 'base64':
90
- case 'ascii':
91
- case 'hex':
92
- return enc;
93
- default:
94
- if (retried) return; // undefined
95
- enc = ('' + enc).toLowerCase();
96
- retried = true;
97
- }
98
- }
99
- }
100
-
101
- function validateEncoding(data: string, encoding: string) {
102
- const normalizedEncoding = normalizeEncoding(encoding);
103
- const length = data.length;
104
-
105
- if (normalizedEncoding === 'hex' && length % 2 !== 0) {
106
- throw new Error(`Encoding ${encoding} not valid for data length ${length}`);
107
- }
108
- }
109
-
110
- function getDecoder(decoder?: StringDecoder, encoding?: BufferEncoding) {
111
- return decoder ?? new StringDecoder(encoding);
112
- }
113
-
114
- class CipherCommon extends Stream.Transform {
115
- private internal: InternalCipher;
116
- private decoder: StringDecoder | undefined;
117
-
118
- constructor(
119
- cipherType: string,
120
- cipherKey: BinaryLikeNode,
121
- isCipher: boolean,
122
- options: Record<string, any> = {},
123
- iv?: BinaryLike | null
124
- ) {
125
- super(options);
126
- const cipherKeyBuffer = binaryLikeToArrayBuffer(cipherKey);
127
- // defaults to 16 bytes
128
- const authTagLength =
129
- getUIntOption(options, 'authTagLength') !== -1
130
- ? getUIntOption(options, 'authTagLength')
131
- : 16;
132
- const args = {
133
- cipher_type: cipherType,
134
- cipher_key: cipherKeyBuffer,
135
- iv,
136
- ...options,
137
- auth_tag_len: authTagLength,
138
- };
139
- this.internal = isCipher
140
- ? createInternalCipher(args)
141
- : createInternalDecipher(args);
142
- }
143
-
144
- update(
145
- data: BinaryLike,
146
- inputEncoding?: CipherEncoding,
147
- outputEncoding?: CipherEncoding
148
- ): ArrayBuffer | string {
149
- const defaultEncoding = getDefaultEncoding();
150
- inputEncoding = inputEncoding ?? defaultEncoding;
151
- outputEncoding = outputEncoding ?? defaultEncoding;
152
-
153
- if (typeof data === 'string') {
154
- validateEncoding(data, inputEncoding);
155
- } else if (!ArrayBuffer.isView(data)) {
156
- throw new Error('Invalid data argument');
157
- }
158
-
159
- if (typeof data === 'string') {
160
- // On node this is handled on the native side
161
- // on our case we need to correctly send the arraybuffer to the jsi side
162
- inputEncoding = inputEncoding === 'buffer' ? 'utf8' : inputEncoding;
163
- data = binaryLikeToArrayBuffer(data, inputEncoding);
164
- } else {
165
- data = binaryLikeToArrayBuffer(data as any, inputEncoding);
166
- }
167
-
168
- const ret = this.internal.update(data);
169
-
170
- if (outputEncoding && outputEncoding !== 'buffer') {
171
- this.decoder = getDecoder(this.decoder, outputEncoding);
172
-
173
- return this.decoder!.write(SBuffer.from(ret) as any);
174
- }
175
-
176
- return ret;
177
- }
178
-
179
- final(): ArrayBuffer;
180
- final(outputEncoding: BufferEncoding | 'buffer'): string;
181
- final(outputEncoding?: BufferEncoding | 'buffer'): ArrayBuffer | string {
182
- const ret = this.internal.final();
183
-
184
- if (outputEncoding && outputEncoding !== 'buffer') {
185
- this.decoder = getDecoder(this.decoder, outputEncoding);
186
-
187
- return this.decoder!.end(SBuffer.from(ret) as any);
188
- }
189
-
190
- return ret;
191
- }
192
-
193
- _transform(chunk: BinaryLike, encoding: Encoding, callback: () => void) {
194
- this.push(this.update(chunk, encoding));
195
- callback();
196
- }
197
-
198
- _flush(callback: () => void) {
199
- this.push(this.final());
200
- callback();
201
- }
202
-
203
- public setAutoPadding(autoPadding?: boolean): this {
204
- this.internal.setAutoPadding(!!autoPadding);
205
- return this;
206
- }
207
-
208
- public setAAD(
209
- buffer: Buffer,
210
- options?: {
211
- plaintextLength: number;
212
- }
213
- ): this {
214
- this.internal.setAAD({
215
- data: buffer.buffer,
216
- plaintextLength: options?.plaintextLength,
217
- });
218
- return this;
219
- }
220
-
221
- public getAuthTag(): ArrayBuffer {
222
- return this.internal.getAuthTag();
223
- }
224
-
225
- public setAuthTag(tag: Buffer): this {
226
- this.internal.setAuthTag(binaryLikeToArrayBuffer(tag));
227
- return this;
228
- }
229
- }
230
-
231
- class Cipher extends CipherCommon {
232
- constructor(
233
- cipherType: string,
234
- cipherKey: BinaryLikeNode,
235
- options: Record<string, any> = {},
236
- iv?: BinaryLike | null
237
- ) {
238
- if (iv != null) {
239
- iv = binaryLikeToArrayBuffer(iv);
240
- }
241
- super(cipherType, cipherKey, true, options, iv);
242
- }
243
- }
244
-
245
- class Decipher extends CipherCommon {
246
- constructor(
247
- cipherType: string,
248
- cipherKey: BinaryLikeNode,
249
- options: Record<string, any> = {},
250
- iv?: BinaryLike | null
251
- ) {
252
- if (iv != null) {
253
- iv = binaryLikeToArrayBuffer(iv);
254
- }
255
-
256
- super(cipherType, cipherKey, false, options, iv);
257
- }
258
- }
259
-
260
- export function createDecipher(
261
- algorithm: CipherCCMTypes,
262
- password: BinaryLikeNode,
263
- options: CipherCCMOptions
264
- ): DecipherCCM;
265
- export function createDecipher(
266
- algorithm: CipherGCMTypes,
267
- password: BinaryLikeNode,
268
- options?: CipherGCMOptions
269
- ): DecipherGCM;
270
- export function createDecipher(
271
- algorithm: string,
272
- password: BinaryLikeNode,
273
- options?: CipherCCMOptions | CipherGCMOptions | Stream.TransformOptions
274
- ): DecipherCCM | DecipherGCM | Decipher {
275
- return new Decipher(algorithm, password, options);
276
- }
277
-
278
- export function createDecipheriv(
279
- algorithm: CipherCCMTypes,
280
- key: BinaryLikeNode,
281
- iv: BinaryLike,
282
- options: CipherCCMOptions
283
- ): DecipherCCM;
284
- export function createDecipheriv(
285
- algorithm: CipherOCBTypes,
286
- key: BinaryLikeNode,
287
- iv: BinaryLike,
288
- options: CipherOCBOptions
289
- ): DecipherOCB;
290
- export function createDecipheriv(
291
- algorithm: CipherGCMTypes,
292
- key: BinaryLikeNode,
293
- iv: BinaryLike,
294
- options?: CipherGCMOptions
295
- ): DecipherGCM;
296
- export function createDecipheriv(
297
- algorithm: string,
298
- key: BinaryLikeNode,
299
- iv: BinaryLike | null,
300
- options?:
301
- | CipherCCMOptions
302
- | CipherOCBOptions
303
- | CipherGCMOptions
304
- | Stream.TransformOptions
305
- ): DecipherCCM | DecipherOCB | DecipherGCM | Decipher {
306
- return new Decipher(algorithm, key, options, iv);
307
- }
308
-
309
- export function createCipher(
310
- algorithm: CipherCCMTypes,
311
- password: BinaryLikeNode,
312
- options: CipherCCMOptions
313
- ): CipherCCM;
314
- export function createCipher(
315
- algorithm: CipherGCMTypes,
316
- password: BinaryLikeNode,
317
- options?: CipherGCMOptions
318
- ): CipherGCM;
319
- export function createCipher(
320
- algorithm: string,
321
- password: BinaryLikeNode,
322
- options?: CipherGCMOptions | CipherCCMOptions | Stream.TransformOptions
323
- ): CipherCCM | CipherGCM | Cipher {
324
- return new Cipher(algorithm, password, options);
325
- }
326
-
327
- export function createCipheriv(
328
- algorithm: CipherCCMTypes,
329
- key: BinaryLikeNode,
330
- iv: BinaryLike,
331
- options: CipherCCMOptions
332
- ): CipherCCM;
333
- export function createCipheriv(
334
- algorithm: CipherOCBTypes,
335
- key: BinaryLikeNode,
336
- iv: BinaryLike,
337
- options: CipherOCBOptions
338
- ): CipherOCB;
339
- export function createCipheriv(
340
- algorithm: CipherGCMTypes,
341
- key: BinaryLikeNode,
342
- iv: BinaryLike,
343
- options?: CipherGCMOptions
344
- ): CipherGCM;
345
- export function createCipheriv(
346
- algorithm: string,
347
- key: BinaryLikeNode,
348
- iv: BinaryLike | null,
349
- options?:
350
- | CipherCCMOptions
351
- | CipherOCBOptions
352
- | CipherGCMOptions
353
- | Stream.TransformOptions
354
- ): CipherCCM | CipherOCB | CipherGCM | Cipher {
355
- return new Cipher(algorithm, key, options, iv);
356
- }
357
-
358
- // RSA Functions
359
- // Follows closely the model implemented in node
360
-
361
- // TODO(osp) types...
362
- function rsaFunctionFor(
363
- method: (
364
- data: ArrayBuffer,
365
- format: number,
366
- type: any,
367
- passphrase: any,
368
- buffer: ArrayBuffer,
369
- padding: number,
370
- oaepHash: any,
371
- oaepLabel: any
372
- ) => Buffer,
373
- defaultPadding: number,
374
- keyType: 'public' | 'private'
375
- ) {
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
- ) => {
388
- const { format, type, data, passphrase } =
389
- keyType === 'private'
390
- ? preparePrivateKey(options)
391
- : preparePublicOrPrivateKey(options);
392
- const padding = options.padding || defaultPadding;
393
- const { oaepHash, encoding } = options;
394
- let { oaepLabel } = options;
395
- if (oaepHash !== undefined) validateString(oaepHash, 'key.oaepHash');
396
- if (oaepLabel !== undefined)
397
- oaepLabel = binaryLikeToArrayBuffer(oaepLabel, encoding);
398
- buffer = binaryLikeToArrayBuffer(buffer, encoding);
399
-
400
- const rawRes = method(
401
- data,
402
- format,
403
- type,
404
- passphrase,
405
- buffer,
406
- padding,
407
- oaepHash,
408
- oaepLabel
409
- );
410
-
411
- return Buffer.from(rawRes);
412
- };
413
- }
414
-
415
- export const publicEncrypt = rsaFunctionFor(
416
- _publicEncrypt,
417
- constants.RSA_PKCS1_OAEP_PADDING,
418
- 'public'
419
- );
420
- export const publicDecrypt = rsaFunctionFor(
421
- _publicDecrypt,
422
- constants.RSA_PKCS1_PADDING,
423
- 'public'
424
- );
425
- // const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,
426
- // 'private');
427
- export const privateDecrypt = rsaFunctionFor(
428
- _privateDecrypt,
429
- constants.RSA_PKCS1_OAEP_PADDING,
430
- 'private'
431
- );
432
-
433
- // _ _ __ _____ _
434
- // | | | |/ / | __ \ (_)
435
- // __ _ ___ _ __ ___ _ __ __ _| |_ ___| ' / ___ _ _| |__) |_ _ _ _ __
436
- // / _` |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \ < / _ \ | | | ___/ _` | | '__|
437
- // | (_| | __/ | | | __/ | | (_| | || __/ . \ __/ |_| | | | (_| | | |
438
- // \__, |\___|_| |_|\___|_| \__,_|\__\___|_|\_\___|\__, |_| \__,_|_|_|
439
- // __/ | __/ |
440
- // |___/ |___/
441
- export type GenerateKeyPairOptions = {
442
- modulusLength?: number; // Key size in bits (RSA, DSA).
443
- publicExponent?: number; // Public exponent (RSA). Default: 0x10001.
444
- hashAlgorithm?: string; // Name of the message digest (RSA-PSS).
445
- mgf1HashAlgorithm?: string; // string Name of the message digest used by MGF1 (RSA-PSS).
446
- saltLength?: number; // Minimal salt length in bytes (RSA-PSS).
447
- divisorLength?: number; // Size of q in bits (DSA).
448
- namedCurve?: string; // Name of the curve to use (EC).
449
- prime?: Buffer; // The prime parameter (DH).
450
- primeLength?: number; // Prime length in bits (DH).
451
- generator?: number; // Custom generator (DH). Default: 2.
452
- groupName?: string; // Diffie-Hellman group name (DH). See crypto.getDiffieHellman().
453
- publicKeyEncoding?: any; // See keyObject.export().
454
- privateKeyEncoding?: any; // See keyObject.export().
455
- paramEncoding?: string;
456
- hash?: any;
457
- mgf1Hash?: any;
458
- };
459
-
460
- export type KeyPairKey = Buffer | KeyObjectHandle | CryptoKey | undefined;
461
-
462
- export type GenerateKeyPairReturn = [
463
- error?: Error,
464
- privateKey?: KeyPairKey,
465
- publicKey?: KeyPairKey,
466
- ];
467
-
468
- export type GenerateKeyPairCallback = (
469
- error?: Error,
470
- publicKey?: KeyPairKey,
471
- privateKey?: KeyPairKey
472
- ) => GenerateKeyPairReturn | void;
473
-
474
- export type KeyPair = {
475
- publicKey?: KeyPairKey;
476
- privateKey?: KeyPairKey;
477
- };
478
-
479
- export type GenerateKeyPairPromiseReturn = [error?: Error, keypair?: KeyPair];
480
-
481
- function parseKeyEncoding(
482
- keyType: string,
483
- options: GenerateKeyPairOptions = kEmptyObject
484
- ) {
485
- const { publicKeyEncoding, privateKeyEncoding } = options;
486
-
487
- let publicFormat, publicType;
488
- if (publicKeyEncoding == null) {
489
- publicFormat = publicType = undefined;
490
- } else if (typeof publicKeyEncoding === 'object') {
491
- ({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(
492
- publicKeyEncoding,
493
- keyType,
494
- 'publicKeyEncoding'
495
- ));
496
- } else {
497
- throw new Error(
498
- 'Invalid argument options.publicKeyEncoding',
499
- publicKeyEncoding
500
- );
501
- }
502
-
503
- let privateFormat, privateType, cipher, passphrase;
504
- if (privateKeyEncoding == null) {
505
- privateFormat = privateType = undefined;
506
- } else if (typeof privateKeyEncoding === 'object') {
507
- ({
508
- format: privateFormat,
509
- type: privateType,
510
- cipher,
511
- passphrase,
512
- } = parsePrivateKeyEncoding(
513
- privateKeyEncoding,
514
- keyType,
515
- 'privateKeyEncoding'
516
- ));
517
- } else {
518
- throw new Error(
519
- 'Invalid argument options.privateKeyEncoding',
520
- publicKeyEncoding
521
- );
522
- }
523
-
524
- return [
525
- publicFormat,
526
- publicType,
527
- privateFormat,
528
- privateType,
529
- cipher,
530
- passphrase,
531
- ];
532
- }
533
-
534
- /** On node a very complex "job" chain is created, we are going for a far simpler approach and calling
535
- * an internal function that basically executes the same byte shuffling on the native side
536
- */
537
- function internalGenerateKeyPair(
538
- isAsync: boolean,
539
- type: KeyPairType,
540
- options: GenerateKeyPairOptions | undefined,
541
- callback?: GenerateKeyPairCallback
542
- ): GenerateKeyPairReturn | void {
543
- const encoding = parseKeyEncoding(type, options);
544
-
545
- // if (options !== undefined)
546
- // validateObject(options, 'options');
547
-
548
- switch (type) {
549
- case 'rsa-pss':
550
- // fallthrough
551
- 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
645
- );
646
-
647
- // case 'dsa': {
648
- // validateObject(options, 'options');
649
- // const { modulusLength } = options!;
650
- // validateUint32(modulusLength, 'options.modulusLength');
651
-
652
- // let { divisorLength } = options!;
653
- // if (divisorLength == null) {
654
- // divisorLength = -1;
655
- // } else validateInt32(divisorLength, 'options.divisorLength', 0);
656
-
657
- // // return new DsaKeyPairGenJob(
658
- // // mode,
659
- // // modulusLength,
660
- // // divisorLength,
661
- // // ...encoding);
662
- // }
663
-
664
- 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
705
- );
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
-
714
- // case 'ed25519':
715
- // case 'ed448':
716
- // case 'x25519':
717
- // case 'x448': {
718
- // let id;
719
- // switch (type) {
720
- // case 'ed25519':
721
- // id = EVP_PKEY_ED25519;
722
- // break;
723
- // case 'ed448':
724
- // id = EVP_PKEY_ED448;
725
- // break;
726
- // case 'x25519':
727
- // id = EVP_PKEY_X25519;
728
- // break;
729
- // case 'x448':
730
- // id = EVP_PKEY_X448;
731
- // break;
732
- // }
733
- // return new NidKeyPairGenJob(mode, id, ...encoding);
734
- // }
735
- // case 'dh': {
736
- // validateObject(options, 'options');
737
- // const { group, primeLength, prime, generator } = options;
738
- // if (group != null) {
739
- // if (prime != null)
740
- // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime');
741
- // if (primeLength != null)
742
- // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
743
- // if (generator != null)
744
- // throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
745
-
746
- // validateString(group, 'options.group');
747
-
748
- // return new DhKeyPairGenJob(mode, group, ...encoding);
749
- // }
750
-
751
- // if (prime != null) {
752
- // if (primeLength != null)
753
- // throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
754
-
755
- // validateBuffer(prime, 'options.prime');
756
- // } else if (primeLength != null) {
757
- // validateInt32(primeLength, 'options.primeLength', 0);
758
- // } else {
759
- // throw new ERR_MISSING_OPTION(
760
- // 'At least one of the group, prime, or primeLength options'
761
- // );
762
- // }
763
-
764
- // if (generator != null) {
765
- // validateInt32(generator, 'options.generator', 0);
766
- // }
767
- // return new DhKeyPairGenJob(
768
- // mode,
769
- // prime != null ? prime : primeLength,
770
- // generator == null ? 2 : generator,
771
- // ...encoding
772
- // );
773
- // }
774
- default:
775
- // Fall through
776
- }
777
- const err = new Error(`
778
- Invalid Argument options: '${type}' scheme not supported for generateKey().
779
- Currently not all encryption methods are supported in quick-crypto. Check
780
- implementation_coverage.md for status.
781
- `);
782
- return [err, undefined, undefined];
783
- }
784
-
785
- export const generateKeyPair = (
786
- type: KeyPairType,
787
- options: GenerateKeyPairOptions,
788
- callback: GenerateKeyPairCallback
789
- ): void => {
790
- validateFunction(callback);
791
- internalGenerateKeyPair(true, type, options, callback);
792
- };
793
-
794
- // Promisify generateKeyPair
795
- // (attempted to use util.promisify, to no avail)
796
- export const generateKeyPairPromise = (
797
- type: KeyPairType,
798
- options: GenerateKeyPairOptions
799
- ): Promise<GenerateKeyPairPromiseReturn> => {
800
- return new Promise((resolve, reject) => {
801
- generateKeyPair(type, options, (err, publicKey, privateKey) => {
802
- if (err) {
803
- reject([err, undefined]);
804
- } else {
805
- resolve([undefined, { publicKey, privateKey }]);
806
- }
807
- });
808
- });
809
- };
810
-
811
- // generateKeyPairSync
812
- export function generateKeyPairSync(type: KeyPairType): CryptoKeyPair;
813
- export function generateKeyPairSync(
814
- type: KeyPairType,
815
- options: GenerateKeyPairOptions
816
- ): CryptoKeyPair;
817
- export function generateKeyPairSync(
818
- type: KeyPairType,
819
- options?: GenerateKeyPairOptions
820
- ): CryptoKeyPair {
821
- const [_, publicKey, privateKey] = internalGenerateKeyPair(
822
- false,
823
- type,
824
- options,
825
- undefined
826
- )!;
827
-
828
- return {
829
- publicKey,
830
- privateKey,
831
- };
832
- }