forked from margelo/react-native-quick-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMGLRandomHostObject.cpp
More file actions
96 lines (81 loc) · 3.04 KB
/
MGLRandomHostObject.cpp
File metadata and controls
96 lines (81 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Created by Szymon on 25/02/2022.
//
#include "MGLRandomHostObject.h"
#ifdef ANDROID
#include "JSIUtils/MGLTypedArray.h"
#else
#include "MGLTypedArray.h"
#endif
#include <openssl/bn.h>
#include <openssl/dsa.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <memory>
#include <utility>
namespace margelo {
namespace jsi = facebook::jsi;
namespace react = facebook::react;
MGLRandomHostObject::MGLRandomHostObject(
std::shared_ptr<react::CallInvoker> jsCallInvoker,
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
: MGLSmartHostObject(jsCallInvoker, workerQueue) {
this->fields.push_back(buildPair(
"randomFill", JSIF([=]) {
if (count != 3) {
throw jsi::JSError(runtime,
"randomFill(..) expects exactly 4 arguments!");
}
if(!arguments[0].isObject() || !arguments[0].asObject(runtime).isArrayBuffer(runtime)) {
throw std::runtime_error("First argument it not an array buffer");
}
if (!arguments[0].isObject()
|| !arguments[0].asObject(runtime).isArrayBuffer(runtime)) {
throw std::runtime_error("First argument it not an array buffer");
}
auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
auto *resultData = result.data(runtime);
auto resultPreventGC =
std::make_shared<jsi::ArrayBuffer>(std::move(result));
auto offset = (int)arguments[1].asNumber();
auto size = arguments[2].asNumber();
return react::createPromiseAsJSIValue(
runtime, [=](jsi::Runtime &runtime,
std::shared_ptr<react::Promise> promise) {
// TODO(Szymon) implement check prime once we have bignums
this->runOnWorkerThread([=]() {
if (RAND_bytes(resultData + offset, size) != 1) {
this->runOnJSThread([=]() {
promise->reject("Sth went wrong with RAND_bytes");
});
}
this->runOnJSThread([=]() {
promise->resolve(
jsi::ArrayBuffer(std::move(*resultPreventGC)));
});
});
});
}));
this->fields.push_back(buildPair(
"randomFillSync", JSIF([=]) {
if (count != 3) {
throw jsi::JSError(runtime,
"randomFillSync(..) expects exactly 4 arguments!");
}
auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
auto *resultData = result.data(runtime);
auto offset = (int)arguments[1].asNumber();
auto size = arguments[2].asNumber();
if (RAND_bytes(resultData + offset, size) != 1) {
throw jsi::JSError(runtime, "Sth went wrong with RAND_bytes" +
std::to_string(ERR_get_error()));
}
return result;
}));
}
} // namespace margelo