react-native-quick-crypto 0.7.9 → 0.7.11

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 (63) hide show
  1. package/android/CMakeLists.txt +59 -62
  2. package/android/build.gradle +10 -9
  3. package/android/src/main/cpp/cpp-adapter.cpp +9 -0
  4. package/cpp/JSIUtils/MGLTypedArray.cpp +38 -17
  5. package/cpp/JSIUtils/MGLTypedArray.h +20 -1
  6. package/cpp/MGLQuickCryptoHostObject.h +1 -1
  7. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  8. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  9. package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  10. package/ios/QuickCryptoModule.mm +10 -0
  11. package/lib/commonjs/Algorithms.js +224 -0
  12. package/lib/commonjs/Algorithms.js.map +1 -0
  13. package/lib/commonjs/Hash.js +1 -1
  14. package/lib/commonjs/Hash.js.map +1 -1
  15. package/lib/commonjs/Hashnames.js +1 -1
  16. package/lib/commonjs/Hashnames.js.map +1 -1
  17. package/lib/commonjs/Utils.js +27 -235
  18. package/lib/commonjs/Utils.js.map +1 -1
  19. package/lib/commonjs/keys.js.map +1 -1
  20. package/lib/commonjs/pbkdf2.js +1 -1
  21. package/lib/commonjs/pbkdf2.js.map +1 -1
  22. package/lib/commonjs/random.js.map +1 -1
  23. package/lib/commonjs/subtle.js +7 -6
  24. package/lib/commonjs/subtle.js.map +1 -1
  25. package/lib/module/Algorithms.js +219 -0
  26. package/lib/module/Algorithms.js.map +1 -0
  27. package/lib/module/Hash.js +1 -1
  28. package/lib/module/Hash.js.map +1 -1
  29. package/lib/module/Hashnames.js +1 -1
  30. package/lib/module/Hashnames.js.map +1 -1
  31. package/lib/module/Utils.js +25 -230
  32. package/lib/module/Utils.js.map +1 -1
  33. package/lib/module/keys.js.map +1 -1
  34. package/lib/module/pbkdf2.js +1 -1
  35. package/lib/module/pbkdf2.js.map +1 -1
  36. package/lib/module/random.js.map +1 -1
  37. package/lib/module/subtle.js +2 -1
  38. package/lib/module/subtle.js.map +1 -1
  39. package/lib/typescript/src/Algorithms.d.ts +4 -0
  40. package/lib/typescript/src/Algorithms.d.ts.map +1 -0
  41. package/lib/typescript/src/Hashnames.d.ts +2 -2
  42. package/lib/typescript/src/Hashnames.d.ts.map +1 -1
  43. package/lib/typescript/src/Utils.d.ts +20 -8
  44. package/lib/typescript/src/Utils.d.ts.map +1 -1
  45. package/lib/typescript/src/index.d.ts +4 -4
  46. package/lib/typescript/src/index.d.ts.map +1 -1
  47. package/lib/typescript/src/keys.d.ts +5 -2
  48. package/lib/typescript/src/keys.d.ts.map +1 -1
  49. package/lib/typescript/src/random.d.ts +5 -6
  50. package/lib/typescript/src/random.d.ts.map +1 -1
  51. package/lib/typescript/src/sig.d.ts +1 -1
  52. package/lib/typescript/src/subtle.d.ts.map +1 -1
  53. package/package.json +40 -52
  54. package/react-native-quick-crypto.podspec +1 -1
  55. package/src/Algorithms.ts +247 -0
  56. package/src/Hash.ts +1 -1
  57. package/src/Hashnames.ts +4 -4
  58. package/src/Utils.ts +34 -279
  59. package/src/keys.ts +5 -1
  60. package/src/pbkdf2.ts +1 -1
  61. package/src/random.ts +16 -24
  62. package/src/subtle.ts +1 -2
  63. package/LICENSE +0 -27
@@ -1,87 +1,84 @@
1
1
  project(ReactNativeQuickCrypto)
2
- cmake_minimum_required(VERSION 3.10.2)
2
+ cmake_minimum_required(VERSION 3.9.0)
3
3
 
4
4
  set(PACKAGE_NAME "reactnativequickcrypto")
5
5
  set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
6
6
  set(CMAKE_VERBOSE_MAKEFILE ON)
7
7
  set(CMAKE_CXX_STANDARD 17)
8
8
 
9
- # set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
10
- # set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
11
-
12
- # Third party libraries (Prefabs)
13
- find_package(fbjni REQUIRED CONFIG)
14
- find_package(ReactAndroid REQUIRED CONFIG)
15
- find_package(openssl REQUIRED CONFIG)
16
- find_library(LOG_LIB log)
17
-
9
+ # Define C++ library and add all sources
18
10
  add_library(
19
- ${PACKAGE_NAME}
20
- SHARED
21
- "src/main/cpp/cpp-adapter.cpp"
22
- "../cpp/MGLQuickCryptoHostObject.cpp"
23
- "../cpp/JSIUtils/MGLTypedArray.cpp"
24
- "../cpp/Utils/MGLDispatchQueue.cpp"
25
- "../cpp/JSIUtils/MGLThreadAwareHostObject.cpp"
26
- "../cpp/JSIUtils/MGLSmartHostObject.cpp"
27
- "../cpp/HMAC/MGLHmacInstaller.cpp"
28
- "../cpp/HMAC/MGLHmacHostObject.cpp"
29
- "../cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp"
30
- "../cpp/fastpbkdf2/fastpbkdf2.c"
31
- "../cpp/Random/MGLRandomHostObject.cpp"
32
- "../cpp/Hash/MGLHashInstaller.cpp"
33
- "../cpp/Hash/MGLHashHostObject.cpp"
34
- "../cpp/Cipher/MGLCipherHostObject.cpp"
35
- "../cpp/Cipher/MGLCreateCipherInstaller.cpp"
36
- "../cpp/Cipher/MGLCreateDecipherInstaller.cpp"
37
- "../cpp/MGLKeys.cpp"
38
- "../cpp/Utils/MGLUtils.cpp"
39
- "../cpp/Cipher/MGLRsa.cpp"
40
- "../cpp/Cipher/MGLGenerateKeyPairInstaller.cpp"
41
- "../cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp"
42
- "../cpp/Sig/MGLSignInstaller.cpp"
43
- "../cpp/Sig/MGLVerifyInstaller.cpp"
44
- "../cpp/Sig/MGLSignHostObjects.cpp"
45
- "../cpp/webcrypto/MGLWebCrypto.cpp"
46
- "../cpp/webcrypto/crypto_aes.cpp"
47
- "../cpp/webcrypto/crypto_ec.cpp"
48
- "../cpp/webcrypto/crypto_keygen.cpp"
11
+ ${PACKAGE_NAME} SHARED
12
+ src/main/cpp/cpp-adapter.cpp
13
+ ../cpp/MGLQuickCryptoHostObject.cpp
14
+ ../cpp/JSIUtils/MGLTypedArray.cpp
15
+ ../cpp/Utils/MGLDispatchQueue.cpp
16
+ ../cpp/JSIUtils/MGLThreadAwareHostObject.cpp
17
+ ../cpp/JSIUtils/MGLSmartHostObject.cpp
18
+ ../cpp/HMAC/MGLHmacInstaller.cpp
19
+ ../cpp/HMAC/MGLHmacHostObject.cpp
20
+ ../cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp
21
+ ../cpp/fastpbkdf2/fastpbkdf2.c
22
+ ../cpp/Random/MGLRandomHostObject.cpp
23
+ ../cpp/Hash/MGLHashInstaller.cpp
24
+ ../cpp/Hash/MGLHashHostObject.cpp
25
+ ../cpp/Cipher/MGLCipherHostObject.cpp
26
+ ../cpp/Cipher/MGLCreateCipherInstaller.cpp
27
+ ../cpp/Cipher/MGLCreateDecipherInstaller.cpp
28
+ ../cpp/MGLKeys.cpp
29
+ ../cpp/Utils/MGLUtils.cpp
30
+ ../cpp/Cipher/MGLRsa.cpp
31
+ ../cpp/Cipher/MGLGenerateKeyPairInstaller.cpp
32
+ ../cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp
33
+ ../cpp/Sig/MGLSignInstaller.cpp
34
+ ../cpp/Sig/MGLVerifyInstaller.cpp
35
+ ../cpp/Sig/MGLSignHostObjects.cpp
36
+ ../cpp/webcrypto/MGLWebCrypto.cpp
37
+ ../cpp/webcrypto/crypto_aes.cpp
38
+ ../cpp/webcrypto/crypto_ec.cpp
39
+ ../cpp/webcrypto/crypto_keygen.cpp
49
40
  )
50
41
 
51
- target_include_directories(
52
- ${PACKAGE_NAME}
53
- PRIVATE
54
- "../cpp"
55
- "src/main/cpp"
56
- "${NODE_MODULES_DIR}/react-native/ReactCommon"
57
- "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
58
- "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
59
- "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
60
- "${NODE_MODULES_DIR}/react-native/ReactCommon/turbomodule/core"
61
- "${NODE_MODULES_DIR}/react-native/ReactCommon/react/nativemodule/core"
42
+ # local includes
43
+ include_directories(
44
+ src/main/cpp
45
+ ../cpp
46
+ ../cpp/Cipher
47
+ ../cpp/fastpbkdf2
48
+ ../cpp/Hash
49
+ ../cpp/HMAC
50
+ ../cpp/JSIUtils
51
+ ../cpp/Random
52
+ ../cpp/Sig
53
+ ../cpp/Utils
54
+ ../cpp/webcrypto
62
55
  )
63
56
 
64
- #file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
57
+ # Third party libraries (Prefabs)
58
+ find_library(LOG_LIB log)
59
+ find_package(openssl REQUIRED CONFIG)
60
+ find_package(fbjni REQUIRED CONFIG)
61
+ find_package(ReactAndroid REQUIRED CONFIG)
62
+
65
63
 
66
64
  target_link_libraries(
67
65
  ${PACKAGE_NAME}
68
- fbjni::fbjni # <-- fbjni
69
- ${LOG_LIB} # <-- Logcat logger
70
- ReactAndroid::jsi # <-- RN: JSI
71
- android # <-- Android JNI core
72
- openssl::crypto # <-- OpenSSL (Crypto)
66
+ ${LOG_LIB} # <-- Logcat logger
67
+ android # <-- Android JNI core
68
+ fbjni::fbjni # <-- Facebook C++ JNI helpers
69
+ ReactAndroid::jsi # <-- RN: JSI
70
+ openssl::crypto # <-- OpenSSL (Crypto)
73
71
  )
74
72
 
75
73
  if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
76
74
  target_link_libraries(
77
75
  ${PACKAGE_NAME}
78
- ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
76
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
79
77
  )
80
78
  else()
81
79
  target_link_libraries(
82
80
  ${PACKAGE_NAME}
83
- ReactAndroid::turbomodulejsijni
84
- ReactAndroid::reactnativejni # <-- RN: React Native JNI bindings
85
- ReactAndroid::react_nativemodule_core # <-- RN: React Native native module core
86
- )
81
+ ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
82
+ ReactAndroid::turbomodulejsijni # <-- RN: TurboModules utils (e.g. CallInvokerHolder)
83
+ )
87
84
  endif()
@@ -7,15 +7,13 @@ def agpVersionMinor = agpVersion.tokenize('.')[1].toInteger()
7
7
 
8
8
  buildscript {
9
9
  repositories {
10
- maven {
11
- url "https://plugins.gradle.org/m2/"
12
- }
13
- mavenCentral()
14
10
  google()
11
+ mavenCentral()
15
12
  }
16
13
 
17
14
  dependencies {
18
- classpath("com.android.tools.build:gradle:8.3.1")
15
+ classpath("com.android.tools.build:gradle:8.7.3")
16
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
19
17
  }
20
18
  }
21
19
 
@@ -34,10 +32,12 @@ def resolveBuildType() {
34
32
  return tskReqStr.contains('Release') ? 'release' : 'debug'
35
33
  }
36
34
 
35
+ apply plugin: 'com.android.library'
36
+ apply plugin: "org.jetbrains.kotlin.android"
37
+
37
38
  if (isNewArchitectureEnabled()) {
38
39
  apply plugin: "com.facebook.react"
39
40
  }
40
- apply plugin: 'com.android.library'
41
41
 
42
42
  def safeExtGet(prop, fallback) {
43
43
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
@@ -112,7 +112,7 @@ android {
112
112
  externalNativeBuild {
113
113
  cmake {
114
114
  cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all -DANDROID"
115
- arguments "-DANDROID_STL=c++_shared", "-DNODE_MODULES_DIR=${nodeModules}"
115
+ arguments "-DANDROID_STL=c++_shared"
116
116
  abiFilters (*reactNativeArchitectures())
117
117
  }
118
118
  }
@@ -171,8 +171,9 @@ repositories {
171
171
  dependencies {
172
172
  //noinspection GradleDynamicVersion
173
173
  implementation "com.facebook.react:react-android:+"
174
- // https://mvnrepository.com/artifact/com.android.ndk.thirdparty/openssl
175
- implementation 'com.android.ndk.thirdparty:openssl:1.1.1q-beta-1'
174
+
175
+ // Add a dependency on OpenSSL
176
+ implementation 'io.github.ronickg:openssl:3.3.2'
176
177
  }
177
178
 
178
179
  // Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct".
@@ -2,8 +2,10 @@
2
2
  #include <fbjni/fbjni.h>
3
3
  #include <jni.h>
4
4
  #include <jsi/jsi.h>
5
+ #include <memory>
5
6
 
6
7
  #include "MGLQuickCryptoHostObject.h"
8
+ #include "JSIUtils/MGLTypedArray.h"
7
9
 
8
10
  using namespace facebook;
9
11
 
@@ -28,6 +30,13 @@ class CryptoCppAdapter : public jni::HybridClass<CryptoCppAdapter> {
28
30
  auto object = jsi::Object::createFromHostObject(runtime, hostObject);
29
31
  runtime.global().setProperty(runtime, "__QuickCryptoProxy",
30
32
  std::move(object));
33
+ // Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets destroyed and the cache gets invalidated.
34
+ auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(runtime);
35
+ runtime.global().setProperty(
36
+ runtime,
37
+ "rnqcArrayBufferPropNameIdCache",
38
+ jsi::Object::createFromHostObject(runtime, propNameIdCache)
39
+ );
31
40
  }
32
41
 
33
42
  void nativeInstall(
@@ -10,6 +10,8 @@
10
10
 
11
11
  #include <algorithm>
12
12
  #include <memory>
13
+ #include <utility>
14
+ #include <vector>
13
15
  #include <string>
14
16
  #include <unordered_map>
15
17
 
@@ -40,33 +42,37 @@ enum class Prop {
40
42
  class PropNameIDCache {
41
43
  public:
42
44
  const jsi::PropNameID &get(jsi::Runtime &runtime, Prop prop) {
43
- if (!this->props[prop]) {
44
- this->props[prop] =
45
- std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
45
+ auto key = reinterpret_cast<uintptr_t>(&runtime);
46
+ if (this->props.find(key) == this->props.end()) {
47
+ this->props[key] = std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>();
46
48
  }
47
- return *(this->props[prop]);
49
+ if (!this->props[key][prop]) {
50
+ this->props[key][prop] = std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
51
+ }
52
+ return *(this->props[key][prop]);
48
53
  }
49
54
 
50
- const jsi::PropNameID &getConstructorNameProp(jsi::Runtime &runtime,
51
- MGLTypedArrayKind kind);
55
+ const jsi::PropNameID &getConstructorNameProp(jsi::Runtime &runtime, MGLTypedArrayKind kind);
52
56
 
53
- void invalidate() {
54
- /** This call (and attempts to use props.clear()) crash 💥 when the
55
- * JSI runtime has already been destroyed. So we are commenting it out
56
- * and waiting for Nitro and 1.0 to fix this the proper way.
57
- */
58
- //props.erase(props.begin(), props.end());
57
+ void invalidate(uintptr_t key) {
58
+ if (props.find(key) != props.end()) {
59
+ props[key].clear();
60
+ }
59
61
  }
60
-
61
62
  private:
62
- std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>> props;
63
+ std::unordered_map<uintptr_t, std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>> props;
63
64
 
64
65
  jsi::PropNameID createProp(jsi::Runtime &runtime, Prop prop);
65
66
  };
66
67
 
67
68
  PropNameIDCache propNameIDCache;
68
69
 
69
- void invalidateJsiPropNameIDCache() { propNameIDCache.invalidate(); }
70
+ InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime &runtime) {
71
+ key = reinterpret_cast<uintptr_t>(&runtime);
72
+ }
73
+ InvalidateCacheOnDestroy::~InvalidateCacheOnDestroy() {
74
+ propNameIDCache.invalidate(key);
75
+ }
70
76
 
71
77
  MGLTypedArrayKind getTypedArrayKindForName(const std::string &name);
72
78
 
@@ -75,8 +81,9 @@ MGLTypedArrayBase::MGLTypedArrayBase(jsi::Runtime &runtime, size_t size,
75
81
  : MGLTypedArrayBase(
76
82
  runtime,
77
83
  runtime.global()
78
- .getProperty(runtime, propNameIDCache.getConstructorNameProp(
79
- runtime, kind))
84
+ .getProperty(
85
+ runtime,
86
+ propNameIDCache.getConstructorNameProp(runtime, kind))
80
87
  .asObject(runtime)
81
88
  .asFunction(runtime)
82
89
  .callAsConstructor(runtime, {static_cast<double>(size)})
@@ -236,6 +243,20 @@ void MGLTypedArray<T>::update(jsi::Runtime &runtime,
236
243
  reinterpret_cast<ContentType<T> *>(rawData));
237
244
  }
238
245
 
246
+ template <MGLTypedArrayKind T>
247
+ void MGLTypedArray<T>::updateUnsafe(jsi::Runtime &runtime, ContentType<T> *data, size_t length) {
248
+ if (length != size(runtime)) {
249
+ throw jsi::JSError(runtime, "TypedArray can only be updated with an array of the same size");
250
+ }
251
+ uint8_t *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
252
+ memcpy(rawData, data, length);
253
+ }
254
+
255
+ template <MGLTypedArrayKind T>
256
+ uint8_t* MGLTypedArray<T>::data(jsi::Runtime &runtime) {
257
+ return getBuffer(runtime).data(runtime) + byteOffset(runtime);
258
+ }
259
+
239
260
  const jsi::PropNameID &PropNameIDCache::getConstructorNameProp(
240
261
  jsi::Runtime &runtime, MGLTypedArrayKind kind) {
241
262
  switch (kind) {
@@ -69,7 +69,24 @@ struct typedArrayTypeMap<MGLTypedArrayKind::Float64Array> {
69
69
  typedef double type;
70
70
  };
71
71
 
72
- void invalidateJsiPropNameIDCache();
72
+ // Instance of this class will invalidate PropNameIDCache when destructor is called.
73
+ // Attach this object to global in specific jsi::Runtime to make sure lifecycle of
74
+ // the cache object is connected to the lifecycle of the js runtime
75
+ class InvalidateCacheOnDestroy : public jsi::HostObject {
76
+ public:
77
+ explicit InvalidateCacheOnDestroy(jsi::Runtime &runtime);
78
+ virtual ~InvalidateCacheOnDestroy();
79
+ virtual jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) {
80
+ return jsi::Value::null();
81
+ }
82
+ virtual void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) {}
83
+ virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) {
84
+ return {};
85
+ }
86
+
87
+ private:
88
+ uintptr_t key;
89
+ };
73
90
 
74
91
  class MGLTypedArrayBase : public jsi::Object {
75
92
  public:
@@ -126,6 +143,8 @@ class MGLTypedArray : public MGLTypedArrayBase {
126
143
 
127
144
  std::vector<ContentType<T>> toVector(jsi::Runtime &runtime);
128
145
  void update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data);
146
+ void updateUnsafe(jsi::Runtime &runtime, ContentType<T> *data, size_t length);
147
+ uint8_t* data(jsi::Runtime &runtime);
129
148
  };
130
149
 
131
150
  template <MGLTypedArrayKind T>
@@ -22,7 +22,7 @@ class JSI_EXPORT MGLQuickCryptoHostObject : public MGLSmartHostObject {
22
22
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
23
23
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
24
24
 
25
- virtual ~MGLQuickCryptoHostObject() { invalidateJsiPropNameIDCache(); }
25
+ virtual ~MGLQuickCryptoHostObject() {}
26
26
  };
27
27
 
28
28
  } // namespace margelo
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Workspace
3
+ version = "1.0">
4
+ <FileRef
5
+ location = "self:">
6
+ </FileRef>
7
+ </Workspace>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>SchemeUserState</key>
6
+ <dict>
7
+ <key>QuickCrypto.xcscheme_^#shared#^_</key>
8
+ <dict>
9
+ <key>orderHint</key>
10
+ <integer>0</integer>
11
+ </dict>
12
+ </dict>
13
+ </dict>
14
+ </plist>
@@ -6,6 +6,7 @@
6
6
  #import <jsi/jsi.h>
7
7
 
8
8
  #import "../cpp/MGLQuickCryptoHostObject.h"
9
+ #import "../cpp/JSIUtils/MGLTypedArray.h"
9
10
 
10
11
  @implementation QuickCryptoModule
11
12
 
@@ -40,6 +41,15 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
40
41
  auto object = jsi::Object::createFromHostObject(runtime, hostObject);
41
42
  runtime.global().setProperty(runtime, "__QuickCryptoProxy", std::move(object));
42
43
 
44
+ // Adds the PropNameIDCache object to the Runtime. If the Runtime gets
45
+ // destroyed, the Object gets destroyed and the cache gets invalidated.
46
+ auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(runtime);
47
+ runtime.global().setProperty(
48
+ runtime,
49
+ "rnqcArrayBufferPropNameIdCache",
50
+ jsi::Object::createFromHostObject(runtime, propNameIdCache)
51
+ );
52
+
43
53
  NSLog(@"Successfully installed JSI bindings for react-native-quick-crypto!");
44
54
  return @true;
45
55
  }
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.normalizeAlgorithm = void 0;
7
+ const kSupportedAlgorithms = {
8
+ digest: {
9
+ 'SHA-1': null,
10
+ 'SHA-256': null,
11
+ 'SHA-384': null,
12
+ 'SHA-512': null
13
+ },
14
+ generateKey: {
15
+ 'RSASSA-PKCS1-v1_5': 'RsaHashedKeyGenParams',
16
+ 'RSA-PSS': 'RsaHashedKeyGenParams',
17
+ 'RSA-OAEP': 'RsaHashedKeyGenParams',
18
+ ECDSA: 'EcKeyGenParams',
19
+ ECDH: 'EcKeyGenParams',
20
+ 'AES-CTR': 'AesKeyGenParams',
21
+ 'AES-CBC': 'AesKeyGenParams',
22
+ 'AES-GCM': 'AesKeyGenParams',
23
+ 'AES-KW': 'AesKeyGenParams',
24
+ HMAC: 'HmacKeyGenParams',
25
+ X25519: null,
26
+ Ed25519: null,
27
+ X448: null,
28
+ Ed448: null
29
+ },
30
+ sign: {
31
+ 'RSASSA-PKCS1-v1_5': null,
32
+ 'RSA-PSS': 'RsaPssParams',
33
+ ECDSA: 'EcdsaParams',
34
+ HMAC: null,
35
+ Ed25519: null,
36
+ Ed448: 'Ed448Params'
37
+ },
38
+ verify: {
39
+ 'RSASSA-PKCS1-v1_5': null,
40
+ 'RSA-PSS': 'RsaPssParams',
41
+ ECDSA: 'EcdsaParams',
42
+ HMAC: null,
43
+ Ed25519: null,
44
+ Ed448: 'Ed448Params'
45
+ },
46
+ importKey: {
47
+ 'RSASSA-PKCS1-v1_5': 'RsaHashedImportParams',
48
+ 'RSA-PSS': 'RsaHashedImportParams',
49
+ 'RSA-OAEP': 'RsaHashedImportParams',
50
+ ECDSA: 'EcKeyImportParams',
51
+ ECDH: 'EcKeyImportParams',
52
+ HMAC: 'HmacImportParams',
53
+ HKDF: null,
54
+ PBKDF2: null,
55
+ 'AES-CTR': null,
56
+ 'AES-CBC': null,
57
+ 'AES-GCM': null,
58
+ 'AES-KW': null,
59
+ Ed25519: null,
60
+ X25519: null,
61
+ Ed448: null,
62
+ X448: null
63
+ },
64
+ deriveBits: {
65
+ HKDF: 'HkdfParams',
66
+ PBKDF2: 'Pbkdf2Params',
67
+ ECDH: 'EcdhKeyDeriveParams',
68
+ X25519: 'EcdhKeyDeriveParams',
69
+ X448: 'EcdhKeyDeriveParams'
70
+ },
71
+ encrypt: {
72
+ 'RSA-OAEP': 'RsaOaepParams',
73
+ 'AES-CBC': 'AesCbcParams',
74
+ 'AES-GCM': 'AesGcmParams',
75
+ 'AES-CTR': 'AesCtrParams'
76
+ },
77
+ decrypt: {
78
+ 'RSA-OAEP': 'RsaOaepParams',
79
+ 'AES-CBC': 'AesCbcParams',
80
+ 'AES-GCM': 'AesGcmParams',
81
+ 'AES-CTR': 'AesCtrParams'
82
+ },
83
+ 'get key length': {
84
+ 'AES-CBC': 'AesDerivedKeyParams',
85
+ 'AES-CTR': 'AesDerivedKeyParams',
86
+ 'AES-GCM': 'AesDerivedKeyParams',
87
+ 'AES-KW': 'AesDerivedKeyParams',
88
+ HMAC: 'HmacImportParams',
89
+ HKDF: null,
90
+ PBKDF2: null
91
+ },
92
+ wrapKey: {
93
+ 'AES-KW': null
94
+ },
95
+ unwrapKey: {
96
+ 'AES-KW': null
97
+ }
98
+ };
99
+ const simpleAlgorithmDictionaries = {
100
+ AesGcmParams: {
101
+ iv: 'BufferSource',
102
+ additionalData: 'BufferSource'
103
+ },
104
+ RsaHashedKeyGenParams: {
105
+ hash: 'HashAlgorithmIdentifier'
106
+ },
107
+ EcKeyGenParams: {},
108
+ HmacKeyGenParams: {
109
+ hash: 'HashAlgorithmIdentifier'
110
+ },
111
+ RsaPssParams: {},
112
+ EcdsaParams: {
113
+ hash: 'HashAlgorithmIdentifier'
114
+ },
115
+ HmacImportParams: {
116
+ hash: 'HashAlgorithmIdentifier'
117
+ },
118
+ HkdfParams: {
119
+ hash: 'HashAlgorithmIdentifier',
120
+ salt: 'BufferSource',
121
+ info: 'BufferSource'
122
+ },
123
+ Ed448Params: {
124
+ context: 'BufferSource'
125
+ },
126
+ Pbkdf2Params: {
127
+ hash: 'HashAlgorithmIdentifier',
128
+ salt: 'BufferSource'
129
+ },
130
+ RsaOaepParams: {
131
+ label: 'BufferSource'
132
+ },
133
+ RsaHashedImportParams: {
134
+ hash: 'HashAlgorithmIdentifier'
135
+ },
136
+ EcKeyImportParams: {}
137
+ };
138
+
139
+ // https://w3c.github.io/webcrypto/#algorithm-normalization-normalize-an-algorithm
140
+ // adapted for Node.js from Deno's implementation
141
+ // https://github.com/denoland/deno/blob/v1.29.1/ext/crypto/00_crypto.js#L195
142
+ const normalizeAlgorithm = (algorithm, op) => {
143
+ if (typeof algorithm === 'string') {
144
+ return normalizeAlgorithm({
145
+ name: algorithm
146
+ }, op);
147
+ }
148
+
149
+ // 1.
150
+ const registeredAlgorithms = kSupportedAlgorithms[op];
151
+ // 2. 3.
152
+ // commented, because typescript takes care of this for us 🤞👀
153
+ // const initialAlg = webidl.converters.Algorithm(algorithm, {
154
+ // prefix: 'Failed to normalize algorithm',
155
+ // context: 'passed algorithm',
156
+ // });
157
+
158
+ // 4.
159
+ let algName = algorithm.name;
160
+ if (algName === undefined) return {
161
+ name: 'unknown'
162
+ };
163
+
164
+ // 5.
165
+ let desiredType;
166
+ for (const key in registeredAlgorithms) {
167
+ if (!Object.prototype.hasOwnProperty.call(registeredAlgorithms, key)) {
168
+ continue;
169
+ }
170
+ if (key.toUpperCase() === algName.toUpperCase()) {
171
+ algName = key;
172
+ desiredType = registeredAlgorithms[algName];
173
+ }
174
+ }
175
+ if (desiredType === undefined) throw new Error(`Unrecognized algorithm name: ${algName}`);
176
+
177
+ // Fast path everything below if the registered dictionary is null
178
+ if (desiredType === null) return {
179
+ name: algName
180
+ };
181
+
182
+ // 6.
183
+ const normalizedAlgorithm = algorithm;
184
+ // TODO: implement this? Maybe via typescript?
185
+ // webidl.converters[desiredType](algorithm, {
186
+ // prefix: 'Failed to normalize algorithm',
187
+ // context: 'passed algorithm',
188
+ // });
189
+ // 7.
190
+ normalizedAlgorithm.name = algName;
191
+
192
+ // 9.
193
+ const dict = simpleAlgorithmDictionaries[desiredType];
194
+ // 10.
195
+ const dictKeys = dict ? Object.keys(dict) : [];
196
+ for (let i = 0; i < dictKeys.length; i++) {
197
+ const member = dictKeys[i] || '';
198
+ if (!Object.prototype.hasOwnProperty.call(dict, member)) continue;
199
+ // TODO: implement this? Maybe via typescript?
200
+ // const idlType = dict[member];
201
+ // const idlValue = normalizedAlgorithm[member];
202
+ // 3.
203
+ // if (idlType === 'BufferSource' && idlValue) {
204
+ // const isView = ArrayBufferIsView(idlValue);
205
+ // normalizedAlgorithm[member] = TypedArrayPrototypeSlice(
206
+ // new Uint8Array(
207
+ // isView ? getDataViewOrTypedArrayBuffer(idlValue) : idlValue,
208
+ // isView ? getDataViewOrTypedArrayByteOffset(idlValue) : 0,
209
+ // isView
210
+ // ? getDataViewOrTypedArrayByteLength(idlValue)
211
+ // : ArrayBufferPrototypeGetByteLength(idlValue)
212
+ // )
213
+ // );
214
+ // } else if (idlType === 'HashAlgorithmIdentifier') {
215
+ // normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, 'digest');
216
+ // } else if (idlType === 'AlgorithmIdentifier') {
217
+ // // This extension point is not used by any supported algorithm (yet?)
218
+ // throw lazyDOMException('Not implemented.', 'NotSupportedError');
219
+ // }
220
+ }
221
+ return normalizedAlgorithm;
222
+ };
223
+ exports.normalizeAlgorithm = normalizeAlgorithm;
224
+ //# sourceMappingURL=Algorithms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["kSupportedAlgorithms","digest","generateKey","ECDSA","ECDH","HMAC","X25519","Ed25519","X448","Ed448","sign","verify","importKey","HKDF","PBKDF2","deriveBits","encrypt","decrypt","wrapKey","unwrapKey","simpleAlgorithmDictionaries","AesGcmParams","iv","additionalData","RsaHashedKeyGenParams","hash","EcKeyGenParams","HmacKeyGenParams","RsaPssParams","EcdsaParams","HmacImportParams","HkdfParams","salt","info","Ed448Params","context","Pbkdf2Params","RsaOaepParams","label","RsaHashedImportParams","EcKeyImportParams","normalizeAlgorithm","algorithm","op","name","registeredAlgorithms","algName","undefined","desiredType","key","Object","prototype","hasOwnProperty","call","toUpperCase","Error","normalizedAlgorithm","dict","dictKeys","keys","i","length","member","exports"],"sourceRoot":"../../src","sources":["Algorithms.ts"],"mappings":";;;;;;AA6CA,MAAMA,oBAAyC,GAAG;EAChDC,MAAM,EAAE;IACN,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,IAAI;IACf,SAAS,EAAE,IAAI;IACf,SAAS,EAAE;EACb,CAAC;EACDC,WAAW,EAAE;IACX,mBAAmB,EAAE,uBAAuB;IAC5C,SAAS,EAAE,uBAAuB;IAClC,UAAU,EAAE,uBAAuB;IACnCC,KAAK,EAAE,gBAAgB;IACvBC,IAAI,EAAE,gBAAgB;IACtB,SAAS,EAAE,iBAAiB;IAC5B,SAAS,EAAE,iBAAiB;IAC5B,SAAS,EAAE,iBAAiB;IAC5B,QAAQ,EAAE,iBAAiB;IAC3BC,IAAI,EAAE,kBAAkB;IACxBC,MAAM,EAAE,IAAI;IACZC,OAAO,EAA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EAAE;IAChC,IAAI,CAACV,MAAM,CAACC,SAAS,CAACC,cAAc,CAACC,IAAI,CAACI,IAAI,EAAEK,MAAM,CAAC,EAAE;IACzD;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;EACF;EAEA,OAAON,mBAAmB;AAC5B,CAAC;AAACO,OAAA,CAAAtB,kBAAA,GAAAA,kBAAA","ignoreList":[]}
@@ -97,6 +97,6 @@ const internalDigest = (algorithm, data) => {
97
97
  const normalizedHashName = (0, _Utils.normalizeHashName)(algorithm.name);
98
98
  const hash = new Hash(normalizedHashName);
99
99
  hash.update((0, _Utils.bufferLikeToArrayBuffer)(data));
100
- return hash.digest();
100
+ return (0, _Utils.toArrayBuffer)(hash.digest());
101
101
  };
102
102
  //# sourceMappingURL=Hash.js.map