react-native-quick-crypto 1.0.0-beta.2 → 1.0.0-beta.4

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 +3 -1
  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 +18 -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
@@ -0,0 +1,93 @@
1
+ ///
2
+ /// KeyDetail.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2024 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
11
+ #include <NitroModules/JSIConverter.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
16
+ #include <NitroModules/NitroDefines.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+
21
+
22
+
23
+ #include <optional>
24
+ #include <string>
25
+
26
+ namespace margelo::nitro::crypto {
27
+
28
+ /**
29
+ * A struct which can be represented as a JavaScript object (KeyDetail).
30
+ */
31
+ struct KeyDetail {
32
+ public:
33
+ std::optional<double> length SWIFT_PRIVATE;
34
+ std::optional<double> publicExponent SWIFT_PRIVATE;
35
+ std::optional<double> modulusLength SWIFT_PRIVATE;
36
+ std::optional<std::string> hashAlgorithm SWIFT_PRIVATE;
37
+ std::optional<std::string> mgf1HashAlgorithm SWIFT_PRIVATE;
38
+ std::optional<double> saltLength SWIFT_PRIVATE;
39
+ std::optional<std::string> namedCurve SWIFT_PRIVATE;
40
+
41
+ public:
42
+ explicit KeyDetail(std::optional<double> length, std::optional<double> publicExponent, std::optional<double> modulusLength, std::optional<std::string> hashAlgorithm, std::optional<std::string> mgf1HashAlgorithm, std::optional<double> saltLength, std::optional<std::string> namedCurve): length(length), publicExponent(publicExponent), modulusLength(modulusLength), hashAlgorithm(hashAlgorithm), mgf1HashAlgorithm(mgf1HashAlgorithm), saltLength(saltLength), namedCurve(namedCurve) {}
43
+ };
44
+
45
+ } // namespace margelo::nitro::crypto
46
+
47
+ namespace margelo::nitro {
48
+
49
+ using namespace margelo::nitro::crypto;
50
+
51
+ // C++ KeyDetail <> JS KeyDetail (object)
52
+ template <>
53
+ struct JSIConverter<KeyDetail> {
54
+ static inline KeyDetail fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
55
+ jsi::Object obj = arg.asObject(runtime);
56
+ return KeyDetail(
57
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, "length")),
58
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, "publicExponent")),
59
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, "modulusLength")),
60
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "hashAlgorithm")),
61
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "mgf1HashAlgorithm")),
62
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, "saltLength")),
63
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "namedCurve"))
64
+ );
65
+ }
66
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const KeyDetail& arg) {
67
+ jsi::Object obj(runtime);
68
+ obj.setProperty(runtime, "length", JSIConverter<std::optional<double>>::toJSI(runtime, arg.length));
69
+ obj.setProperty(runtime, "publicExponent", JSIConverter<std::optional<double>>::toJSI(runtime, arg.publicExponent));
70
+ obj.setProperty(runtime, "modulusLength", JSIConverter<std::optional<double>>::toJSI(runtime, arg.modulusLength));
71
+ obj.setProperty(runtime, "hashAlgorithm", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.hashAlgorithm));
72
+ obj.setProperty(runtime, "mgf1HashAlgorithm", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.mgf1HashAlgorithm));
73
+ obj.setProperty(runtime, "saltLength", JSIConverter<std::optional<double>>::toJSI(runtime, arg.saltLength));
74
+ obj.setProperty(runtime, "namedCurve", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.namedCurve));
75
+ return obj;
76
+ }
77
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
78
+ if (!value.isObject()) {
79
+ return false;
80
+ }
81
+ jsi::Object obj = value.getObject(runtime);
82
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, "length"))) return false;
83
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, "publicExponent"))) return false;
84
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, "modulusLength"))) return false;
85
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "hashAlgorithm"))) return false;
86
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "mgf1HashAlgorithm"))) return false;
87
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, "saltLength"))) return false;
88
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "namedCurve"))) return false;
89
+ return true;
90
+ }
91
+ };
92
+
93
+ } // namespace margelo::nitro
@@ -0,0 +1,66 @@
1
+ ///
2
+ /// KeyEncoding.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2024 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #include <cmath>
11
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
12
+ #include <NitroModules/JSIConverter.hpp>
13
+ #else
14
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
15
+ #endif
16
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
17
+ #include <NitroModules/NitroDefines.hpp>
18
+ #else
19
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
20
+ #endif
21
+
22
+ namespace margelo::nitro::crypto {
23
+
24
+ /**
25
+ * An enum which can be represented as a JavaScript enum (KeyEncoding).
26
+ */
27
+ enum class KeyEncoding {
28
+ KKEYENCODINGPKCS1 SWIFT_NAME(kkeyencodingpkcs1) = 0,
29
+ KKEYENCODINGPKCS8 SWIFT_NAME(kkeyencodingpkcs8) = 1,
30
+ KKEYENCODINGSPKI SWIFT_NAME(kkeyencodingspki) = 2,
31
+ KKEYENCODINGSEC1 SWIFT_NAME(kkeyencodingsec1) = 3,
32
+ } CLOSED_ENUM;
33
+
34
+ } // namespace margelo::nitro::crypto
35
+
36
+ namespace margelo::nitro {
37
+
38
+ using namespace margelo::nitro::crypto;
39
+
40
+ // C++ KeyEncoding <> JS KeyEncoding (enum)
41
+ template <>
42
+ struct JSIConverter<KeyEncoding> {
43
+ static inline KeyEncoding fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
44
+ int enumValue = JSIConverter<int>::fromJSI(runtime, arg);
45
+ return static_cast<KeyEncoding>(enumValue);
46
+ }
47
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, KeyEncoding arg) {
48
+ int enumValue = static_cast<int>(arg);
49
+ return JSIConverter<int>::toJSI(runtime, enumValue);
50
+ }
51
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
52
+ if (!value.isNumber()) {
53
+ return false;
54
+ }
55
+ double integer;
56
+ double fraction = modf(value.getNumber(), &integer);
57
+ if (fraction != 0.0) {
58
+ // It is some kind of floating point number - our enums are ints.
59
+ return false;
60
+ }
61
+ // Check if we are within the bounds of the enum.
62
+ return integer >= 0 && integer <= 3;
63
+ }
64
+ };
65
+
66
+ } // namespace margelo::nitro
@@ -0,0 +1,65 @@
1
+ ///
2
+ /// KeyType.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2024 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #include <cmath>
11
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
12
+ #include <NitroModules/JSIConverter.hpp>
13
+ #else
14
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
15
+ #endif
16
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
17
+ #include <NitroModules/NitroDefines.hpp>
18
+ #else
19
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
20
+ #endif
21
+
22
+ namespace margelo::nitro::crypto {
23
+
24
+ /**
25
+ * An enum which can be represented as a JavaScript enum (KeyType).
26
+ */
27
+ enum class KeyType {
28
+ SECRET SWIFT_NAME(secret) = 0,
29
+ PUBLIC SWIFT_NAME(public) = 1,
30
+ PRIVATE SWIFT_NAME(private) = 2,
31
+ } CLOSED_ENUM;
32
+
33
+ } // namespace margelo::nitro::crypto
34
+
35
+ namespace margelo::nitro {
36
+
37
+ using namespace margelo::nitro::crypto;
38
+
39
+ // C++ KeyType <> JS KeyType (enum)
40
+ template <>
41
+ struct JSIConverter<KeyType> {
42
+ static inline KeyType fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
43
+ int enumValue = JSIConverter<int>::fromJSI(runtime, arg);
44
+ return static_cast<KeyType>(enumValue);
45
+ }
46
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, KeyType arg) {
47
+ int enumValue = static_cast<int>(arg);
48
+ return JSIConverter<int>::toJSI(runtime, enumValue);
49
+ }
50
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
51
+ if (!value.isNumber()) {
52
+ return false;
53
+ }
54
+ double integer;
55
+ double fraction = modf(value.getNumber(), &integer);
56
+ if (fraction != 0.0) {
57
+ // It is some kind of floating point number - our enums are ints.
58
+ return false;
59
+ }
60
+ // Check if we are within the bounds of the enum.
61
+ return integer >= 0 && integer <= 2;
62
+ }
63
+ };
64
+
65
+ } // namespace margelo::nitro
@@ -0,0 +1,102 @@
1
+ ///
2
+ /// KeyUsage.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2024 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::crypto {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (KeyUsage).
30
+ */
31
+ enum class KeyUsage {
32
+ ENCRYPT SWIFT_NAME(encrypt) = 0,
33
+ DECRYPT SWIFT_NAME(decrypt) = 1,
34
+ SIGN SWIFT_NAME(sign) = 2,
35
+ VERIFY SWIFT_NAME(verify) = 3,
36
+ DERIVEKEY SWIFT_NAME(derivekey) = 4,
37
+ DERIVEBITS SWIFT_NAME(derivebits) = 5,
38
+ WRAPKEY SWIFT_NAME(wrapkey) = 6,
39
+ UNWRAPKEY SWIFT_NAME(unwrapkey) = 7,
40
+ } CLOSED_ENUM;
41
+
42
+ } // namespace margelo::nitro::crypto
43
+
44
+ namespace margelo::nitro {
45
+
46
+ using namespace margelo::nitro::crypto;
47
+
48
+ // C++ KeyUsage <> JS KeyUsage (union)
49
+ template <>
50
+ struct JSIConverter<KeyUsage> {
51
+ static inline KeyUsage fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
52
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
53
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
54
+ case hashString("encrypt"): return KeyUsage::ENCRYPT;
55
+ case hashString("decrypt"): return KeyUsage::DECRYPT;
56
+ case hashString("sign"): return KeyUsage::SIGN;
57
+ case hashString("verify"): return KeyUsage::VERIFY;
58
+ case hashString("deriveKey"): return KeyUsage::DERIVEKEY;
59
+ case hashString("deriveBits"): return KeyUsage::DERIVEBITS;
60
+ case hashString("wrapKey"): return KeyUsage::WRAPKEY;
61
+ case hashString("unwrapKey"): return KeyUsage::UNWRAPKEY;
62
+ default: [[unlikely]]
63
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum KeyUsage - invalid value!");
64
+ }
65
+ }
66
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, KeyUsage arg) {
67
+ switch (arg) {
68
+ case KeyUsage::ENCRYPT: return JSIConverter<std::string>::toJSI(runtime, "encrypt");
69
+ case KeyUsage::DECRYPT: return JSIConverter<std::string>::toJSI(runtime, "decrypt");
70
+ case KeyUsage::SIGN: return JSIConverter<std::string>::toJSI(runtime, "sign");
71
+ case KeyUsage::VERIFY: return JSIConverter<std::string>::toJSI(runtime, "verify");
72
+ case KeyUsage::DERIVEKEY: return JSIConverter<std::string>::toJSI(runtime, "deriveKey");
73
+ case KeyUsage::DERIVEBITS: return JSIConverter<std::string>::toJSI(runtime, "deriveBits");
74
+ case KeyUsage::WRAPKEY: return JSIConverter<std::string>::toJSI(runtime, "wrapKey");
75
+ case KeyUsage::UNWRAPKEY: return JSIConverter<std::string>::toJSI(runtime, "unwrapKey");
76
+ default: [[unlikely]]
77
+ throw std::invalid_argument("Cannot convert KeyUsage to JS - invalid value: "
78
+ + std::to_string(static_cast<int>(arg)) + "!");
79
+ }
80
+ }
81
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
82
+ if (!value.isString()) {
83
+ return false;
84
+ }
85
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
86
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
87
+ case hashString("encrypt"):
88
+ case hashString("decrypt"):
89
+ case hashString("sign"):
90
+ case hashString("verify"):
91
+ case hashString("deriveKey"):
92
+ case hashString("deriveBits"):
93
+ case hashString("wrapKey"):
94
+ case hashString("unwrapKey"):
95
+ return true;
96
+ default:
97
+ return false;
98
+ }
99
+ }
100
+ };
101
+
102
+ } // namespace margelo::nitro
@@ -0,0 +1,82 @@
1
+ ///
2
+ /// NamedCurve.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2024 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::crypto {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (NamedCurve).
30
+ */
31
+ enum class NamedCurve {
32
+ P_256 SWIFT_NAME(p256) = 0,
33
+ P_384 SWIFT_NAME(p384) = 1,
34
+ P_521 SWIFT_NAME(p521) = 2,
35
+ } CLOSED_ENUM;
36
+
37
+ } // namespace margelo::nitro::crypto
38
+
39
+ namespace margelo::nitro {
40
+
41
+ using namespace margelo::nitro::crypto;
42
+
43
+ // C++ NamedCurve <> JS NamedCurve (union)
44
+ template <>
45
+ struct JSIConverter<NamedCurve> {
46
+ static inline NamedCurve fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
47
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
48
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
49
+ case hashString("P-256"): return NamedCurve::P_256;
50
+ case hashString("P-384"): return NamedCurve::P_384;
51
+ case hashString("P-521"): return NamedCurve::P_521;
52
+ default: [[unlikely]]
53
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum NamedCurve - invalid value!");
54
+ }
55
+ }
56
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, NamedCurve arg) {
57
+ switch (arg) {
58
+ case NamedCurve::P_256: return JSIConverter<std::string>::toJSI(runtime, "P-256");
59
+ case NamedCurve::P_384: return JSIConverter<std::string>::toJSI(runtime, "P-384");
60
+ case NamedCurve::P_521: return JSIConverter<std::string>::toJSI(runtime, "P-521");
61
+ default: [[unlikely]]
62
+ throw std::invalid_argument("Cannot convert NamedCurve to JS - invalid value: "
63
+ + std::to_string(static_cast<int>(arg)) + "!");
64
+ }
65
+ }
66
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
67
+ if (!value.isString()) {
68
+ return false;
69
+ }
70
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
71
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
72
+ case hashString("P-256"):
73
+ case hashString("P-384"):
74
+ case hashString("P-521"):
75
+ return true;
76
+ default:
77
+ return false;
78
+ }
79
+ }
80
+ };
81
+
82
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.4",
4
4
  "description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
5
5
  "type": "module",
6
6
  "main": "lib/commonjs/index",
@@ -17,6 +17,7 @@
17
17
  "android/src",
18
18
  "ios",
19
19
  "cpp",
20
+ "deps",
20
21
  "nitrogen",
21
22
  "react-native.config.js",
22
23
  "app.plugin.js",
@@ -27,15 +28,17 @@
27
28
  "!**/__mocks__"
28
29
  ],
29
30
  "scripts": {
30
- "clean": "del-cli lib",
31
+ "clean": "del-cli android/build lib",
31
32
  "tsc": "tsc --noEmit",
32
33
  "typescript": "tsc --noEmit",
33
34
  "lint": "eslint \"**/*.{js,ts,tsx}\"",
34
- "lint-fix": "eslint \"**/*.{js,ts,tsx}\" --fix",
35
+ "lint:fix": "eslint \"**/*.{js,ts,tsx}\" --fix",
36
+ "format": "prettier --check \"**/*.{js,ts,tsx}\"",
37
+ "format:fix": "prettier --write \"**/*.{js,ts,tsx}\"",
35
38
  "prepare": "bun clean && bob build",
36
39
  "release-it": "bun prepare && release-it",
37
- "test": "jest",
38
- "postinstall": "bun tsc"
40
+ "specs": "nitro-codegen",
41
+ "test": "jest"
39
42
  },
40
43
  "keywords": [
41
44
  "react-native",
@@ -66,7 +69,7 @@
66
69
  "dependencies": {
67
70
  "@craftzdog/react-native-buffer": "6.0.5",
68
71
  "events": "3.3.0",
69
- "react-native-nitro-modules": "0.4.0",
72
+ "react-native-nitro-modules": "0.14.0",
70
73
  "react-native-quick-base64": "2.1.2",
71
74
  "readable-stream": "4.5.2",
72
75
  "string_decoder": "1.3.0",
@@ -84,11 +87,9 @@
84
87
  "eslint": "9.9.0",
85
88
  "eslint-plugin-react-native": "^4.1.0",
86
89
  "jest": "29.7.0",
87
- "nitro-codegen": "0.4.0",
88
- "prettier": "3.2.5",
89
- "react": "18.3.1",
90
- "react-native": "0.74.5",
91
- "react-native-builder-bob": "0.29.1",
90
+ "nitro-codegen": "0.14.0",
91
+ "prettier": "3.3.3",
92
+ "react-native-builder-bob": "0.30.0",
92
93
  "release-it": "17.6.0",
93
94
  "typescript": "5.1.6",
94
95
  "typescript-eslint": "^8.1.0"
@@ -121,5 +122,10 @@
121
122
  }
122
123
  ]
123
124
  ]
124
- }
125
+ },
126
+ "trustedDependencies": [
127
+ "react-native-quick-crypto",
128
+ "nitro-codegen",
129
+ "react-native-nitro-modules"
130
+ ]
125
131
  }
package/src/index.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  import { Buffer } from '@craftzdog/react-native-buffer';
3
3
 
4
4
  // API imports
5
+ import * as pbkdf2 from './pbkdf2';
5
6
  import * as random from './random';
6
7
 
7
8
  // utils import
8
- import { utils } from './utils';
9
+ import * as utils from './utils';
9
10
 
10
11
  /**
11
12
  * Loosely matches Node.js {crypto} with some unimplemented functionality.
@@ -34,16 +35,17 @@ const QuickCrypto = {
34
35
  // createVerify,
35
36
  // subtle,
36
37
  // constants,
37
- // ...pbkdf2,
38
+ ...pbkdf2,
38
39
  ...random,
39
40
  // getCiphers,
40
41
  // getHashes,
41
42
  // webcrypto,
42
- utils,
43
+ ...utils,
43
44
  };
44
45
 
45
46
  /**
46
- * Optional. Patch global.crypto with quickcrypto and global.Buffer with react-native-buffer.
47
+ * Optional. Patch global.crypto with react-native-quick-crypto and
48
+ * global.Buffer with react-native-buffer.
47
49
  */
48
50
  export const install = () => {
49
51
  // @ts-expect-error copyBytesFrom and poolSizets are missing from react-native-buffer
@@ -56,7 +58,9 @@ export const install = () => {
56
58
  // random, cipher, hash use nextTick
57
59
  global.process.nextTick = setImmediate;
58
60
 
61
+ // exports
59
62
  export default QuickCrypto;
63
+ export * from './utils';
60
64
 
61
65
  // Additional exports for CommonJS compatibility
62
66
  module.exports = QuickCrypto;
@@ -0,0 +1,99 @@
1
+ import type { KeyObjectHandle } from '../specs/keyObjectHandle.nitro';
2
+ import type { EncodingOptions, KeyUsage, SubtleAlgorithm } from '../utils';
3
+
4
+ export class CryptoKey {
5
+ keyObject: KeyObject;
6
+ keyAlgorithm: SubtleAlgorithm;
7
+ keyUsages: KeyUsage[];
8
+ keyExtractable: boolean;
9
+
10
+ constructor(
11
+ keyObject: KeyObject,
12
+ keyAlgorithm: SubtleAlgorithm,
13
+ keyUsages: KeyUsage[],
14
+ keyExtractable: boolean,
15
+ ) {
16
+ this.keyObject = keyObject;
17
+ this.keyAlgorithm = keyAlgorithm;
18
+ this.keyUsages = keyUsages;
19
+ this.keyExtractable = keyExtractable;
20
+ }
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
+ inspect(_depth: number, _options: unknown): unknown {
23
+ throw new Error('CryptoKey.inspect is not implemented');
24
+ // if (depth < 0) return this;
25
+
26
+ // const opts = {
27
+ // ...options,
28
+ // depth: options.depth == null ? null : options.depth - 1,
29
+ // };
30
+
31
+ // return `CryptoKey ${inspect(
32
+ // {
33
+ // type: this.type,
34
+ // extractable: this.extractable,
35
+ // algorithm: this.algorithm,
36
+ // usages: this.usages,
37
+ // },
38
+ // opts
39
+ // )}`;
40
+ }
41
+
42
+ get type() {
43
+ // if (!(this instanceof CryptoKey)) throw new Error('Invalid CryptoKey');
44
+ return this.keyObject.type;
45
+ }
46
+
47
+ get extractable() {
48
+ return this.keyExtractable;
49
+ }
50
+
51
+ get algorithm() {
52
+ return this.keyAlgorithm;
53
+ }
54
+
55
+ get usages() {
56
+ return this.keyUsages;
57
+ }
58
+ }
59
+
60
+ class KeyObject {
61
+ handle: KeyObjectHandle;
62
+ type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
63
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
64
+ export(_options?: EncodingOptions): ArrayBuffer {
65
+ return new ArrayBuffer(0);
66
+ }
67
+
68
+ constructor(type: string, handle: KeyObjectHandle) {
69
+ if (type !== 'secret' && type !== 'public' && type !== 'private')
70
+ throw new Error(`invalid KeyObject type: ${type}`);
71
+ this.handle = handle;
72
+ this.type = type;
73
+ }
74
+
75
+ // get type(): string {
76
+ // return this.type;
77
+ // }
78
+
79
+ // static from(key) {
80
+ // if (!isCryptoKey(key))
81
+ // throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
82
+ // return key[kKeyObject];
83
+ // }
84
+
85
+ // equals(otherKeyObject) {
86
+ // if (!isKeyObject(otherKeyObject)) {
87
+ // throw new ERR_INVALID_ARG_TYPE(
88
+ // 'otherKeyObject',
89
+ // 'KeyObject',
90
+ // otherKeyObject
91
+ // );
92
+ // }
93
+
94
+ // return (
95
+ // otherKeyObject.type === this.type &&
96
+ // this[kHandle].equals(otherKeyObject[kHandle])
97
+ // );
98
+ // }
99
+ }