react-native-quick-crypto 1.0.0-beta.13 → 1.0.0-beta.14

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 (88) hide show
  1. package/README.md +1 -1
  2. package/android/CMakeLists.txt +8 -4
  3. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +0 -2
  4. package/cpp/cipher/CCMCipher.cpp +199 -0
  5. package/cpp/cipher/CCMCipher.hpp +26 -0
  6. package/cpp/cipher/HybridCipher.cpp +324 -0
  7. package/cpp/cipher/HybridCipher.hpp +69 -0
  8. package/cpp/cipher/HybridCipherFactory.hpp +59 -0
  9. package/cpp/cipher/OCBCipher.cpp +55 -0
  10. package/cpp/cipher/OCBCipher.hpp +19 -0
  11. package/cpp/ed25519/HybridEdKeyPair.cpp +2 -2
  12. package/cpp/random/HybridRandom.cpp +1 -1
  13. package/lib/commonjs/cipher.js +157 -0
  14. package/lib/commonjs/cipher.js.map +1 -0
  15. package/lib/commonjs/index.js +26 -33
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/pbkdf2.js.map +1 -1
  18. package/lib/commonjs/specs/cipher.nitro.js +6 -0
  19. package/lib/commonjs/specs/cipher.nitro.js.map +1 -0
  20. package/lib/commonjs/utils/cipher.js +64 -0
  21. package/lib/commonjs/utils/cipher.js.map +1 -0
  22. package/lib/commonjs/utils/types.js +3 -1
  23. package/lib/commonjs/utils/types.js.map +1 -1
  24. package/lib/module/cipher.js +150 -0
  25. package/lib/module/cipher.js.map +1 -0
  26. package/lib/module/index.js +6 -23
  27. package/lib/module/index.js.map +1 -1
  28. package/lib/module/pbkdf2.js.map +1 -1
  29. package/lib/module/specs/cipher.nitro.js +4 -0
  30. package/lib/module/specs/cipher.nitro.js.map +1 -0
  31. package/lib/module/utils/cipher.js +56 -0
  32. package/lib/module/utils/cipher.js.map +1 -0
  33. package/lib/module/utils/types.js +4 -0
  34. package/lib/module/utils/types.js.map +1 -1
  35. package/lib/tsconfig.tsbuildinfo +1 -1
  36. package/lib/typescript/cipher.d.ts +52 -0
  37. package/lib/typescript/cipher.d.ts.map +1 -0
  38. package/lib/typescript/hash.d.ts +0 -1
  39. package/lib/typescript/hash.d.ts.map +1 -1
  40. package/lib/typescript/hmac.d.ts +0 -1
  41. package/lib/typescript/hmac.d.ts.map +1 -1
  42. package/lib/typescript/index.d.ts +15 -12
  43. package/lib/typescript/index.d.ts.map +1 -1
  44. package/lib/typescript/keys/utils.d.ts.map +1 -1
  45. package/lib/typescript/pbkdf2.d.ts +1 -1
  46. package/lib/typescript/pbkdf2.d.ts.map +1 -1
  47. package/lib/typescript/specs/cipher.nitro.d.ts +29 -0
  48. package/lib/typescript/specs/cipher.nitro.d.ts.map +1 -0
  49. package/lib/typescript/utils/cipher.d.ts +7 -0
  50. package/lib/typescript/utils/cipher.d.ts.map +1 -0
  51. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  52. package/lib/typescript/utils/types.d.ts +6 -1
  53. package/lib/typescript/utils/types.d.ts.map +1 -1
  54. package/nitrogen/generated/.gitattributes +1 -0
  55. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +18 -0
  56. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +30 -10
  57. package/nitrogen/generated/android/kotlin/com/margelo/nitro/crypto/QuickCryptoOnLoad.kt +35 -0
  58. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +2 -0
  59. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +0 -1
  60. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +30 -10
  61. package/nitrogen/generated/shared/c++/CFRGKeyPairType.hpp +1 -1
  62. package/nitrogen/generated/shared/c++/CipherArgs.hpp +88 -0
  63. package/nitrogen/generated/shared/c++/HybridCipherFactorySpec.cpp +21 -0
  64. package/nitrogen/generated/shared/c++/HybridCipherFactorySpec.hpp +67 -0
  65. package/nitrogen/generated/shared/c++/HybridCipherSpec.cpp +28 -0
  66. package/nitrogen/generated/shared/c++/HybridCipherSpec.hpp +76 -0
  67. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +1 -1
  68. package/nitrogen/generated/shared/c++/HybridHashSpec.hpp +1 -1
  69. package/nitrogen/generated/shared/c++/HybridHmacSpec.hpp +1 -1
  70. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +1 -1
  71. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +1 -1
  72. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +1 -1
  73. package/nitrogen/generated/shared/c++/JWK.hpp +2 -1
  74. package/nitrogen/generated/shared/c++/JWKkty.hpp +1 -1
  75. package/nitrogen/generated/shared/c++/JWKuse.hpp +1 -1
  76. package/nitrogen/generated/shared/c++/KFormatType.hpp +1 -1
  77. package/nitrogen/generated/shared/c++/KeyDetail.hpp +2 -1
  78. package/nitrogen/generated/shared/c++/KeyEncoding.hpp +1 -1
  79. package/nitrogen/generated/shared/c++/KeyType.hpp +1 -1
  80. package/nitrogen/generated/shared/c++/KeyUsage.hpp +1 -1
  81. package/nitrogen/generated/shared/c++/NamedCurve.hpp +1 -1
  82. package/package.json +3 -13
  83. package/src/cipher.ts +303 -0
  84. package/src/index.ts +6 -23
  85. package/src/pbkdf2.ts +1 -1
  86. package/src/specs/cipher.nitro.ts +25 -0
  87. package/src/utils/cipher.ts +60 -0
  88. package/src/utils/types.ts +27 -0
@@ -0,0 +1,28 @@
1
+ ///
2
+ /// HybridCipherSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridCipherSpec.hpp"
9
+
10
+ namespace margelo::nitro::crypto {
11
+
12
+ void HybridCipherSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("update", &HybridCipherSpec::update);
18
+ prototype.registerHybridMethod("final", &HybridCipherSpec::final);
19
+ prototype.registerHybridMethod("setArgs", &HybridCipherSpec::setArgs);
20
+ prototype.registerHybridMethod("setAAD", &HybridCipherSpec::setAAD);
21
+ prototype.registerHybridMethod("setAutoPadding", &HybridCipherSpec::setAutoPadding);
22
+ prototype.registerHybridMethod("setAuthTag", &HybridCipherSpec::setAuthTag);
23
+ prototype.registerHybridMethod("getAuthTag", &HybridCipherSpec::getAuthTag);
24
+ prototype.registerHybridMethod("getSupportedCiphers", &HybridCipherSpec::getSupportedCiphers);
25
+ });
26
+ }
27
+
28
+ } // namespace margelo::nitro::crypto
@@ -0,0 +1,76 @@
1
+ ///
2
+ /// HybridCipherSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+ // Forward declaration of `ArrayBuffer` to properly resolve imports.
17
+ namespace NitroModules { class ArrayBuffer; }
18
+ // Forward declaration of `CipherArgs` to properly resolve imports.
19
+ namespace margelo::nitro::crypto { struct CipherArgs; }
20
+
21
+ #include <NitroModules/ArrayBuffer.hpp>
22
+ #include "CipherArgs.hpp"
23
+ #include <optional>
24
+ #include <vector>
25
+ #include <string>
26
+
27
+ namespace margelo::nitro::crypto {
28
+
29
+ using namespace margelo::nitro;
30
+
31
+ /**
32
+ * An abstract base class for `Cipher`
33
+ * Inherit this class to create instances of `HybridCipherSpec` in C++.
34
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
35
+ * @example
36
+ * ```cpp
37
+ * class HybridCipher: public HybridCipherSpec {
38
+ * public:
39
+ * HybridCipher(...): HybridObject(TAG) { ... }
40
+ * // ...
41
+ * };
42
+ * ```
43
+ */
44
+ class HybridCipherSpec: public virtual HybridObject {
45
+ public:
46
+ // Constructor
47
+ explicit HybridCipherSpec(): HybridObject(TAG) { }
48
+
49
+ // Destructor
50
+ ~HybridCipherSpec() override = default;
51
+
52
+ public:
53
+ // Properties
54
+
55
+
56
+ public:
57
+ // Methods
58
+ virtual std::shared_ptr<ArrayBuffer> update(const std::shared_ptr<ArrayBuffer>& data) = 0;
59
+ virtual std::shared_ptr<ArrayBuffer> final() = 0;
60
+ virtual void setArgs(const CipherArgs& args) = 0;
61
+ virtual bool setAAD(const std::shared_ptr<ArrayBuffer>& data, std::optional<double> plaintextLength) = 0;
62
+ virtual bool setAutoPadding(bool autoPad) = 0;
63
+ virtual bool setAuthTag(const std::shared_ptr<ArrayBuffer>& tag) = 0;
64
+ virtual std::shared_ptr<ArrayBuffer> getAuthTag() = 0;
65
+ virtual std::vector<std::string> getSupportedCiphers() = 0;
66
+
67
+ protected:
68
+ // Hybrid Setup
69
+ void loadHybridMethods() override;
70
+
71
+ protected:
72
+ // Tag for logging
73
+ static constexpr auto TAG = "Cipher";
74
+ };
75
+
76
+ } // namespace margelo::nitro::crypto
@@ -44,7 +44,7 @@ namespace margelo::nitro::crypto {
44
44
  explicit HybridEdKeyPairSpec(): HybridObject(TAG) { }
45
45
 
46
46
  // Destructor
47
- virtual ~HybridEdKeyPairSpec() { }
47
+ ~HybridEdKeyPairSpec() override = default;
48
48
 
49
49
  public:
50
50
  // Properties
@@ -48,7 +48,7 @@ namespace margelo::nitro::crypto {
48
48
  explicit HybridHashSpec(): HybridObject(TAG) { }
49
49
 
50
50
  // Destructor
51
- virtual ~HybridHashSpec() { }
51
+ ~HybridHashSpec() override = default;
52
52
 
53
53
  public:
54
54
  // Properties
@@ -42,7 +42,7 @@ namespace margelo::nitro::crypto {
42
42
  explicit HybridHmacSpec(): HybridObject(TAG) { }
43
43
 
44
44
  // Destructor
45
- virtual ~HybridHmacSpec() { }
45
+ ~HybridHmacSpec() override = default;
46
46
 
47
47
  public:
48
48
  // Properties
@@ -65,7 +65,7 @@ namespace margelo::nitro::crypto {
65
65
  explicit HybridKeyObjectHandleSpec(): HybridObject(TAG) { }
66
66
 
67
67
  // Destructor
68
- virtual ~HybridKeyObjectHandleSpec() { }
68
+ ~HybridKeyObjectHandleSpec() override = default;
69
69
 
70
70
  public:
71
71
  // Properties
@@ -43,7 +43,7 @@ namespace margelo::nitro::crypto {
43
43
  explicit HybridPbkdf2Spec(): HybridObject(TAG) { }
44
44
 
45
45
  // Destructor
46
- virtual ~HybridPbkdf2Spec() { }
46
+ ~HybridPbkdf2Spec() override = default;
47
47
 
48
48
  public:
49
49
  // Properties
@@ -42,7 +42,7 @@ namespace margelo::nitro::crypto {
42
42
  explicit HybridRandomSpec(): HybridObject(TAG) { }
43
43
 
44
44
  // Destructor
45
- virtual ~HybridRandomSpec() { }
45
+ ~HybridRandomSpec() override = default;
46
46
 
47
47
  public:
48
48
  // Properties
@@ -63,6 +63,7 @@ namespace margelo::nitro::crypto {
63
63
  std::optional<bool> ext SWIFT_PRIVATE;
64
64
 
65
65
  public:
66
+ JWK() = default;
66
67
  explicit JWK(std::optional<JWKkty> kty, std::optional<JWKuse> use, std::optional<std::vector<KeyUsage>> key_ops, std::optional<std::string> alg, std::optional<std::string> crv, std::optional<std::string> kid, std::optional<std::string> x5u, std::optional<std::vector<std::string>> x5c, std::optional<std::string> x5t, std::optional<std::string> x5t_256, std::optional<std::string> n, std::optional<std::string> e, std::optional<std::string> d, std::optional<std::string> p, std::optional<std::string> q, std::optional<std::string> x, std::optional<std::string> y, std::optional<std::string> k, std::optional<std::string> dp, std::optional<std::string> dq, std::optional<std::string> qi, std::optional<bool> ext): kty(kty), use(use), key_ops(key_ops), alg(alg), crv(crv), kid(kid), x5u(x5u), x5c(x5c), x5t(x5t), x5t_256(x5t_256), n(n), e(e), d(d), p(p), q(q), x(x), y(y), k(k), dp(dp), dq(dq), qi(qi), ext(ext) {}
67
68
  };
68
69
 
@@ -74,7 +75,7 @@ namespace margelo::nitro {
74
75
 
75
76
  // C++ JWK <> JS JWK (object)
76
77
  template <>
77
- struct JSIConverter<JWK> {
78
+ struct JSIConverter<JWK> final {
78
79
  static inline JWK fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
79
80
  jsi::Object obj = arg.asObject(runtime);
80
81
  return JWK(
@@ -43,7 +43,7 @@ namespace margelo::nitro {
43
43
 
44
44
  // C++ JWKkty <> JS JWKkty (union)
45
45
  template <>
46
- struct JSIConverter<JWKkty> {
46
+ struct JSIConverter<JWKkty> final {
47
47
  static inline JWKkty fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
48
48
  std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
49
49
  switch (hashString(unionValue.c_str(), unionValue.size())) {
@@ -41,7 +41,7 @@ namespace margelo::nitro {
41
41
 
42
42
  // C++ JWKuse <> JS JWKuse (union)
43
43
  template <>
44
- struct JSIConverter<JWKuse> {
44
+ struct JSIConverter<JWKuse> final {
45
45
  static inline JWKuse fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
46
46
  std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
47
47
  switch (hashString(unionValue.c_str(), unionValue.size())) {
@@ -38,7 +38,7 @@ namespace margelo::nitro {
38
38
 
39
39
  // C++ KFormatType <> JS KFormatType (enum)
40
40
  template <>
41
- struct JSIConverter<KFormatType> {
41
+ struct JSIConverter<KFormatType> final {
42
42
  static inline KFormatType fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
43
43
  int enumValue = JSIConverter<int>::fromJSI(runtime, arg);
44
44
  return static_cast<KFormatType>(enumValue);
@@ -39,6 +39,7 @@ namespace margelo::nitro::crypto {
39
39
  std::optional<std::string> namedCurve SWIFT_PRIVATE;
40
40
 
41
41
  public:
42
+ KeyDetail() = default;
42
43
  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
  };
44
45
 
@@ -50,7 +51,7 @@ namespace margelo::nitro {
50
51
 
51
52
  // C++ KeyDetail <> JS KeyDetail (object)
52
53
  template <>
53
- struct JSIConverter<KeyDetail> {
54
+ struct JSIConverter<KeyDetail> final {
54
55
  static inline KeyDetail fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
55
56
  jsi::Object obj = arg.asObject(runtime);
56
57
  return KeyDetail(
@@ -39,7 +39,7 @@ namespace margelo::nitro {
39
39
 
40
40
  // C++ KeyEncoding <> JS KeyEncoding (enum)
41
41
  template <>
42
- struct JSIConverter<KeyEncoding> {
42
+ struct JSIConverter<KeyEncoding> final {
43
43
  static inline KeyEncoding fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
44
44
  int enumValue = JSIConverter<int>::fromJSI(runtime, arg);
45
45
  return static_cast<KeyEncoding>(enumValue);
@@ -38,7 +38,7 @@ namespace margelo::nitro {
38
38
 
39
39
  // C++ KeyType <> JS KeyType (enum)
40
40
  template <>
41
- struct JSIConverter<KeyType> {
41
+ struct JSIConverter<KeyType> final {
42
42
  static inline KeyType fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
43
43
  int enumValue = JSIConverter<int>::fromJSI(runtime, arg);
44
44
  return static_cast<KeyType>(enumValue);
@@ -47,7 +47,7 @@ namespace margelo::nitro {
47
47
 
48
48
  // C++ KeyUsage <> JS KeyUsage (union)
49
49
  template <>
50
- struct JSIConverter<KeyUsage> {
50
+ struct JSIConverter<KeyUsage> final {
51
51
  static inline KeyUsage fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
52
52
  std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
53
53
  switch (hashString(unionValue.c_str(), unionValue.size())) {
@@ -42,7 +42,7 @@ namespace margelo::nitro {
42
42
 
43
43
  // C++ NamedCurve <> JS NamedCurve (union)
44
44
  template <>
45
- struct JSIConverter<NamedCurve> {
45
+ struct JSIConverter<NamedCurve> final {
46
46
  static inline NamedCurve fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
47
47
  std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
48
48
  switch (hashString(unionValue.c_str(), unionValue.size())) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "1.0.0-beta.13",
3
+ "version": "1.0.0-beta.14",
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",
@@ -72,28 +72,18 @@
72
72
  "events": "3.3.0",
73
73
  "react-native-quick-base64": "2.1.2",
74
74
  "readable-stream": "4.5.2",
75
- "string_decoder": "1.3.0",
76
75
  "util": "0.12.5"
77
76
  },
78
77
  "devDependencies": {
79
- "@eslint/compat": "^1.1.1",
80
- "@eslint/js": "9.18.0",
81
- "@release-it/bumper": "7.0.1",
82
78
  "@types/jest": "29.5.11",
83
79
  "@types/node": "22.0.0",
84
80
  "@types/react": "18.3.3",
85
81
  "@types/readable-stream": "4.0.18",
86
82
  "del-cli": "6.0.0",
87
- "eslint": "9.9.0",
88
- "eslint-plugin-react-native": "5.0.0",
89
83
  "jest": "29.7.0",
90
- "nitro-codegen": "0.21.0",
91
- "prettier": "3.3.3",
84
+ "nitro-codegen": "0.25.2",
92
85
  "react-native-builder-bob": "0.35.2",
93
- "react-native-nitro-modules": "0.21.0",
94
- "release-it": "18.1.1",
95
- "typescript": "5.1.6",
96
- "typescript-eslint": "^8.1.0"
86
+ "react-native-nitro-modules": "0.25.2"
97
87
  },
98
88
  "peerDependencies": {
99
89
  "react": "*",
package/src/cipher.ts ADDED
@@ -0,0 +1,303 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import Stream, { type TransformOptions } from 'readable-stream';
3
+ import { Buffer } from '@craftzdog/react-native-buffer';
4
+ import type { BinaryLike, BinaryLikeNode, Encoding } from './utils';
5
+ import type {
6
+ CipherCCMOptions,
7
+ CipherCCMTypes,
8
+ CipherGCMTypes,
9
+ CipherGCMOptions,
10
+ CipherOCBOptions,
11
+ CipherOCBTypes,
12
+ } from 'crypto'; // @types/node
13
+ import type {
14
+ Cipher as NativeCipher,
15
+ CipherFactory,
16
+ } from './specs/cipher.nitro';
17
+ import { ab2str, binaryLikeToArrayBuffer } from './utils';
18
+ import {
19
+ getDefaultEncoding,
20
+ getUIntOption,
21
+ normalizeEncoding,
22
+ validateEncoding,
23
+ } from './utils/cipher';
24
+
25
+ export type CipherOptions =
26
+ | CipherCCMOptions
27
+ | CipherOCBOptions
28
+ | CipherGCMOptions
29
+ | TransformOptions;
30
+
31
+ class CipherUtils {
32
+ private static native =
33
+ NitroModules.createHybridObject<NativeCipher>('Cipher');
34
+ public static getSupportedCiphers(): string[] {
35
+ return this.native.getSupportedCiphers();
36
+ }
37
+ }
38
+
39
+ export function getCiphers(): string[] {
40
+ return CipherUtils.getSupportedCiphers();
41
+ }
42
+
43
+ interface CipherArgs {
44
+ isCipher: boolean;
45
+ cipherType: string;
46
+ cipherKey: BinaryLikeNode;
47
+ iv: BinaryLike;
48
+ options?: CipherOptions;
49
+ }
50
+
51
+ class CipherCommon extends Stream.Transform {
52
+ private native: NativeCipher;
53
+
54
+ constructor({ isCipher, cipherType, cipherKey, iv, options }: CipherArgs) {
55
+ // Explicitly create TransformOptions for super()
56
+ const streamOptions: TransformOptions = {};
57
+ if (options) {
58
+ // List known TransformOptions keys (adjust if needed)
59
+ const transformKeys: Array<keyof TransformOptions> = [
60
+ 'readableHighWaterMark',
61
+ 'writableHighWaterMark',
62
+ 'decodeStrings',
63
+ 'defaultEncoding',
64
+ 'objectMode',
65
+ 'destroy',
66
+ 'read',
67
+ 'write',
68
+ 'writev',
69
+ 'final',
70
+ 'transform',
71
+ 'flush',
72
+ // Add any other relevant keys from readable-stream's TransformOptions
73
+ ];
74
+ for (const key of transformKeys) {
75
+ if (key in options) {
76
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
+ (streamOptions as any)[key] = (options as any)[key];
78
+ }
79
+ }
80
+ }
81
+ super(streamOptions); // Pass filtered options
82
+
83
+ const authTagLen: number =
84
+ getUIntOption(options ?? {}, 'authTagLength') !== -1
85
+ ? getUIntOption(options ?? {}, 'authTagLength')
86
+ : 16; // defaults to 16 bytes
87
+
88
+ const factory =
89
+ NitroModules.createHybridObject<CipherFactory>('CipherFactory');
90
+ this.native = factory.createCipher({
91
+ isCipher,
92
+ cipherType,
93
+ cipherKey: binaryLikeToArrayBuffer(cipherKey),
94
+ iv: binaryLikeToArrayBuffer(iv),
95
+ authTagLen,
96
+ });
97
+ }
98
+
99
+ update(
100
+ data: BinaryLike,
101
+ inputEncoding?: Encoding,
102
+ outputEncoding?: Encoding,
103
+ ): Buffer | string {
104
+ const defaultEncoding = getDefaultEncoding();
105
+ inputEncoding = inputEncoding ?? defaultEncoding;
106
+ outputEncoding = outputEncoding ?? defaultEncoding;
107
+
108
+ if (typeof data === 'string') {
109
+ validateEncoding(data, inputEncoding);
110
+ } else if (!ArrayBuffer.isView(data)) {
111
+ throw new Error('Invalid data argument');
112
+ }
113
+
114
+ const ret = this.native.update(
115
+ binaryLikeToArrayBuffer(data, inputEncoding),
116
+ );
117
+
118
+ if (outputEncoding && outputEncoding !== 'buffer') {
119
+ return ab2str(ret, outputEncoding);
120
+ }
121
+
122
+ return Buffer.from(ret);
123
+ }
124
+
125
+ final(): Buffer;
126
+ final(outputEncoding: BufferEncoding | 'buffer'): string;
127
+ final(outputEncoding?: BufferEncoding | 'buffer'): Buffer | string {
128
+ const ret = this.native.final();
129
+
130
+ if (outputEncoding && outputEncoding !== 'buffer') {
131
+ return ab2str(ret, outputEncoding);
132
+ }
133
+
134
+ return Buffer.from(ret);
135
+ }
136
+
137
+ _transform(
138
+ chunk: BinaryLike,
139
+ encoding: BufferEncoding,
140
+ callback: () => void,
141
+ ) {
142
+ this.push(this.update(chunk, normalizeEncoding(encoding)));
143
+ callback();
144
+ }
145
+
146
+ _flush(callback: () => void) {
147
+ this.push(this.final());
148
+ callback();
149
+ }
150
+
151
+ public setAutoPadding(autoPadding?: boolean): this {
152
+ const res = this.native.setAutoPadding(!!autoPadding);
153
+ if (!res) {
154
+ throw new Error('setAutoPadding failed');
155
+ }
156
+ return this;
157
+ }
158
+
159
+ public setAAD(
160
+ buffer: Buffer,
161
+ options?: {
162
+ plaintextLength: number;
163
+ },
164
+ ): this {
165
+ // Check if native parts are initialized
166
+ if (!this.native || typeof this.native.setAAD !== 'function') {
167
+ throw new Error('Cipher native object or setAAD method not initialized.');
168
+ }
169
+ const res = this.native.setAAD(buffer.buffer, options?.plaintextLength);
170
+ if (!res) {
171
+ throw new Error('setAAD failed (native call returned false)');
172
+ }
173
+ return this;
174
+ }
175
+
176
+ public getAuthTag(): Buffer {
177
+ return Buffer.from(this.native.getAuthTag());
178
+ }
179
+
180
+ public setAuthTag(tag: Buffer): this {
181
+ const res = this.native.setAuthTag(binaryLikeToArrayBuffer(tag));
182
+ if (!res) {
183
+ throw new Error('setAuthTag failed');
184
+ }
185
+ return this;
186
+ }
187
+
188
+ public getSupportedCiphers(): string[] {
189
+ return this.native.getSupportedCiphers();
190
+ }
191
+ }
192
+
193
+ class Cipheriv extends CipherCommon {
194
+ constructor(
195
+ cipherType: string,
196
+ cipherKey: BinaryLikeNode,
197
+ iv: BinaryLike,
198
+ options?: CipherOptions,
199
+ ) {
200
+ super({
201
+ isCipher: true,
202
+ cipherType,
203
+ cipherKey: binaryLikeToArrayBuffer(cipherKey),
204
+ iv: binaryLikeToArrayBuffer(iv),
205
+ options,
206
+ });
207
+ }
208
+ }
209
+
210
+ type Cipher = Cipheriv;
211
+
212
+ class Decipheriv extends CipherCommon {
213
+ constructor(
214
+ cipherType: string,
215
+ cipherKey: BinaryLikeNode,
216
+ iv: BinaryLike,
217
+ options?: CipherOptions,
218
+ ) {
219
+ super({
220
+ isCipher: false,
221
+ cipherType,
222
+ cipherKey: binaryLikeToArrayBuffer(cipherKey),
223
+ iv: binaryLikeToArrayBuffer(iv),
224
+ options,
225
+ });
226
+ }
227
+ }
228
+
229
+ type Decipher = Decipheriv;
230
+
231
+ export function createDecipheriv(
232
+ algorithm: CipherCCMTypes,
233
+ key: BinaryLikeNode,
234
+ iv: BinaryLike,
235
+ options: CipherCCMOptions,
236
+ ): Decipher;
237
+ export function createDecipheriv(
238
+ algorithm: CipherOCBTypes,
239
+ key: BinaryLikeNode,
240
+ iv: BinaryLike,
241
+ options: CipherOCBOptions,
242
+ ): Decipher;
243
+ export function createDecipheriv(
244
+ algorithm: CipherGCMTypes,
245
+ key: BinaryLikeNode,
246
+ iv: BinaryLike,
247
+ options?: CipherGCMOptions,
248
+ ): Decipher;
249
+ export function createDecipheriv(
250
+ algorithm: string,
251
+ key: BinaryLikeNode,
252
+ iv: BinaryLike,
253
+ options?: TransformOptions,
254
+ ): Decipher;
255
+ export function createDecipheriv(
256
+ algorithm: string,
257
+ key: BinaryLikeNode,
258
+ iv: BinaryLike,
259
+ options?: CipherOptions,
260
+ ): Decipher {
261
+ return new Decipheriv(algorithm, key, iv, options);
262
+ }
263
+
264
+ export function createCipheriv(
265
+ algorithm: CipherCCMTypes,
266
+ key: BinaryLikeNode,
267
+ iv: BinaryLike,
268
+ options: CipherCCMOptions,
269
+ ): Cipher;
270
+ export function createCipheriv(
271
+ algorithm: CipherOCBTypes,
272
+ key: BinaryLikeNode,
273
+ iv: BinaryLike,
274
+ options: CipherOCBOptions,
275
+ ): Cipher;
276
+ export function createCipheriv(
277
+ algorithm: CipherGCMTypes,
278
+ key: BinaryLikeNode,
279
+ iv: BinaryLike,
280
+ options?: CipherGCMOptions,
281
+ ): Cipher;
282
+ export function createCipheriv(
283
+ algorithm: string,
284
+ key: BinaryLikeNode,
285
+ iv: BinaryLike,
286
+ options?: TransformOptions,
287
+ ): Cipher;
288
+ export function createCipheriv(
289
+ algorithm: string,
290
+ key: BinaryLikeNode,
291
+ iv: BinaryLike,
292
+ options?: CipherOptions,
293
+ ): Cipher {
294
+ return new Cipheriv(algorithm, key, iv, options);
295
+ }
296
+
297
+ export const cipherExports = {
298
+ createCipheriv,
299
+ createDecipheriv,
300
+ getCiphers,
301
+ };
302
+
303
+ export type { Cipher, Decipher };
package/src/index.ts CHANGED
@@ -3,9 +3,10 @@ import { Buffer } from '@craftzdog/react-native-buffer';
3
3
 
4
4
  // API imports
5
5
  import * as keys from './keys';
6
+ import { cipherExports as cipher } from './cipher';
7
+ import * as ed from './ed';
6
8
  import { hashExports as hash } from './hash';
7
9
  import { hmacExports as hmac } from './hmac';
8
- import * as ed from './ed';
9
10
  import * as pbkdf2 from './pbkdf2';
10
11
  import * as random from './random';
11
12
 
@@ -17,32 +18,13 @@ import * as utils from './utils';
17
18
  * See `docs/implementation-coverage.md` for status.
18
19
  */
19
20
  const QuickCrypto = {
20
- // createHmac,
21
- // Hmac: createHmac,
22
- // Hash: createHash,
23
- // createHash,
24
- // createCipher,
25
- // createCipheriv,
26
- // createDecipher,
27
- // createDecipheriv,
28
- // publicEncrypt,
29
- // publicDecrypt,
30
- // privateDecrypt,
31
- // generateKey,
32
- // generateKeySync,
33
- // createSign,
34
- // createVerify,
35
- // subtle,
36
- // constants,
37
21
  ...keys,
22
+ ...cipher,
23
+ ...ed,
38
24
  ...hash,
39
25
  ...hmac,
40
- ...ed,
41
26
  ...pbkdf2,
42
27
  ...random,
43
- // getCiphers,
44
- // getHashes,
45
- // webcrypto,
46
28
  ...utils,
47
29
  };
48
30
 
@@ -63,9 +45,10 @@ global.process.nextTick = setImmediate;
63
45
 
64
46
  // exports
65
47
  export default QuickCrypto;
48
+ export * from './cipher';
49
+ export * from './ed';
66
50
  export * from './hash';
67
51
  export * from './hmac';
68
- export * from './ed';
69
52
  export * from './pbkdf2';
70
53
  export * from './random';
71
54
  export * from './utils';
package/src/pbkdf2.ts CHANGED
@@ -80,7 +80,7 @@ export function pbkdf2Sync(
80
80
  iterations: number,
81
81
  keylen: number,
82
82
  digest?: HashAlgorithm,
83
- ): ArrayBuffer {
83
+ ): Buffer {
84
84
  const sanitizedPassword = sanitizeInput(password, WRONG_PASS);
85
85
  const sanitizedSalt = sanitizeInput(salt, WRONG_SALT);
86
86