react-native-quick-crypto 1.0.0-beta.3 → 1.0.0-beta.5

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 (121) hide show
  1. package/QuickCrypto.podspec +4 -0
  2. package/README.md +2 -0
  3. package/android/CMakeLists.txt +26 -13
  4. package/android/build.gradle +4 -0
  5. package/android/src/main/cpp/cpp-adapter.cpp +3 -10
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +15 -10
  7. package/cpp/pbkdf2/HybridPbkdf2.cpp +71 -0
  8. package/cpp/pbkdf2/HybridPbkdf2.hpp +35 -0
  9. package/cpp/random/HybridRandom.cpp +42 -18
  10. package/cpp/random/HybridRandom.hpp +13 -28
  11. package/cpp/utils/Utils.hpp +12 -1
  12. package/deps/fastpbkdf2/fastpbkdf2.c +352 -0
  13. package/deps/fastpbkdf2/fastpbkdf2.h +68 -0
  14. package/lib/commonjs/index.js +24 -5
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/keys/index.js +86 -0
  17. package/lib/commonjs/keys/index.js.map +1 -0
  18. package/lib/commonjs/pbkdf2.js +89 -0
  19. package/lib/commonjs/pbkdf2.js.map +1 -0
  20. package/lib/commonjs/random.js +3 -3
  21. package/lib/commonjs/random.js.map +1 -1
  22. package/lib/commonjs/specs/keyObjectHandle.nitro.js +6 -0
  23. package/lib/commonjs/specs/keyObjectHandle.nitro.js.map +1 -0
  24. package/lib/commonjs/specs/pbkdf2.nitro.js +6 -0
  25. package/lib/commonjs/specs/pbkdf2.nitro.js.map +1 -0
  26. package/lib/commonjs/utils/conversion.js +74 -0
  27. package/lib/commonjs/utils/conversion.js.map +1 -1
  28. package/lib/commonjs/utils/errors.js +14 -0
  29. package/lib/commonjs/utils/errors.js.map +1 -0
  30. package/lib/commonjs/utils/hashnames.js +90 -0
  31. package/lib/commonjs/utils/hashnames.js.map +1 -0
  32. package/lib/commonjs/utils/index.js +43 -5
  33. package/lib/commonjs/utils/index.js.map +1 -1
  34. package/lib/commonjs/utils/types.js +27 -0
  35. package/lib/commonjs/utils/types.js.map +1 -1
  36. package/lib/module/index.js +9 -4
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/module/keys/index.js +81 -0
  39. package/lib/module/keys/index.js.map +1 -0
  40. package/lib/module/pbkdf2.js +83 -0
  41. package/lib/module/pbkdf2.js.map +1 -0
  42. package/lib/module/random.js +1 -1
  43. package/lib/module/random.js.map +1 -1
  44. package/lib/module/specs/keyObjectHandle.nitro.js +4 -0
  45. package/lib/module/specs/keyObjectHandle.nitro.js.map +1 -0
  46. package/lib/module/specs/pbkdf2.nitro.js +4 -0
  47. package/lib/module/specs/pbkdf2.nitro.js.map +1 -0
  48. package/lib/module/utils/conversion.js +71 -0
  49. package/lib/module/utils/conversion.js.map +1 -1
  50. package/lib/module/utils/errors.js +10 -0
  51. package/lib/module/utils/errors.js.map +1 -0
  52. package/lib/module/utils/hashnames.js +88 -0
  53. package/lib/module/utils/hashnames.js.map +1 -0
  54. package/lib/module/utils/index.js +4 -5
  55. package/lib/module/utils/index.js.map +1 -1
  56. package/lib/module/utils/types.js +26 -0
  57. package/lib/module/utils/types.js.map +1 -1
  58. package/lib/tsconfig.tsbuildinfo +1 -1
  59. package/lib/typescript/index.d.ts +26 -9
  60. package/lib/typescript/index.d.ts.map +1 -1
  61. package/lib/typescript/keys/index.d.ts +22 -0
  62. package/lib/typescript/keys/index.d.ts.map +1 -0
  63. package/lib/typescript/pbkdf2.d.ts +12 -0
  64. package/lib/typescript/pbkdf2.d.ts.map +1 -0
  65. package/lib/typescript/random.d.ts +1 -1
  66. package/lib/typescript/random.d.ts.map +1 -1
  67. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +15 -0
  68. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -0
  69. package/lib/typescript/specs/pbkdf2.nitro.d.ts +9 -0
  70. package/lib/typescript/specs/pbkdf2.nitro.d.ts.map +1 -0
  71. package/lib/typescript/utils/conversion.d.ts +7 -1
  72. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  73. package/lib/typescript/utils/errors.d.ts +7 -0
  74. package/lib/typescript/utils/errors.d.ts.map +1 -0
  75. package/lib/typescript/utils/hashnames.d.ts +11 -0
  76. package/lib/typescript/utils/hashnames.d.ts.map +1 -0
  77. package/lib/typescript/utils/index.d.ts +4 -5
  78. package/lib/typescript/utils/index.d.ts.map +1 -1
  79. package/lib/typescript/utils/types.d.ts +101 -0
  80. package/lib/typescript/utils/types.d.ts.map +1 -1
  81. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +19 -3
  82. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +1 -2
  83. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +50 -0
  84. package/nitrogen/generated/android/QuickCryptoOnLoad.hpp +25 -0
  85. package/nitrogen/generated/android/QuickCryptoOnLoad.kt +1 -0
  86. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +7 -6
  87. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +10 -2
  88. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +4 -2
  89. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +12 -6
  90. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +45 -0
  91. package/nitrogen/generated/ios/QuickCryptoAutolinking.swift +12 -0
  92. package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +86 -0
  93. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +27 -0
  94. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +93 -0
  95. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.cpp +22 -0
  96. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +66 -0
  97. package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +1 -2
  98. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +7 -4
  99. package/nitrogen/generated/shared/c++/JWK.hpp +162 -0
  100. package/nitrogen/generated/shared/c++/JWKkty.hpp +86 -0
  101. package/nitrogen/generated/shared/c++/JWKuse.hpp +78 -0
  102. package/nitrogen/generated/shared/c++/KFormatType.hpp +65 -0
  103. package/nitrogen/generated/shared/c++/KeyDetail.hpp +93 -0
  104. package/nitrogen/generated/shared/c++/KeyEncoding.hpp +66 -0
  105. package/nitrogen/generated/shared/c++/KeyType.hpp +65 -0
  106. package/nitrogen/generated/shared/c++/KeyUsage.hpp +102 -0
  107. package/nitrogen/generated/shared/c++/NamedCurve.hpp +82 -0
  108. package/package.json +13 -12
  109. package/src/index.ts +8 -4
  110. package/src/keys/index.ts +99 -0
  111. package/src/pbkdf2.ts +154 -0
  112. package/src/random.ts +14 -14
  113. package/src/specs/keyObjectHandle.nitro.ts +32 -0
  114. package/src/specs/pbkdf2.nitro.ts +18 -0
  115. package/src/specs/random.nitro.ts +2 -2
  116. package/src/utils/conversion.ts +83 -1
  117. package/src/utils/errors.ts +15 -0
  118. package/src/utils/hashnames.ts +96 -0
  119. package/src/utils/index.ts +4 -6
  120. package/src/utils/types.ts +178 -2
  121. package/ios/QuickCryptoOnLoad.mm +0 -19
@@ -22,6 +22,10 @@ Pod::Spec.new do |s|
22
22
  "ios/**/*.{h,m,mm}",
23
23
  # implementation (C++)
24
24
  "cpp/**/*.{hpp,cpp}",
25
+ # dependencies (C++)
26
+ "deps/**/*.{hpp,cpp}",
27
+ # dependencies (C)
28
+ "deps/**/*.{h,c}",
25
29
  ]
26
30
 
27
31
  # Add all files generated by Nitrogen
package/README.md CHANGED
@@ -8,6 +8,8 @@ A fast implementation of Node's `crypto` module.
8
8
 
9
9
  > Note: This version `1.x` is undergoing a major refactor, porting to New Architecture, Bridgeless, and [`Nitro Modules`](https://github.com/mrousavy/react-native-nitro) and is incomplete compared to the `0.x` version. Status, as always, will be represented in [implementation-coverage.md](../main/docs/implementation-coverage.md).
10
10
 
11
+ > Note: Minimum supported version of React Native is `0.75`. If you need to use earlier versions, please use `0.x` versions of this library.
12
+
11
13
  ## Features
12
14
 
13
15
  Unlike any other current JS-based polyfills, react-native-quick-crypto is written in C/C++ JSI and provides much greater performance - especially on mobile devices.
@@ -1,8 +1,7 @@
1
+ project(QuickCrypto)
1
2
  cmake_minimum_required(VERSION 3.9.0)
2
3
 
3
- project(QuickCrypto)
4
4
  set(PACKAGE_NAME QuickCrypto)
5
-
6
5
  set(CMAKE_VERBOSE_MAKEFILE ON)
7
6
  set(CMAKE_CXX_STANDARD 20)
8
7
 
@@ -10,12 +9,23 @@ set(CMAKE_CXX_STANDARD 20)
10
9
  add_library(
11
10
  ${PACKAGE_NAME} SHARED
12
11
  src/main/cpp/cpp-adapter.cpp
12
+ ../cpp/pbkdf2/HybridPbkdf2.cpp
13
13
  ../cpp/random/HybridRandom.cpp
14
+ ../deps/fastpbkdf2/fastpbkdf2.c
14
15
  )
15
16
 
16
- include_directories(../cpp)
17
+ # add Nitrogen specs
17
18
  include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/QuickCrypto+autolinking.cmake)
18
19
 
20
+ # local includes
21
+ include_directories(
22
+ "src/main/cpp"
23
+ "../cpp/pbkdf2"
24
+ "../cpp/random"
25
+ "../cpp/utils"
26
+ "../deps/fastpbkdf2"
27
+ )
28
+
19
29
  # Third party libraries (Prefabs)
20
30
  find_library(LOG_LIB log)
21
31
 
@@ -31,14 +41,17 @@ target_link_libraries(
31
41
  fbjni::fbjni # <-- Facebook C++ JNI helpers
32
42
  openssl::crypto # <-- OpenSSL (Crypto)
33
43
  ReactAndroid::jsi
34
- ReactAndroid::turbomodulejsijni
35
- ReactAndroid::react_nativemodule_core
36
- ReactAndroid::react_render_core
37
- ReactAndroid::runtimeexecutor
38
- ReactAndroid::fabricjni
39
- ReactAndroid::react_debug
40
- ReactAndroid::react_render_core
41
- ReactAndroid::react_render_componentregistry
42
- ReactAndroid::rrc_view
43
- ReactAndroid::folly_runtime
44
44
  )
45
+
46
+ if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
47
+ target_link_libraries(
48
+ ${PACKAGE_NAME}
49
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
50
+ )
51
+ else()
52
+ target_link_libraries(
53
+ ${PACKAGE_NAME}
54
+ ReactAndroid::turbomodulejsijni
55
+ ReactAndroid::react_nativemodule_core # <-- RN: React Native native module core
56
+ )
57
+ endif()
@@ -104,7 +104,11 @@ dependencies {
104
104
  //noinspection GradleDynamicVersion
105
105
  implementation "com.facebook.react:react-native:+"
106
106
 
107
+ // Add a dependency on NitroModules
107
108
  implementation project(":react-native-nitro-modules")
109
+
110
+ // Add a dependency on OpenSSL
111
+ // TODO: update to v3 (see #351)
108
112
  implementation "com.android.ndk.thirdparty:openssl:1.1.1q-beta-1"
109
113
  }
110
114
 
@@ -1,15 +1,8 @@
1
+ #include <fbjni/fbjni.h>
1
2
  #include <jni.h>
2
3
 
3
- #include "random/HybridRandom.hpp"
4
- #include <NitroModules/HybridObjectRegistry.hpp>
5
-
6
- using namespace margelo::nitro::crypto;
7
- using namespace margelo::crypto;
4
+ #include "QuickCryptoOnLoad.hpp"
8
5
 
9
6
  JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
10
- HybridObjectRegistry::registerHybridObjectConstructor(
11
- "Random", []() -> std::shared_ptr<HybridObject> { return std::make_shared<HybridRandom>();
12
- });
13
-
14
- return JNI_VERSION_1_2;
7
+ return facebook::jni::initialize(vm, [=] { margelo::nitro::crypto::initialize(vm); });
15
8
  }
@@ -1,27 +1,21 @@
1
1
  package com.margelo.nitro.quickcrypto;
2
2
 
3
3
  import android.util.Log;
4
+
4
5
  import androidx.annotation.Nullable;
5
6
 
6
7
  import com.facebook.react.bridge.NativeModule;
7
8
  import com.facebook.react.bridge.ReactApplicationContext;
8
9
  import com.facebook.react.module.model.ReactModuleInfoProvider;
9
10
  import com.facebook.react.TurboReactPackage;
11
+ import com.margelo.nitro.core.HybridObject;
12
+ import com.margelo.nitro.core.HybridObjectRegistry;
10
13
 
11
14
  import java.util.HashMap;
15
+ import java.util.function.Supplier;
12
16
 
13
17
  public class QuickCryptoPackage extends TurboReactPackage {
14
18
  private static final String TAG = "QuickCrypto";
15
- static {
16
- try {
17
- Log.i(TAG, "Loading C++ library...");
18
- System.loadLibrary(TAG);
19
- Log.i(TAG, "Successfully loaded C++ library!");
20
- } catch (Throwable e) {
21
- Log.e(TAG, "Failed to load C++ library! Is it properly installed and linked?", e);
22
- throw e;
23
- }
24
- }
25
19
 
26
20
  @Nullable
27
21
  @Override
@@ -35,4 +29,15 @@ public class QuickCryptoPackage extends TurboReactPackage {
35
29
  return new HashMap<>();
36
30
  };
37
31
  }
32
+
33
+ static {
34
+ try {
35
+ Log.i(TAG, "Loading C++ library...");
36
+ System.loadLibrary(TAG);
37
+ Log.i(TAG, "Successfully loaded C++ library!");
38
+ } catch (Throwable e) {
39
+ Log.e(TAG, "Failed to load C++ library! Is it properly installed and linked?", e);
40
+ throw e;
41
+ }
42
+ }
38
43
  }
@@ -0,0 +1,71 @@
1
+ #include "HybridPbkdf2.hpp"
2
+ #include "Utils.hpp"
3
+
4
+ namespace margelo::nitro::crypto {
5
+
6
+ std::future<std::shared_ptr<ArrayBuffer>>
7
+ HybridPbkdf2::pbkdf2(
8
+ const std::shared_ptr<ArrayBuffer>& password,
9
+ const std::shared_ptr<ArrayBuffer>& salt,
10
+ double iterations,
11
+ double keylen,
12
+ const std::string& digest
13
+ ) {
14
+ // get owned NativeArrayBuffers before passing to sync function
15
+ auto nativePassword = ToNativeArrayBuffer(password);
16
+ auto nativeSalt = ToNativeArrayBuffer(salt);
17
+
18
+ return std::async(std::launch::async,
19
+ [this, nativePassword, nativeSalt, iterations, keylen, digest]() {
20
+ return this->pbkdf2Sync(nativePassword, nativeSalt, iterations, keylen, digest);
21
+ });
22
+ }
23
+
24
+ std::shared_ptr<ArrayBuffer>
25
+ HybridPbkdf2::pbkdf2Sync(
26
+ const std::shared_ptr<ArrayBuffer>& password,
27
+ const std::shared_ptr<ArrayBuffer>& salt,
28
+ double iterations,
29
+ double keylen,
30
+ const std::string& digest
31
+ ) {
32
+ size_t bufferSize = static_cast<size_t>(keylen);
33
+ uint8_t* data = new uint8_t[bufferSize];
34
+ auto result = std::make_shared<NativeArrayBuffer>(data, bufferSize, [=]() { delete[] data; });
35
+
36
+ // use fastpbkdf2 when possible
37
+ if (digest == "sha1") {
38
+ fastpbkdf2_hmac_sha1(password.get()->data(), password.get()->size(),
39
+ salt.get()->data(), salt.get()->size(),
40
+ static_cast<uint32_t>(iterations),
41
+ result.get()->data(), result.get()->size());
42
+ } else if (digest == "sha256") {
43
+ fastpbkdf2_hmac_sha256(password.get()->data(), password.get()->size(),
44
+ salt.get()->data(), salt.get()->size(),
45
+ static_cast<uint32_t>(iterations),
46
+ result.get()->data(), result.get()->size());
47
+ } else if (digest == "sha512") {
48
+ fastpbkdf2_hmac_sha512(password.get()->data(), password.get()->size(),
49
+ salt.get()->data(), salt.get()->size(),
50
+ static_cast<uint32_t>(iterations),
51
+ result.get()->data(), result.get()->size());
52
+ } else {
53
+ // fallback to OpenSSL
54
+ auto *digestByName = EVP_get_digestbyname(digest.c_str());
55
+ if (digestByName == nullptr) {
56
+ throw std::runtime_error("Invalid hash-algorithm: " + digest);
57
+ }
58
+ char *passAsCharA = reinterpret_cast<char *>(password.get()->data());
59
+ const unsigned char *saltAsCharA =
60
+ reinterpret_cast<const unsigned char *>(salt.get()->data());
61
+ unsigned char *resultAsCharA =
62
+ reinterpret_cast<unsigned char *>(result.get()->data());
63
+ PKCS5_PBKDF2_HMAC(passAsCharA, password.get()->size(), saltAsCharA,
64
+ salt.get()->size(), static_cast<uint32_t>(iterations),
65
+ digestByName, result.get()->size(), resultAsCharA);
66
+ }
67
+
68
+ return result;
69
+ }
70
+
71
+ } // namespace margelo::nitro::crypto
@@ -0,0 +1,35 @@
1
+ #include <openssl/evp.h>
2
+
3
+ #include "HybridPbkdf2Spec.hpp"
4
+ #include "fastpbkdf2.h"
5
+
6
+ namespace margelo::nitro::crypto {
7
+
8
+ using namespace facebook;
9
+
10
+ class HybridPbkdf2 : public HybridPbkdf2Spec {
11
+ public:
12
+ HybridPbkdf2() : HybridObject(TAG) {}
13
+
14
+ public:
15
+ // Methods
16
+ virtual std::future<std::shared_ptr<ArrayBuffer>>
17
+ pbkdf2(
18
+ const std::shared_ptr<ArrayBuffer>& password,
19
+ const std::shared_ptr<ArrayBuffer>& salt,
20
+ double iterations,
21
+ double keylen,
22
+ const std::string& digest
23
+ ) override;
24
+
25
+ virtual std::shared_ptr<ArrayBuffer>
26
+ pbkdf2Sync(
27
+ const std::shared_ptr<ArrayBuffer>& password,
28
+ const std::shared_ptr<ArrayBuffer>& salt,
29
+ double iterations,
30
+ double keylen,
31
+ const std::string& digest
32
+ ) override;
33
+ };
34
+
35
+ } // namespace margelo::nitro::crypto
@@ -1,34 +1,58 @@
1
- #include "HybridRandom.hpp"
2
-
3
1
  #include <openssl/err.h>
4
2
  #include <openssl/rand.h>
5
3
 
6
- namespace margelo::crypto {
4
+ #include "HybridRandom.hpp"
5
+ #include "Utils.hpp"
7
6
 
8
- using namespace margelo::nitro;
9
- using namespace margelo::nitro::crypto;
10
7
 
11
- std::future<std::shared_ptr<ArrayBuffer>> HybridRandom::randomFill(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset,
12
- double dSize) {
13
- size_t size = checkSize(dSize);
14
- // copy the JSArrayBuffer that we do not own into a NativeArrayBuffer that we do own, before passing to sync function
15
- uint8_t* data = new uint8_t[size];
16
- memcpy(data, buffer.get()->data(), size);
17
- std::shared_ptr<ArrayBuffer> nativeBuffer = std::make_shared<NativeArrayBuffer>(data, size, false);
8
+ size_t checkSize(double size) {
9
+ if (!CheckIsUint32(size)) {
10
+ throw std::runtime_error("size must be uint32");
11
+ }
12
+ if (static_cast<uint32_t>(size) > pow(2, 31) - 1) {
13
+ throw std::runtime_error("size must be less than 2^31 - 1");
14
+ }
15
+ return static_cast<size_t>(size);
16
+ }
17
+
18
+ size_t checkOffset(double size, double offset) {
19
+ if (!CheckIsUint32(offset)) {
20
+ throw std::runtime_error("offset must be uint32");
21
+ }
22
+ if (offset > size) {
23
+ throw std::runtime_error("offset must be less than size");
24
+ }
25
+ return static_cast<size_t>(offset);
26
+ }
27
+
28
+
29
+ namespace margelo::nitro::crypto {
30
+
31
+ std::future<std::shared_ptr<ArrayBuffer>>
32
+ HybridRandom::randomFill(const std::shared_ptr<ArrayBuffer>& buffer,
33
+ double dOffset,
34
+ double dSize) {
35
+ // get owned NativeArrayBuffer before passing to sync function
36
+ auto nativeBuffer = ToNativeArrayBuffer(buffer);
18
37
 
19
38
  return std::async(std::launch::async,
20
- [this, nativeBuffer, dOffset, dSize]() { return this->randomFillSync(nativeBuffer, dOffset, dSize); });
39
+ [this, nativeBuffer, dOffset, dSize]() {
40
+ return this->randomFillSync(nativeBuffer, dOffset, dSize);
41
+ });
21
42
  };
22
43
 
23
- std::shared_ptr<ArrayBuffer> HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) {
44
+ std::shared_ptr<ArrayBuffer>
45
+ HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer,
46
+ double dOffset,
47
+ double dSize) {
24
48
  size_t size = checkSize(dSize);
25
49
  size_t offset = checkOffset(dSize, dOffset);
26
50
  uint8_t* data = buffer.get()->data();
27
-
28
51
  if (RAND_bytes(data + offset, (int)size) != 1) {
29
- throw std::runtime_error("error calling RAND_bytes" + std::to_string(ERR_get_error()));
52
+ throw std::runtime_error("error calling RAND_bytes" +
53
+ std::to_string(ERR_get_error()));
30
54
  }
31
- return std::make_shared<NativeArrayBuffer>(data, size, false);
55
+ return buffer;
32
56
  };
33
57
 
34
- } // namespace margelo::crypto
58
+ } // namespace margelo::nitro::crypto
@@ -1,44 +1,29 @@
1
- #include <NitroModules/ArrayBuffer.hpp>
2
1
  #include <cmath>
3
2
  #include <future>
3
+ #include <iostream>
4
4
 
5
5
  #include "HybridRandomSpec.hpp"
6
6
 
7
- #ifdef ANDROID
8
- #include "utils/Utils.hpp"
9
- #else
10
- #include "Utils.hpp"
11
- #endif
7
+ namespace margelo::nitro::crypto {
12
8
 
13
- namespace margelo::crypto {
14
-
15
- using namespace margelo::nitro;
16
- using namespace margelo::nitro::crypto;
9
+ using namespace facebook;
17
10
 
18
11
  class HybridRandom : public HybridRandomSpec {
19
12
  public:
13
+ HybridRandom() : HybridObject(TAG) {}
14
+
15
+ public:
16
+ // Methods
20
17
  std::future<std::shared_ptr<ArrayBuffer>> randomFill(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) override;
21
18
  std::shared_ptr<ArrayBuffer> randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) override;
22
19
  };
23
20
 
24
- inline size_t checkSize(double size) {
25
- if (!CheckIsUint32(size)) {
26
- throw std::runtime_error("size must be uint32");
27
- }
28
- if (static_cast<uint32_t>(size) > pow(2, 31) - 1) {
29
- throw std::runtime_error("size must be less than 2^31 - 1");
30
- }
31
- return static_cast<size_t>(size);
32
- }
33
-
34
- inline size_t checkOffset(double size, double offset) {
35
- if (!CheckIsUint32(offset)) {
36
- throw std::runtime_error("offset must be uint32");
37
- }
38
- if (offset > size) {
39
- throw std::runtime_error("offset must be less than size");
21
+ inline void printData(std::string name, uint8_t* data, size_t size) {
22
+ std::cout << "data - " << name << std::endl;
23
+ for (size_t i = 0; i < size; i++) {
24
+ printf("%u ", data[i]);
40
25
  }
41
- return static_cast<size_t>(offset);
26
+ printf("\n");
42
27
  }
43
28
 
44
- } // namespace margelo::crypto
29
+ } // namespace margelo::nitro::crypto
@@ -1,9 +1,20 @@
1
1
  #include <limits>
2
2
 
3
+ #include <NitroModules/ArrayBuffer.hpp>
4
+
5
+ // copy a JSArrayBuffer that we do not own into a NativeArrayBuffer that we do own
6
+ inline std::shared_ptr<margelo::nitro::NativeArrayBuffer>
7
+ ToNativeArrayBuffer(const std::shared_ptr<margelo::nitro::ArrayBuffer>& buffer) {
8
+ size_t bufferSize = buffer.get()->size();
9
+ uint8_t* data = new uint8_t[bufferSize];
10
+ memcpy(data, buffer.get()->data(), bufferSize);
11
+ return std::make_shared<margelo::nitro::NativeArrayBuffer>(data, bufferSize, [=]() { delete[] data; });
12
+ }
13
+
3
14
  inline bool CheckIsUint32(double value) {
4
15
  return (value >= std::numeric_limits<uint32_t>::lowest() && value <= std::numeric_limits<uint32_t>::max());
5
16
  }
6
17
 
7
18
  inline bool CheckIsInt32(double value) {
8
19
  return (value >= std::numeric_limits<int32_t>::lowest() && value <= std::numeric_limits<int32_t>::max());
9
- }
20
+ }