react-native-quick-crypto 0.2.0 → 0.3.0

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 (69) hide show
  1. package/README.md +23 -6
  2. package/cpp/Cipher/MGLCipherHostObject.cpp +64 -48
  3. package/cpp/Cipher/MGLCipherKeys.cpp +1469 -0
  4. package/cpp/Cipher/MGLCipherKeys.h +124 -0
  5. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +56 -53
  6. package/cpp/Cipher/MGLCreateCipherInstaller.h +5 -0
  7. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +56 -53
  8. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +107 -0
  9. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +32 -0
  10. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +60 -0
  11. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +35 -0
  12. package/cpp/Cipher/MGLPublicCipher.h +120 -0
  13. package/cpp/Cipher/MGLPublicCipherInstaller.h +113 -0
  14. package/cpp/Cipher/MGLRsa.cpp +188 -0
  15. package/cpp/Cipher/MGLRsa.h +61 -0
  16. package/cpp/JSIUtils/MGLJSIUtils.h +24 -0
  17. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
  18. package/cpp/MGLQuickCryptoHostObject.cpp +42 -3
  19. package/cpp/Utils/MGLUtils.cpp +156 -0
  20. package/cpp/Utils/MGLUtils.h +254 -0
  21. package/lib/commonjs/Cipher.js +307 -0
  22. package/lib/commonjs/Cipher.js.map +1 -1
  23. package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -0
  24. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  25. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  26. package/lib/commonjs/QuickCrypto.js +8 -0
  27. package/lib/commonjs/QuickCrypto.js.map +1 -1
  28. package/lib/commonjs/Utils.js +82 -1
  29. package/lib/commonjs/Utils.js.map +1 -1
  30. package/lib/commonjs/constants.js +86 -0
  31. package/lib/commonjs/constants.js.map +1 -0
  32. package/lib/commonjs/index.js +5 -0
  33. package/lib/commonjs/index.js.map +1 -1
  34. package/lib/commonjs/keys.js +207 -0
  35. package/lib/commonjs/keys.js.map +1 -0
  36. package/lib/module/Cipher.js +296 -3
  37. package/lib/module/Cipher.js.map +1 -1
  38. package/lib/module/NativeQuickCrypto/Cipher.js +9 -1
  39. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  40. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  41. package/lib/module/QuickCrypto.js +8 -1
  42. package/lib/module/QuickCrypto.js.map +1 -1
  43. package/lib/module/Utils.js +67 -1
  44. package/lib/module/Utils.js.map +1 -1
  45. package/lib/module/constants.js +79 -0
  46. package/lib/module/constants.js.map +1 -0
  47. package/lib/module/index.js +2 -0
  48. package/lib/module/index.js.map +1 -1
  49. package/lib/module/keys.js +193 -0
  50. package/lib/module/keys.js.map +1 -0
  51. package/lib/typescript/Cipher.d.ts +58 -1
  52. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +10 -0
  53. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +6 -1
  54. package/lib/typescript/QuickCrypto.d.ts +105 -1
  55. package/lib/typescript/Utils.d.ts +11 -0
  56. package/lib/typescript/constants.d.ts +75 -0
  57. package/lib/typescript/index.d.ts +2 -0
  58. package/lib/typescript/keys.d.ts +60 -0
  59. package/package.json +5 -5
  60. package/react-native-quick-crypto.podspec +1 -1
  61. package/src/.DS_Store +0 -0
  62. package/src/Cipher.ts +444 -3
  63. package/src/NativeQuickCrypto/Cipher.ts +44 -0
  64. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +13 -1
  65. package/src/QuickCrypto.ts +12 -0
  66. package/src/Utils.ts +91 -0
  67. package/src/constants.ts +79 -0
  68. package/src/index.ts +4 -0
  69. package/src/keys.ts +297 -0
@@ -0,0 +1,124 @@
1
+ //
2
+ // MGLCipherKeys.hpp
3
+ // react-native-fast-crypto
4
+ //
5
+ // Created by Oscar on 20.06.22.
6
+ //
7
+
8
+ #ifndef MGLCipherKeys_hpp
9
+ #define MGLCipherKeys_hpp
10
+
11
+ #include <jsi/jsi.h>
12
+ #include <openssl/evp.h>
13
+
14
+ #include <memory>
15
+ #include <optional>
16
+ #include <string>
17
+
18
+ #ifdef ANDROID
19
+ #include "JSIUtils/MGLUtils.h"
20
+ #else
21
+ #include "MGLUtils.h"
22
+ #endif
23
+
24
+ namespace margelo {
25
+
26
+ namespace jsi = facebook::jsi;
27
+
28
+ enum PKEncodingType {
29
+ // RSAPublicKey / RSAPrivateKey according to PKCS#1.
30
+ kKeyEncodingPKCS1,
31
+ // PrivateKeyInfo or EncryptedPrivateKeyInfo according to PKCS#8.
32
+ kKeyEncodingPKCS8,
33
+ // SubjectPublicKeyInfo according to X.509.
34
+ kKeyEncodingSPKI,
35
+ // ECPrivateKey according to SEC1.
36
+ kKeyEncodingSEC1
37
+ };
38
+
39
+ enum PKFormatType { kKeyFormatDER, kKeyFormatPEM, kKeyFormatJWK };
40
+
41
+ enum KeyType { kKeyTypeSecret, kKeyTypePublic, kKeyTypePrivate };
42
+
43
+ enum KeyEncodingContext {
44
+ kKeyContextInput,
45
+ kKeyContextExport,
46
+ kKeyContextGenerate
47
+ };
48
+
49
+ enum class ParseKeyResult {
50
+ kParseKeyOk,
51
+ kParseKeyNotRecognized,
52
+ kParseKeyNeedPassphrase,
53
+ kParseKeyFailed
54
+ };
55
+
56
+ struct AsymmetricKeyEncodingConfig {
57
+ bool output_key_object_ = false;
58
+ PKFormatType format_ = kKeyFormatDER;
59
+ std::optional<PKEncodingType> type_ = std::nullopt;
60
+ };
61
+
62
+ using PublicKeyEncodingConfig = AsymmetricKeyEncodingConfig;
63
+
64
+ struct PrivateKeyEncodingConfig : public AsymmetricKeyEncodingConfig {
65
+ const EVP_CIPHER* cipher_;
66
+ // The ByteSource alone is not enough to distinguish between "no passphrase"
67
+ // and a zero-length passphrase (which can be a null pointer), therefore, we
68
+ // use a NonCopyableMaybe.
69
+ NonCopyableMaybe<ByteSource> passphrase_;
70
+ };
71
+
72
+ // Here node uses extends MemoryRetainer no clue what that is, something with
73
+ // Snapshots stripped it for our implementation but if something doesn't work,
74
+ // you know why
75
+ class ManagedEVPPKey {
76
+ public:
77
+ ManagedEVPPKey() {}
78
+ explicit ManagedEVPPKey(EVPKeyPointer&& pkey);
79
+ ManagedEVPPKey(const ManagedEVPPKey& that);
80
+ ManagedEVPPKey& operator=(const ManagedEVPPKey& that);
81
+
82
+ operator bool() const;
83
+ EVP_PKEY* get() const;
84
+
85
+ static PublicKeyEncodingConfig GetPublicKeyEncodingFromJs(
86
+ jsi::Runtime& runtime, const jsi::Value* arguments, unsigned int* offset,
87
+ KeyEncodingContext context);
88
+
89
+ static NonCopyableMaybe<PrivateKeyEncodingConfig> GetPrivateKeyEncodingFromJs(
90
+ jsi::Runtime& runtime, const jsi::Value* arguments, unsigned int* offset,
91
+ KeyEncodingContext context);
92
+ //
93
+ static ManagedEVPPKey GetParsedKey(jsi::Runtime& runtime,
94
+ EVPKeyPointer&& pkey, ParseKeyResult ret,
95
+ const char* default_msg);
96
+
97
+ static ManagedEVPPKey GetPublicOrPrivateKeyFromJs(jsi::Runtime& runtime,
98
+ const jsi::Value* args,
99
+ unsigned int* offset);
100
+
101
+ // static ManagedEVPPKey GetPrivateKeyFromJs(
102
+ // const
103
+ // v8::FunctionCallbackInfo<v8::Value>&
104
+ // args, unsigned int* offset, bool
105
+ // allow_key_object);
106
+
107
+ static std::optional<StringOrBuffer> ToEncodedPublicKey(
108
+ jsi::Runtime& runtime, ManagedEVPPKey key,
109
+ const PublicKeyEncodingConfig& config);
110
+
111
+ static std::optional<StringOrBuffer> ToEncodedPrivateKey(
112
+ jsi::Runtime& runtime, ManagedEVPPKey key,
113
+ const PrivateKeyEncodingConfig& config);
114
+
115
+ private:
116
+ size_t size_of_private_key() const;
117
+ size_t size_of_public_key() const;
118
+
119
+ EVPKeyPointer pkey_;
120
+ };
121
+
122
+ } // namespace margelo
123
+
124
+ #endif /* MGLCipherKeys_hpp */
@@ -17,58 +17,61 @@ namespace margelo {
17
17
  FieldDefinition getCreateCipherFieldDefinition(
18
18
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
19
19
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
20
- return HOST_LAMBDA("createCipher", {
21
- if (count < 1) {
22
- throw jsi::JSError(runtime, "Params object is required");
23
- }
24
-
25
- if (!arguments[0].isObject()) {
26
- throw jsi::JSError(runtime, "createCipher: Params needs to be an object");
27
- }
28
-
29
- auto params = arguments[0].getObject(runtime);
30
-
31
- if (!params.hasProperty(runtime, "cipher_type")) {
32
- throw jsi::JSError(runtime, "createCipher: cipher_type is required");
33
- }
34
-
35
- auto cipher_type = params.getProperty(runtime, "cipher_type")
36
- .asString(runtime)
37
- .utf8(runtime);
38
-
39
- if (!params.hasProperty(runtime, "cipher_key")) {
40
- throw jsi::JSError(runtime, "createCipher: cipher_key is required");
41
- }
42
-
43
- auto cipher_key = params.getProperty(runtime, "cipher_key")
44
- .getObject(runtime)
45
- .getArrayBuffer(runtime);
46
-
47
- if (!params.hasProperty(runtime, "auth_tag_len")) {
48
- throw jsi::JSError(runtime, "createCipher: auth_tag_len is required");
49
- }
50
-
51
- unsigned auto int auth_tag_len =
52
- (int)params.getProperty(runtime, "auth_tag_len").getNumber();
53
-
54
- if (params.hasProperty(runtime, "iv") &&
55
- !params.getProperty(runtime, "iv").isNull() &&
56
- !params.getProperty(runtime, "iv").isUndefined()) { // createCipheriv
57
- auto iv = params.getProperty(runtime, "iv")
58
- .getObject(runtime)
59
- .getArrayBuffer(runtime);
60
- auto hostObject = std::make_shared<MGLCipherHostObject>(
61
- cipher_type, &cipher_key, true, auth_tag_len, &iv, runtime,
62
- jsCallInvoker, workerQueue);
63
-
64
- return jsi::Object::createFromHostObject(runtime, hostObject);
65
- } else {
66
- auto hostObject = std::make_shared<MGLCipherHostObject>(
67
- cipher_type, &cipher_key, true, auth_tag_len, runtime, jsCallInvoker,
68
- workerQueue);
69
-
70
- return jsi::Object::createFromHostObject(runtime, hostObject);
71
- }
72
- });
20
+ return buildPair(
21
+ "createCipher", JSIF([=]) {
22
+ if (count < 1) {
23
+ throw jsi::JSError(runtime, "Params object is required");
24
+ }
25
+
26
+ if (!arguments[0].isObject()) {
27
+ throw jsi::JSError(runtime,
28
+ "createCipher: Params needs to be an object");
29
+ }
30
+
31
+ auto params = arguments[0].getObject(runtime);
32
+
33
+ if (!params.hasProperty(runtime, "cipher_type")) {
34
+ throw jsi::JSError(runtime, "createCipher: cipher_type is required");
35
+ }
36
+
37
+ auto cipher_type = params.getProperty(runtime, "cipher_type")
38
+ .asString(runtime)
39
+ .utf8(runtime);
40
+
41
+ if (!params.hasProperty(runtime, "cipher_key")) {
42
+ throw jsi::JSError(runtime, "createCipher: cipher_key is required");
43
+ }
44
+
45
+ auto cipher_key = params.getProperty(runtime, "cipher_key")
46
+ .getObject(runtime)
47
+ .getArrayBuffer(runtime);
48
+
49
+ if (!params.hasProperty(runtime, "auth_tag_len")) {
50
+ throw jsi::JSError(runtime, "createCipher: auth_tag_len is required");
51
+ }
52
+
53
+ unsigned int auth_tag_len = static_cast<int>(
54
+ params.getProperty(runtime, "auth_tag_len").getNumber());
55
+
56
+ if (params.hasProperty(runtime, "iv") &&
57
+ !params.getProperty(runtime, "iv").isNull() &&
58
+ !params.getProperty(runtime, "iv")
59
+ .isUndefined()) { // createCipheriv
60
+ auto iv = params.getProperty(runtime, "iv")
61
+ .getObject(runtime)
62
+ .getArrayBuffer(runtime);
63
+ auto hostObject = std::make_shared<MGLCipherHostObject>(
64
+ cipher_type, &cipher_key, true, auth_tag_len, &iv, runtime,
65
+ jsCallInvoker, workerQueue);
66
+
67
+ return jsi::Object::createFromHostObject(runtime, hostObject);
68
+ } else {
69
+ auto hostObject = std::make_shared<MGLCipherHostObject>(
70
+ cipher_type, &cipher_key, true, auth_tag_len, runtime,
71
+ jsCallInvoker, workerQueue);
72
+
73
+ return jsi::Object::createFromHostObject(runtime, hostObject);
74
+ }
75
+ });
73
76
  }
74
77
  } // namespace margelo
@@ -1,3 +1,6 @@
1
+ #ifndef MGLCreateCipherInstaller_h
2
+ #define MGLCreateCipherInstaller_h
3
+
1
4
  #include <jsi/jsi.h>
2
5
 
3
6
  #include <memory>
@@ -15,3 +18,5 @@ FieldDefinition getCreateCipherFieldDefinition(
15
18
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
16
19
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
17
20
  } // namespace margelo
21
+
22
+ #endif
@@ -17,58 +17,61 @@ namespace margelo {
17
17
  FieldDefinition getCreateDecipherFieldDefinition(
18
18
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
19
19
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
20
- return HOST_LAMBDA("createDecipher", {
21
- if (count < 1) {
22
- throw jsi::JSError(runtime, "Params object is required");
23
- }
24
-
25
- if (!arguments[0].isObject()) {
26
- throw jsi::JSError(runtime, "createCipher: Params needs to be an object");
27
- }
28
-
29
- auto params = arguments[0].getObject(runtime);
30
-
31
- if (!params.hasProperty(runtime, "cipher_type")) {
32
- throw jsi::JSError(runtime, "createCipher: cipher_type is required");
33
- }
34
-
35
- auto cipher_type = params.getProperty(runtime, "cipher_type")
36
- .asString(runtime)
37
- .utf8(runtime);
38
-
39
- if (!params.hasProperty(runtime, "cipher_key")) {
40
- throw jsi::JSError(runtime, "createCipher: cipher_key is required");
41
- }
42
-
43
- auto cipher_key = params.getProperty(runtime, "cipher_key")
44
- .getObject(runtime)
45
- .getArrayBuffer(runtime);
46
-
47
- if (!params.hasProperty(runtime, "auth_tag_len")) {
48
- throw jsi::JSError(runtime, "createCipher: auth_tag_len is required");
49
- }
50
-
51
- unsigned auto int auth_tag_len =
52
- (int)params.getProperty(runtime, "auth_tag_len").getNumber();
53
-
54
- if (params.hasProperty(runtime, "iv") &&
55
- !params.getProperty(runtime, "iv").isNull() &&
56
- !params.getProperty(runtime, "iv").isUndefined()) { // createDecipheriv
57
- auto iv = params.getProperty(runtime, "iv")
58
- .getObject(runtime)
59
- .getArrayBuffer(runtime);
60
- auto hostObject = std::make_shared<MGLCipherHostObject>(
61
- cipher_type, &cipher_key, false, auth_tag_len, &iv, runtime,
62
- jsCallInvoker, workerQueue);
63
-
64
- return jsi::Object::createFromHostObject(runtime, hostObject);
65
- } else {
66
- auto hostObject = std::make_shared<MGLCipherHostObject>(
67
- cipher_type, &cipher_key, false, auth_tag_len, runtime, jsCallInvoker,
68
- workerQueue);
69
-
70
- return jsi::Object::createFromHostObject(runtime, hostObject);
71
- }
72
- });
20
+ return buildPair(
21
+ "createDecipher", JSIF([=]) {
22
+ if (count < 1) {
23
+ throw jsi::JSError(runtime, "Params object is required");
24
+ }
25
+
26
+ if (!arguments[0].isObject()) {
27
+ throw jsi::JSError(runtime,
28
+ "createCipher: Params needs to be an object");
29
+ }
30
+
31
+ auto params = arguments[0].getObject(runtime);
32
+
33
+ if (!params.hasProperty(runtime, "cipher_type")) {
34
+ throw jsi::JSError(runtime, "createCipher: cipher_type is required");
35
+ }
36
+
37
+ auto cipher_type = params.getProperty(runtime, "cipher_type")
38
+ .asString(runtime)
39
+ .utf8(runtime);
40
+
41
+ if (!params.hasProperty(runtime, "cipher_key")) {
42
+ throw jsi::JSError(runtime, "createCipher: cipher_key is required");
43
+ }
44
+
45
+ auto cipher_key = params.getProperty(runtime, "cipher_key")
46
+ .getObject(runtime)
47
+ .getArrayBuffer(runtime);
48
+
49
+ if (!params.hasProperty(runtime, "auth_tag_len")) {
50
+ throw jsi::JSError(runtime, "createCipher: auth_tag_len is required");
51
+ }
52
+
53
+ unsigned int auth_tag_len =
54
+ (int)params.getProperty(runtime, "auth_tag_len").getNumber();
55
+
56
+ if (params.hasProperty(runtime, "iv") &&
57
+ !params.getProperty(runtime, "iv").isNull() &&
58
+ !params.getProperty(runtime, "iv")
59
+ .isUndefined()) { // createDecipheriv
60
+ auto iv = params.getProperty(runtime, "iv")
61
+ .getObject(runtime)
62
+ .getArrayBuffer(runtime);
63
+ auto hostObject = std::make_shared<MGLCipherHostObject>(
64
+ cipher_type, &cipher_key, false, auth_tag_len, &iv, runtime,
65
+ jsCallInvoker, workerQueue);
66
+
67
+ return jsi::Object::createFromHostObject(runtime, hostObject);
68
+ } else {
69
+ auto hostObject = std::make_shared<MGLCipherHostObject>(
70
+ cipher_type, &cipher_key, false, auth_tag_len, runtime,
71
+ jsCallInvoker, workerQueue);
72
+
73
+ return jsi::Object::createFromHostObject(runtime, hostObject);
74
+ }
75
+ });
73
76
  }
74
77
  } // namespace margelo
@@ -0,0 +1,107 @@
1
+ //
2
+ // MGLGenerateKeyPairInstaller.cpp
3
+ // react-native-quick-crypto
4
+ //
5
+ // Created by Oscar on 24.06.22.
6
+ //
7
+
8
+ #include "MGLGenerateKeyPairInstaller.h"
9
+
10
+ #include <iostream>
11
+ #include <memory>
12
+ #include <mutex>
13
+ #include <thread>
14
+ #include <utility>
15
+
16
+ #include "MGLRsa.h"
17
+
18
+ #ifdef ANDROID
19
+ #include "JSIUtils/MGLJSIMacros.h"
20
+ #include "JSIUtils/MGLTypedArray.h"
21
+ #else
22
+ #include "MGLJSIMacros.h"
23
+ #include "MGLTypedArray.h"
24
+ #endif
25
+
26
+ using namespace facebook;
27
+
28
+ namespace margelo {
29
+
30
+ std::mutex m;
31
+
32
+ // Current implementation only supports RSA schemes (check line config.variant =
33
+ // ) As more encryption schemes are added this will require an abstraction that
34
+ // supports more schemes
35
+ FieldDefinition getGenerateKeyPairFieldDefinition(
36
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
37
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
38
+ return buildPair(
39
+ "generateKeyPair", JSIF([=]) {
40
+ auto config = std::make_shared<RsaKeyPairGenConfig>(
41
+ prepareRsaKeyGenConfig(runtime, arguments));
42
+ auto promiseConstructor =
43
+ runtime.global().getPropertyAsFunction(runtime, "Promise");
44
+
45
+ auto promise = promiseConstructor.callAsConstructor(
46
+ runtime,
47
+ jsi::Function::createFromHostFunction(
48
+ runtime, jsi::PropNameID::forAscii(runtime, "executor"), 2,
49
+ [arguments, &jsCallInvoker, config](
50
+ jsi::Runtime &runtime, const jsi::Value &,
51
+ const jsi::Value *promiseArgs, size_t) -> jsi::Value {
52
+ auto resolve =
53
+ std::make_shared<jsi::Value>(runtime, promiseArgs[0]);
54
+ auto reject =
55
+ std::make_shared<jsi::Value>(runtime, promiseArgs[1]);
56
+
57
+ std::thread t([&runtime, arguments, resolve, reject,
58
+ jsCallInvoker, config]() {
59
+ m.lock();
60
+ try {
61
+ const auto keys = generateRSAKeyPair(runtime, config);
62
+ jsCallInvoker->invokeAsync([&runtime, keys, jsCallInvoker,
63
+ resolve]() {
64
+ if (keys.first.isString && keys.second.isString) {
65
+ auto publicKey = jsi::String::createFromUtf8(
66
+ runtime, keys.first.stringValue);
67
+ auto privateKey = jsi::String::createFromUtf8(
68
+ runtime, keys.second.stringValue);
69
+ auto res = jsi::Array::createWithElements(
70
+ runtime, jsi::Value::undefined(), publicKey,
71
+ privateKey);
72
+ resolve->asObject(runtime).asFunction(runtime).call(
73
+ runtime, std::move(res));
74
+ } else {
75
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array>
76
+ publicKeyBuffer(runtime, keys.first.vectorValue);
77
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array>
78
+ privateKeyBuffer(runtime,
79
+ keys.second.vectorValue);
80
+
81
+ auto res = jsi::Array::createWithElements(
82
+ runtime, jsi::Value::undefined(), publicKeyBuffer,
83
+ privateKeyBuffer);
84
+ resolve->asObject(runtime).asFunction(runtime).call(
85
+ runtime, std::move(res));
86
+ }
87
+ });
88
+ } catch (std::exception e) {
89
+ jsCallInvoker->invokeAsync(
90
+ [&runtime, &jsCallInvoker, reject]() {
91
+ reject->asObject(runtime).asFunction(runtime).call(
92
+ runtime, jsi::String::createFromUtf8(
93
+ runtime, "Error generating key"));
94
+ });
95
+ }
96
+ m.unlock();
97
+ });
98
+
99
+ t.detach();
100
+
101
+ return {};
102
+ }));
103
+
104
+ return promise;
105
+ });
106
+ }
107
+ } // namespace margelo
@@ -0,0 +1,32 @@
1
+
2
+ //
3
+ // MGLGenerateKeyPairInstaller.hpp
4
+ // react-native-quick-crypto
5
+ //
6
+ // Created by Oscar on 22.06.22.
7
+ //
8
+
9
+ #ifndef MGLGenerateKeyPairInstaller_hpp
10
+ #define MGLGenerateKeyPairInstaller_hpp
11
+
12
+ #include <jsi/jsi.h>
13
+
14
+ #include <memory>
15
+
16
+ #ifdef ANDROID
17
+ #include "JSIUtils/MGLSmartHostObject.h"
18
+ #else
19
+ #include "MGLSmartHostObject.h"
20
+ #endif
21
+ #include "MGLCipherKeys.h"
22
+ #include "MGLRsa.h"
23
+ #include "MGLUtils.h"
24
+
25
+ namespace margelo {
26
+
27
+ FieldDefinition getGenerateKeyPairFieldDefinition(
28
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
29
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
30
+ } // namespace margelo
31
+
32
+ #endif /* MGLGenerateKeyPairInstaller_hpp */
@@ -0,0 +1,60 @@
1
+ //
2
+ // MGLGenerateKeyPairInstaller.cpp
3
+ // react-native-quick-crypto
4
+ //
5
+ // Created by Oscar on 22.06.22.
6
+ //
7
+
8
+ #include "MGLGenerateKeyPairSyncInstaller.h"
9
+
10
+ #include <iostream>
11
+ #include <memory>
12
+ #include <utility>
13
+
14
+ #include "MGLRsa.h"
15
+
16
+ #ifdef ANDROID
17
+ #include "JSIUtils/MGLJSIMacros.h"
18
+ #include "JSIUtils/MGLJSIUtils.h"
19
+ #include "JSIUtils/MGLTypedArray.h"
20
+ #else
21
+ #include "MGLJSIMacros.h"
22
+ #include "MGLJSIUtils.h"
23
+ #include "MGLTypedArray.h"
24
+ #endif
25
+
26
+ using namespace facebook;
27
+
28
+ namespace margelo {
29
+
30
+ // Current implementation only supports RSA schemes (check line config.variant =
31
+ // ) As more encryption schemes are added this will require an abstraction that
32
+ // supports more schemes
33
+ FieldDefinition getGenerateKeyPairSyncFieldDefinition(
34
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
35
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
36
+ return buildPair(
37
+ "generateKeyPairSync", JSIF([=]) {
38
+ auto config = std::make_shared<RsaKeyPairGenConfig>(
39
+ prepareRsaKeyGenConfig(runtime, arguments));
40
+ auto keys = generateRSAKeyPair(runtime, std::move(config));
41
+ if (keys.first.isString && keys.second.isString) {
42
+ auto publicKey =
43
+ jsi::String::createFromUtf8(runtime, keys.first.stringValue);
44
+ auto privateKey =
45
+ jsi::String::createFromUtf8(runtime, keys.second.stringValue);
46
+ return jsi::Array::createWithElements(
47
+ runtime, jsi::Value::undefined(), publicKey, privateKey);
48
+ } else {
49
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array> publicKeyBuffer(
50
+ runtime, keys.first.vectorValue);
51
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array> privateKeyBuffer(
52
+ runtime, keys.second.vectorValue);
53
+
54
+ return jsi::Array::createWithElements(
55
+ runtime, jsi::Value::undefined(), publicKeyBuffer,
56
+ privateKeyBuffer);
57
+ }
58
+ });
59
+ }
60
+ } // namespace margelo
@@ -0,0 +1,35 @@
1
+ //
2
+ // MGLGenerateKeyPairInstaller.hpp
3
+ // react-native-quick-crypto
4
+ //
5
+ // Created by Oscar on 22.06.22.
6
+ //
7
+
8
+ #ifndef MGLGenerateKeyPairSyncInstaller_hpp
9
+ #define MGLGenerateKeyPairSyncInstaller_hpp
10
+
11
+ #include <jsi/jsi.h>
12
+
13
+ #include <memory>
14
+
15
+ #ifdef ANDROID
16
+ #include "JSIUtils/MGLSmartHostObject.h"
17
+ #else
18
+ #include "MGLSmartHostObject.h"
19
+ #endif
20
+ #include "MGLCipherKeys.h"
21
+ #include "MGLRsa.h"
22
+ #include "MGLUtils.h"
23
+
24
+ namespace margelo {
25
+
26
+ // https://nodejs.org/api/crypto.html go to generateKeyPair
27
+ /// It's signature is:
28
+ /// generateKeyPair(type: string, options: record, callback: (error, publicKey,
29
+ /// privateKey))
30
+ FieldDefinition getGenerateKeyPairSyncFieldDefinition(
31
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
32
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
33
+ } // namespace margelo
34
+
35
+ #endif /* MGLGenerateKeyPairInstaller_hpp */