react-native-quick-crypto 0.2.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.
- package/LICENSE +21 -0
- package/README.md +84 -0
- package/android/.DS_Store +0 -0
- package/android/CMakeLists.txt +117 -0
- package/android/build.gradle +367 -0
- package/android/gradle/.DS_Store +0 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/android/gradle.properties +6 -0
- package/android/gradlew +183 -0
- package/android/gradlew.bat +100 -0
- package/android/src/.DS_Store +0 -0
- package/android/src/main/.DS_Store +0 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/cpp/cpp-adapter.cpp +60 -0
- package/android/src/main/java/.DS_Store +0 -0
- package/android/src/main/java/com/.DS_Store +0 -0
- package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoModule.java +70 -0
- package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoPackage.java +26 -0
- package/cpp/.DS_Store +0 -0
- package/cpp/Cipher/MGLCipherHostObject.cpp +632 -0
- package/cpp/Cipher/MGLCipherHostObject.h +90 -0
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +74 -0
- package/cpp/Cipher/MGLCreateCipherInstaller.h +17 -0
- package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +74 -0
- package/cpp/Cipher/MGLCreateDecipherInstaller.h +17 -0
- package/cpp/HMAC/MGLHmacHostObject.cpp +95 -0
- package/cpp/HMAC/MGLHmacHostObject.h +39 -0
- package/cpp/HMAC/MGLHmacInstaller.cpp +43 -0
- package/cpp/HMAC/MGLHmacInstaller.h +20 -0
- package/cpp/Hash/MGLHashHostObject.cpp +147 -0
- package/cpp/Hash/MGLHashHostObject.h +53 -0
- package/cpp/Hash/MGLHashInstaller.cpp +46 -0
- package/cpp/Hash/MGLHashInstaller.h +20 -0
- package/cpp/JSIUtils/MGLJSIMacros.h +37 -0
- package/cpp/JSIUtils/MGLSmartHostObject.cpp +43 -0
- package/cpp/JSIUtils/MGLSmartHostObject.h +46 -0
- package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +24 -0
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +43 -0
- package/cpp/JSIUtils/MGLTypedArray.cpp +325 -0
- package/cpp/JSIUtils/MGLTypedArray.h +160 -0
- package/cpp/MGLQuickCryptoHostObject.cpp +64 -0
- package/cpp/MGLQuickCryptoHostObject.h +30 -0
- package/cpp/Random/MGLRandomHostObject.cpp +89 -0
- package/cpp/Random/MGLRandomHostObject.h +27 -0
- package/cpp/Utils/MGLDispatchQueue.cpp +75 -0
- package/cpp/Utils/MGLDispatchQueue.h +55 -0
- package/cpp/Utils/logs.h +38 -0
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +164 -0
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +29 -0
- package/cpp/fastpbkdf2/fastpbkdf2.c +352 -0
- package/cpp/fastpbkdf2/fastpbkdf2.h +68 -0
- package/ios/.DS_Store +0 -0
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +274 -0
- package/ios/QuickCryptoModule.h +5 -0
- package/ios/QuickCryptoModule.mm +42 -0
- package/lib/.DS_Store +0 -0
- package/lib/commonjs/Cipher.js +293 -0
- package/lib/commonjs/Cipher.js.map +1 -0
- package/lib/commonjs/Hash.js +102 -0
- package/lib/commonjs/Hash.js.map +1 -0
- package/lib/commonjs/Hmac.js +104 -0
- package/lib/commonjs/Hmac.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +61 -0
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/hash.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/hmac.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/random.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -0
- package/lib/commonjs/QuickCrypto.js +35 -0
- package/lib/commonjs/QuickCrypto.js.map +1 -0
- package/lib/commonjs/Utils.js +152 -0
- package/lib/commonjs/Utils.js.map +1 -0
- package/lib/commonjs/index.js +19 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/pbkdf2.js +64 -0
- package/lib/commonjs/pbkdf2.js.map +1 -0
- package/lib/commonjs/random.js +213 -0
- package/lib/commonjs/random.js.map +1 -0
- package/lib/module/Cipher.js +287 -0
- package/lib/module/Cipher.js.map +1 -0
- package/lib/module/Hash.js +87 -0
- package/lib/module/Hash.js.map +1 -0
- package/lib/module/Hmac.js +90 -0
- package/lib/module/Hmac.js.map +1 -0
- package/lib/module/NativeQuickCrypto/Cipher.js +2 -0
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -0
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +53 -0
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -0
- package/lib/module/NativeQuickCrypto/hash.js +2 -0
- package/lib/module/NativeQuickCrypto/hash.js.map +1 -0
- package/lib/module/NativeQuickCrypto/hmac.js +2 -0
- package/lib/module/NativeQuickCrypto/hmac.js.map +1 -0
- package/lib/module/NativeQuickCrypto/pbkdf2.js +2 -0
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -0
- package/lib/module/NativeQuickCrypto/random.js +2 -0
- package/lib/module/NativeQuickCrypto/random.js.map +1 -0
- package/lib/module/QuickCrypto.js +18 -0
- package/lib/module/QuickCrypto.js.map +1 -0
- package/lib/module/Utils.js +131 -0
- package/lib/module/Utils.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/pbkdf2.js +51 -0
- package/lib/module/pbkdf2.js.map +1 -0
- package/lib/module/random.js +190 -0
- package/lib/module/random.js.map +1 -0
- package/lib/typescript/Cipher.d.ts +30 -0
- package/lib/typescript/Hash.d.ts +44 -0
- package/lib/typescript/Hmac.d.ts +37 -0
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +22 -0
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +19 -0
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +6 -0
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +5 -0
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +4 -0
- package/lib/typescript/NativeQuickCrypto/random.d.ts +4 -0
- package/lib/typescript/QuickCrypto.d.ts +31 -0
- package/lib/typescript/Utils.d.ts +13 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/pbkdf2.d.ts +9 -0
- package/lib/typescript/random.d.ts +20 -0
- package/package.json +168 -0
- package/react-native-quick-crypto.podspec +44 -0
- package/src/Cipher.ts +322 -0
- package/src/Hash.ts +98 -0
- package/src/Hmac.ts +107 -0
- package/src/NativeQuickCrypto/Cipher.ts +25 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +79 -0
- package/src/NativeQuickCrypto/hash.ts +10 -0
- package/src/NativeQuickCrypto/hmac.ts +9 -0
- package/src/NativeQuickCrypto/pbkdf2.ts +16 -0
- package/src/NativeQuickCrypto/random.ts +12 -0
- package/src/QuickCrypto.ts +23 -0
- package/src/Utils.ts +151 -0
- package/src/index.ts +1 -0
- package/src/pbkdf2.ts +96 -0
- package/src/random.ts +277 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//
|
|
2
|
+
// TypedArray.h
|
|
3
|
+
// react-native-quick-crypto
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 31.10.21.
|
|
6
|
+
// Originally created by Expo (expo-gl)
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include <jsi/jsi.h>
|
|
12
|
+
|
|
13
|
+
#include <utility>
|
|
14
|
+
#include <vector>
|
|
15
|
+
|
|
16
|
+
namespace jsi = facebook::jsi;
|
|
17
|
+
|
|
18
|
+
enum class MGLTypedArrayKind {
|
|
19
|
+
Int8Array,
|
|
20
|
+
Int16Array,
|
|
21
|
+
Int32Array,
|
|
22
|
+
Uint8Array,
|
|
23
|
+
Uint8ClampedArray,
|
|
24
|
+
Uint16Array,
|
|
25
|
+
Uint32Array,
|
|
26
|
+
Float32Array,
|
|
27
|
+
Float64Array,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
template <MGLTypedArrayKind T>
|
|
31
|
+
class MGLTypedArray;
|
|
32
|
+
|
|
33
|
+
template <MGLTypedArrayKind T>
|
|
34
|
+
struct typedArrayTypeMap;
|
|
35
|
+
template <>
|
|
36
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Int8Array> {
|
|
37
|
+
typedef int8_t type;
|
|
38
|
+
};
|
|
39
|
+
template <>
|
|
40
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Int16Array> {
|
|
41
|
+
typedef int16_t type;
|
|
42
|
+
};
|
|
43
|
+
template <>
|
|
44
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Int32Array> {
|
|
45
|
+
typedef int32_t type;
|
|
46
|
+
};
|
|
47
|
+
template <>
|
|
48
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Uint8Array> {
|
|
49
|
+
typedef uint8_t type;
|
|
50
|
+
};
|
|
51
|
+
template <>
|
|
52
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Uint8ClampedArray> {
|
|
53
|
+
typedef uint8_t type;
|
|
54
|
+
};
|
|
55
|
+
template <>
|
|
56
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Uint16Array> {
|
|
57
|
+
typedef uint16_t type;
|
|
58
|
+
};
|
|
59
|
+
template <>
|
|
60
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Uint32Array> {
|
|
61
|
+
typedef uint32_t type;
|
|
62
|
+
};
|
|
63
|
+
template <>
|
|
64
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Float32Array> {
|
|
65
|
+
typedef float type;
|
|
66
|
+
};
|
|
67
|
+
template <>
|
|
68
|
+
struct typedArrayTypeMap<MGLTypedArrayKind::Float64Array> {
|
|
69
|
+
typedef double type;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
void invalidateJsiPropNameIDCache();
|
|
73
|
+
|
|
74
|
+
class MGLTypedArrayBase : public jsi::Object {
|
|
75
|
+
public:
|
|
76
|
+
template <MGLTypedArrayKind T>
|
|
77
|
+
using ContentType = typename typedArrayTypeMap<T>::type;
|
|
78
|
+
|
|
79
|
+
MGLTypedArrayBase(jsi::Runtime &, size_t, MGLTypedArrayKind);
|
|
80
|
+
MGLTypedArrayBase(jsi::Runtime &, const jsi::Object &);
|
|
81
|
+
MGLTypedArrayBase(MGLTypedArrayBase &&) = default;
|
|
82
|
+
MGLTypedArrayBase &operator=(MGLTypedArrayBase &&) = default;
|
|
83
|
+
|
|
84
|
+
MGLTypedArrayKind getKind(jsi::Runtime &runtime) const;
|
|
85
|
+
|
|
86
|
+
template <MGLTypedArrayKind T>
|
|
87
|
+
MGLTypedArray<T> get(jsi::Runtime &runtime) const &;
|
|
88
|
+
template <MGLTypedArrayKind T>
|
|
89
|
+
MGLTypedArray<T> get(jsi::Runtime &runtime) &&;
|
|
90
|
+
template <MGLTypedArrayKind T>
|
|
91
|
+
MGLTypedArray<T> as(jsi::Runtime &runtime) const &;
|
|
92
|
+
template <MGLTypedArrayKind T>
|
|
93
|
+
MGLTypedArray<T> as(jsi::Runtime &runtime) &&;
|
|
94
|
+
|
|
95
|
+
size_t size(jsi::Runtime &runtime) const;
|
|
96
|
+
size_t length(jsi::Runtime &runtime) const;
|
|
97
|
+
size_t byteLength(jsi::Runtime &runtime) const;
|
|
98
|
+
size_t byteOffset(jsi::Runtime &runtime) const;
|
|
99
|
+
bool hasBuffer(jsi::Runtime &runtime) const;
|
|
100
|
+
|
|
101
|
+
std::vector<uint8_t> toVector(jsi::Runtime &runtime);
|
|
102
|
+
jsi::ArrayBuffer getBuffer(jsi::Runtime &runtime) const;
|
|
103
|
+
|
|
104
|
+
private:
|
|
105
|
+
template <MGLTypedArrayKind>
|
|
106
|
+
friend class MGLTypedArray;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
|
|
110
|
+
MGLTypedArrayBase getTypedArray(jsi::Runtime &runtime,
|
|
111
|
+
const jsi::Object &jsObj);
|
|
112
|
+
|
|
113
|
+
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime,
|
|
114
|
+
jsi::Object &jsObj);
|
|
115
|
+
void arrayBufferUpdate(jsi::Runtime &runtime, jsi::ArrayBuffer &buffer,
|
|
116
|
+
std::vector<uint8_t> data, size_t offset);
|
|
117
|
+
|
|
118
|
+
template <MGLTypedArrayKind T>
|
|
119
|
+
class MGLTypedArray : public MGLTypedArrayBase {
|
|
120
|
+
public:
|
|
121
|
+
MGLTypedArray(jsi::Runtime &runtime, size_t size);
|
|
122
|
+
MGLTypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data);
|
|
123
|
+
explicit MGLTypedArray(MGLTypedArrayBase &&base);
|
|
124
|
+
explicit MGLTypedArray(MGLTypedArray &&) = default;
|
|
125
|
+
MGLTypedArray &operator=(MGLTypedArray &&) = default;
|
|
126
|
+
|
|
127
|
+
std::vector<ContentType<T>> toVector(jsi::Runtime &runtime);
|
|
128
|
+
void update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
template <MGLTypedArrayKind T>
|
|
132
|
+
MGLTypedArray<T> MGLTypedArrayBase::get(jsi::Runtime &runtime) const & {
|
|
133
|
+
assert(getKind(runtime) == T);
|
|
134
|
+
(void)runtime; // when assert is disabled we need to mark this as used
|
|
135
|
+
return MGLTypedArray<T>(
|
|
136
|
+
jsi::Value(runtime, jsi::Value(runtime, *this).asObject(runtime)));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
template <MGLTypedArrayKind T>
|
|
140
|
+
MGLTypedArray<T> MGLTypedArrayBase::get(jsi::Runtime &runtime) && {
|
|
141
|
+
assert(getKind(runtime) == T);
|
|
142
|
+
(void)runtime; // when assert is disabled we need to mark this as used
|
|
143
|
+
return MGLTypedArray<T>(std::move(*this));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
template <MGLTypedArrayKind T>
|
|
147
|
+
MGLTypedArray<T> MGLTypedArrayBase::as(jsi::Runtime &runtime) const & {
|
|
148
|
+
if (getKind(runtime) != T) {
|
|
149
|
+
throw jsi::JSError(runtime, "Object is not a MGLTypedArray");
|
|
150
|
+
}
|
|
151
|
+
return get<T>(runtime);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
template <MGLTypedArrayKind T>
|
|
155
|
+
MGLTypedArray<T> MGLTypedArrayBase::as(jsi::Runtime &runtime) && {
|
|
156
|
+
if (getKind(runtime) != T) {
|
|
157
|
+
throw jsi::JSError(runtime, "Object is not a MGLTypedArray");
|
|
158
|
+
}
|
|
159
|
+
return std::move(*this).get<T>(runtime);
|
|
160
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2022 Margelo
|
|
2
|
+
#include "MGLQuickCryptoHostObject.h"
|
|
3
|
+
|
|
4
|
+
#include <ReactCommon/TurboModuleUtils.h>
|
|
5
|
+
#include <jsi/jsi.h>
|
|
6
|
+
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#ifdef ANDROID
|
|
11
|
+
#include "Cipher/MGLCreateCipherInstaller.h"
|
|
12
|
+
#include "Cipher/MGLCreateDecipherInstaller.h"
|
|
13
|
+
#include "HMAC/MGLHmacInstaller.h"
|
|
14
|
+
#include "Hash/MGLHashInstaller.h"
|
|
15
|
+
#include "Random/MGLRandomHostObject.h"
|
|
16
|
+
#include "fastpbkdf2/MGLPbkdf2HostObject.h"
|
|
17
|
+
#else
|
|
18
|
+
#include "MGLCreateCipherInstaller.h"
|
|
19
|
+
#include "MGLCreateDecipherInstaller.h"
|
|
20
|
+
#include "MGLHashInstaller.h"
|
|
21
|
+
#include "MGLHmacInstaller.h"
|
|
22
|
+
#include "MGLPbkdf2HostObject.h"
|
|
23
|
+
#include "MGLRandomHostObject.h"
|
|
24
|
+
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
namespace margelo {
|
|
28
|
+
|
|
29
|
+
namespace jsi = facebook::jsi;
|
|
30
|
+
|
|
31
|
+
MGLQuickCryptoHostObject::MGLQuickCryptoHostObject(
|
|
32
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
33
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
|
|
34
|
+
: MGLSmartHostObject(jsCallInvoker, workerQueue) {
|
|
35
|
+
// HmacInstaller
|
|
36
|
+
this->fields.push_back(getHmacFieldDefinition(jsCallInvoker, workerQueue));
|
|
37
|
+
|
|
38
|
+
// HashInstaller
|
|
39
|
+
this->fields.push_back(getHashFieldDefinition(jsCallInvoker, workerQueue));
|
|
40
|
+
|
|
41
|
+
// CreateCipherInstaller
|
|
42
|
+
this->fields.push_back(
|
|
43
|
+
getCreateCipherFieldDefinition(jsCallInvoker, workerQueue));
|
|
44
|
+
|
|
45
|
+
// CreateDecipherInstaller
|
|
46
|
+
this->fields.push_back(
|
|
47
|
+
getCreateDecipherFieldDefinition(jsCallInvoker, workerQueue));
|
|
48
|
+
|
|
49
|
+
// Pbkdf2HostObject
|
|
50
|
+
this->fields.push_back(JSI_VALUE("pbkdf2", {
|
|
51
|
+
auto hostObject =
|
|
52
|
+
std::make_shared<MGLPbkdf2HostObject>(jsCallInvoker, workerQueue);
|
|
53
|
+
return jsi::Object::createFromHostObject(runtime, hostObject);
|
|
54
|
+
}));
|
|
55
|
+
|
|
56
|
+
// RandomHostObject
|
|
57
|
+
this->fields.push_back(JSI_VALUE("random", {
|
|
58
|
+
auto hostObject =
|
|
59
|
+
std::make_shared<MGLRandomHostObject>(jsCallInvoker, workerQueue);
|
|
60
|
+
return jsi::Object::createFromHostObject(runtime, hostObject);
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
} // namespace margelo
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2022 Margelo
|
|
2
|
+
#ifndef CPP_FASTCRYPTOHOSTOBJECT_H_
|
|
3
|
+
#define CPP_FASTCRYPTOHOSTOBJECT_H_
|
|
4
|
+
|
|
5
|
+
#include <ReactCommon/CallInvoker.h>
|
|
6
|
+
#include <jsi/jsi.h>
|
|
7
|
+
|
|
8
|
+
#include <memory>
|
|
9
|
+
|
|
10
|
+
#include "JSIUtils/MGLSmartHostObject.h"
|
|
11
|
+
#include "JSIUtils/MGLTypedArray.h"
|
|
12
|
+
#include "Utils/MGLDispatchQueue.h"
|
|
13
|
+
|
|
14
|
+
namespace margelo {
|
|
15
|
+
|
|
16
|
+
namespace jsi = facebook::jsi;
|
|
17
|
+
namespace react = facebook::react;
|
|
18
|
+
|
|
19
|
+
class JSI_EXPORT MGLQuickCryptoHostObject : public MGLSmartHostObject {
|
|
20
|
+
public:
|
|
21
|
+
explicit MGLQuickCryptoHostObject(
|
|
22
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
23
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
24
|
+
|
|
25
|
+
virtual ~MGLQuickCryptoHostObject() { invalidateJsiPropNameIDCache(); }
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
} // namespace margelo
|
|
29
|
+
|
|
30
|
+
#endif // CPP_FASTCRYPTOHOSTOBJECT_H_
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Szymon on 25/02/2022.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include "MGLRandomHostObject.h"
|
|
6
|
+
|
|
7
|
+
#ifdef ANDROID
|
|
8
|
+
#include "JSIUtils/MGLTypedArray.h"
|
|
9
|
+
#else
|
|
10
|
+
#include "MGLTypedArray.h"
|
|
11
|
+
#endif
|
|
12
|
+
#include <openssl/bn.h>
|
|
13
|
+
#include <openssl/dsa.h>
|
|
14
|
+
#include <openssl/ec.h>
|
|
15
|
+
#include <openssl/err.h>
|
|
16
|
+
#include <openssl/evp.h>
|
|
17
|
+
#include <openssl/kdf.h>
|
|
18
|
+
#include <openssl/rand.h>
|
|
19
|
+
#include <openssl/rsa.h>
|
|
20
|
+
#include <openssl/ssl.h>
|
|
21
|
+
|
|
22
|
+
#include <memory>
|
|
23
|
+
#include <utility>
|
|
24
|
+
|
|
25
|
+
namespace margelo {
|
|
26
|
+
namespace jsi = facebook::jsi;
|
|
27
|
+
namespace react = facebook::react;
|
|
28
|
+
|
|
29
|
+
MGLRandomHostObject::MGLRandomHostObject(
|
|
30
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
31
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
|
|
32
|
+
: MGLSmartHostObject(jsCallInvoker, workerQueue) {
|
|
33
|
+
this->fields.push_back(buildPair(
|
|
34
|
+
"randomFill", JSIF([=]) {
|
|
35
|
+
if (count != 3) {
|
|
36
|
+
throw jsi::JSError(runtime,
|
|
37
|
+
"randomFill(..) expects exactly 4 arguments!");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
|
|
41
|
+
auto resultSize = result.size(runtime);
|
|
42
|
+
auto *resultData = result.data(runtime);
|
|
43
|
+
auto resultPreventGC =
|
|
44
|
+
std::make_shared<jsi::ArrayBuffer>(std::move(result));
|
|
45
|
+
|
|
46
|
+
auto offset = (int)arguments[1].asNumber();
|
|
47
|
+
auto size = arguments[2].asNumber();
|
|
48
|
+
|
|
49
|
+
return react::createPromiseAsJSIValue(
|
|
50
|
+
runtime, [=](jsi::Runtime &runtime,
|
|
51
|
+
std::shared_ptr<react::Promise> promise) {
|
|
52
|
+
// TODO(Szymon) implement check prime once we have bignums
|
|
53
|
+
this->runOnWorkerThread([=]() {
|
|
54
|
+
if (RAND_bytes(resultData + offset, size) != 1) {
|
|
55
|
+
this->runOnJSThread([=]() {
|
|
56
|
+
promise->reject("Sth went wrong with RAND_bytes");
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
this->runOnJSThread([=]() {
|
|
60
|
+
promise->resolve(
|
|
61
|
+
jsi::ArrayBuffer(std::move(*resultPreventGC)));
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
this->fields.push_back(buildPair(
|
|
68
|
+
"randomFillSync", JSIF([=]) {
|
|
69
|
+
if (count != 3) {
|
|
70
|
+
throw jsi::JSError(runtime,
|
|
71
|
+
"randomFillSync(..) expects exactly 4 arguments!");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
|
|
75
|
+
auto resultSize = result.size(runtime);
|
|
76
|
+
auto *resultData = result.data(runtime);
|
|
77
|
+
auto offset = (int)arguments[1].asNumber();
|
|
78
|
+
auto size = arguments[2].asNumber();
|
|
79
|
+
|
|
80
|
+
if (RAND_bytes(resultData + offset, size) != 1) {
|
|
81
|
+
throw jsi::JSError(runtime, "Sth went wrong with RAND_bytes" +
|
|
82
|
+
std::to_string(ERR_get_error()));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return result;
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
} // namespace margelo
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Szymon on 25/02/2022.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#ifndef MGL_RANDOMHOSTOBJECT_H
|
|
6
|
+
#define MGL_RANDOMHOSTOBJECT_H
|
|
7
|
+
|
|
8
|
+
#include <memory>
|
|
9
|
+
|
|
10
|
+
#ifdef ANDROID
|
|
11
|
+
#include "JSIUtils/MGLSmartHostObject.h"
|
|
12
|
+
#else
|
|
13
|
+
#include "MGLSmartHostObject.h"
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
namespace margelo {
|
|
17
|
+
namespace jsi = facebook::jsi;
|
|
18
|
+
|
|
19
|
+
class MGLRandomHostObject : public MGLSmartHostObject {
|
|
20
|
+
public:
|
|
21
|
+
MGLRandomHostObject(
|
|
22
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
23
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
} // namespace margelo
|
|
27
|
+
#endif // MGL_RANDOMHOSTOBJECT_H
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Szymon on 23/02/2022.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include "MGLDispatchQueue.h"
|
|
6
|
+
|
|
7
|
+
#include <utility>
|
|
8
|
+
|
|
9
|
+
namespace margelo {
|
|
10
|
+
|
|
11
|
+
namespace DispatchQueue {
|
|
12
|
+
|
|
13
|
+
dispatch_queue::dispatch_queue(std::string name, size_t thread_cnt)
|
|
14
|
+
: name_{std::move(name)}, threads_(thread_cnt) {
|
|
15
|
+
printf("Creating dispatch queue: %s\n", name_.c_str());
|
|
16
|
+
printf("Dispatch threads: %zu\n", thread_cnt);
|
|
17
|
+
|
|
18
|
+
for (size_t i = 0; i < threads_.size(); i++) {
|
|
19
|
+
threads_[i] = std::thread(&dispatch_queue::dispatch_thread_handler, this);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
dispatch_queue::~dispatch_queue() {
|
|
24
|
+
printf("Destructor: Destroying dispatch threads...\n");
|
|
25
|
+
|
|
26
|
+
// Signal to dispatch threads that it's time to wrap up
|
|
27
|
+
std::unique_lock<std::mutex> lock(lock_);
|
|
28
|
+
quit_ = true;
|
|
29
|
+
cv_.notify_all();
|
|
30
|
+
lock.unlock();
|
|
31
|
+
|
|
32
|
+
// Wait for threads to finish before we exit
|
|
33
|
+
for (size_t i = 0; i < threads_.size(); i++) {
|
|
34
|
+
if (threads_[i].joinable()) {
|
|
35
|
+
printf("Destructor: Joining thread %zu until completion\n", i);
|
|
36
|
+
threads_[i].join();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void dispatch_queue::dispatch(const fp_t &op) {
|
|
42
|
+
std::unique_lock<std::mutex> lock(lock_);
|
|
43
|
+
q_.push(op);
|
|
44
|
+
cv_.notify_one();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void dispatch_queue::dispatch(fp_t &&op) {
|
|
48
|
+
std::unique_lock<std::mutex> lock(lock_);
|
|
49
|
+
q_.push(std::move(op));
|
|
50
|
+
cv_.notify_one();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void dispatch_queue::dispatch_thread_handler(void) {
|
|
54
|
+
std::unique_lock<std::mutex> lock(lock_);
|
|
55
|
+
|
|
56
|
+
do {
|
|
57
|
+
// Wait until we have data or a quit signal
|
|
58
|
+
cv_.wait(lock, [this] { return (q_.size() || quit_); });
|
|
59
|
+
|
|
60
|
+
// after wait, we own the lock
|
|
61
|
+
if (!quit_ && q_.size()) {
|
|
62
|
+
auto op = std::move(q_.front());
|
|
63
|
+
q_.pop();
|
|
64
|
+
|
|
65
|
+
// unlock now that we're done messing with the queue
|
|
66
|
+
lock.unlock();
|
|
67
|
+
|
|
68
|
+
op();
|
|
69
|
+
|
|
70
|
+
lock.lock();
|
|
71
|
+
}
|
|
72
|
+
} while (!quit_);
|
|
73
|
+
}
|
|
74
|
+
} // namespace DispatchQueue
|
|
75
|
+
} // namespace margelo
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Szymon on 23/02/2022.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#ifndef MGL_DISPATCHQUEUE_H
|
|
6
|
+
#define MGL_DISPATCHQUEUE_H
|
|
7
|
+
|
|
8
|
+
#include <condition_variable>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <cstdio>
|
|
11
|
+
#include <functional>
|
|
12
|
+
#include <mutex>
|
|
13
|
+
#include <queue>
|
|
14
|
+
#include <string>
|
|
15
|
+
#include <thread>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
namespace margelo {
|
|
19
|
+
|
|
20
|
+
// taken from
|
|
21
|
+
// https://github.com/embeddedartistry/embedded-resources/blob/master/examples/cpp/dispatch.cpp
|
|
22
|
+
namespace DispatchQueue {
|
|
23
|
+
class dispatch_queue {
|
|
24
|
+
typedef std::function<void(void)> fp_t;
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
explicit dispatch_queue(std::string name, size_t thread_cnt = 1);
|
|
28
|
+
~dispatch_queue();
|
|
29
|
+
|
|
30
|
+
// dispatch and copy
|
|
31
|
+
void dispatch(const fp_t &op);
|
|
32
|
+
// dispatch and move
|
|
33
|
+
void dispatch(fp_t &&op);
|
|
34
|
+
|
|
35
|
+
// Deleted operations
|
|
36
|
+
dispatch_queue(const dispatch_queue &rhs) = delete;
|
|
37
|
+
dispatch_queue &operator=(const dispatch_queue &rhs) = delete;
|
|
38
|
+
dispatch_queue(dispatch_queue &&rhs) = delete;
|
|
39
|
+
dispatch_queue &operator=(dispatch_queue &&rhs) = delete;
|
|
40
|
+
|
|
41
|
+
private:
|
|
42
|
+
std::string name_;
|
|
43
|
+
std::mutex lock_;
|
|
44
|
+
std::vector<std::thread> threads_;
|
|
45
|
+
std::queue<fp_t> q_;
|
|
46
|
+
std::condition_variable cv_;
|
|
47
|
+
bool quit_ = false;
|
|
48
|
+
|
|
49
|
+
void dispatch_thread_handler(void);
|
|
50
|
+
};
|
|
51
|
+
} // namespace DispatchQueue
|
|
52
|
+
|
|
53
|
+
} // namespace margelo
|
|
54
|
+
|
|
55
|
+
#endif // MGL_DISPATCHQUEUE_H
|
package/cpp/Utils/logs.h
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#ifdef ANDROID
|
|
2
|
+
// LOGS ANDROID
|
|
3
|
+
#include <android/log.h>
|
|
4
|
+
#define LOG_TAG "react-native-quick-crypto"
|
|
5
|
+
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
|
|
6
|
+
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
|
|
7
|
+
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
|
|
8
|
+
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
|
|
9
|
+
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
|
10
|
+
#define LOGSIMPLE(...)
|
|
11
|
+
#else
|
|
12
|
+
// LOGS NO ANDROID
|
|
13
|
+
#include <stdio.h>
|
|
14
|
+
#define LOG_TAG "react-native-quick-crypto"
|
|
15
|
+
#define LOGV(...) \
|
|
16
|
+
printf(" "); \
|
|
17
|
+
printf(__VA_ARGS__); \
|
|
18
|
+
printf("\t - <%s> \n", LOG_TAG);
|
|
19
|
+
#define LOGD(...) \
|
|
20
|
+
printf(" "); \
|
|
21
|
+
printf(__VA_ARGS__); \
|
|
22
|
+
printf("\t - <%s> \n", LOG_TAG);
|
|
23
|
+
#define LOGI(...) \
|
|
24
|
+
printf(" "); \
|
|
25
|
+
printf(__VA_ARGS__); \
|
|
26
|
+
printf("\t - <%s> \n", LOG_TAG);
|
|
27
|
+
#define LOGW(...) \
|
|
28
|
+
printf(" * Warning: "); \
|
|
29
|
+
printf(__VA_ARGS__); \
|
|
30
|
+
printf("\t - <%s> \n", LOG_TAG);
|
|
31
|
+
#define LOGE(...) \
|
|
32
|
+
printf(" *** Error: "); \
|
|
33
|
+
printf(__VA_ARGS__); \
|
|
34
|
+
printf("\t - <%s> \n", LOG_TAG);
|
|
35
|
+
#define LOGSIMPLE(...) \
|
|
36
|
+
printf(" "); \
|
|
37
|
+
printf(__VA_ARGS__);
|
|
38
|
+
#endif // ANDROID
|