react-native-quick-crypto 1.0.10 → 1.0.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 (158) hide show
  1. package/android/CMakeLists.txt +9 -0
  2. package/cpp/argon2/HybridArgon2.cpp +103 -0
  3. package/cpp/argon2/HybridArgon2.hpp +32 -0
  4. package/cpp/certificate/HybridCertificate.cpp +42 -0
  5. package/cpp/certificate/HybridCertificate.hpp +16 -0
  6. package/cpp/cipher/HybridCipher.cpp +58 -0
  7. package/cpp/cipher/HybridCipher.hpp +4 -0
  8. package/cpp/dh/HybridDhKeyPair.cpp +179 -0
  9. package/cpp/dh/HybridDhKeyPair.hpp +37 -0
  10. package/cpp/dsa/HybridDsaKeyPair.cpp +128 -0
  11. package/cpp/dsa/HybridDsaKeyPair.hpp +32 -0
  12. package/cpp/ecdh/HybridECDH.cpp +35 -0
  13. package/cpp/ecdh/HybridECDH.hpp +1 -0
  14. package/cpp/keys/HybridKeyObjectHandle.cpp +19 -0
  15. package/cpp/prime/HybridPrime.cpp +81 -0
  16. package/cpp/prime/HybridPrime.hpp +20 -0
  17. package/lib/commonjs/argon2.js +39 -0
  18. package/lib/commonjs/argon2.js.map +1 -0
  19. package/lib/commonjs/certificate.js +35 -0
  20. package/lib/commonjs/certificate.js.map +1 -0
  21. package/lib/commonjs/cipher.js +8 -0
  22. package/lib/commonjs/cipher.js.map +1 -1
  23. package/lib/commonjs/dhKeyPair.js +109 -0
  24. package/lib/commonjs/dhKeyPair.js.map +1 -0
  25. package/lib/commonjs/dsa.js +92 -0
  26. package/lib/commonjs/dsa.js.map +1 -0
  27. package/lib/commonjs/ecdh.js +37 -0
  28. package/lib/commonjs/ecdh.js.map +1 -1
  29. package/lib/commonjs/index.js +35 -0
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/keys/classes.js +9 -7
  32. package/lib/commonjs/keys/classes.js.map +1 -1
  33. package/lib/commonjs/keys/generateKeyPair.js +11 -0
  34. package/lib/commonjs/keys/generateKeyPair.js.map +1 -1
  35. package/lib/commonjs/prime.js +84 -0
  36. package/lib/commonjs/prime.js.map +1 -0
  37. package/lib/commonjs/specs/argon2.nitro.js +6 -0
  38. package/lib/commonjs/specs/argon2.nitro.js.map +1 -0
  39. package/lib/commonjs/specs/certificate.nitro.js +6 -0
  40. package/lib/commonjs/specs/certificate.nitro.js.map +1 -0
  41. package/lib/commonjs/specs/dhKeyPair.nitro.js +6 -0
  42. package/lib/commonjs/specs/dhKeyPair.nitro.js.map +1 -0
  43. package/lib/commonjs/specs/dsaKeyPair.nitro.js +6 -0
  44. package/lib/commonjs/specs/dsaKeyPair.nitro.js.map +1 -0
  45. package/lib/commonjs/specs/prime.nitro.js +6 -0
  46. package/lib/commonjs/specs/prime.nitro.js.map +1 -0
  47. package/lib/commonjs/subtle.js +101 -10
  48. package/lib/commonjs/subtle.js.map +1 -1
  49. package/lib/commonjs/utils/types.js.map +1 -1
  50. package/lib/module/argon2.js +34 -0
  51. package/lib/module/argon2.js.map +1 -0
  52. package/lib/module/certificate.js +30 -0
  53. package/lib/module/certificate.js.map +1 -0
  54. package/lib/module/cipher.js +7 -0
  55. package/lib/module/cipher.js.map +1 -1
  56. package/lib/module/dhKeyPair.js +102 -0
  57. package/lib/module/dhKeyPair.js.map +1 -0
  58. package/lib/module/dsa.js +85 -0
  59. package/lib/module/dsa.js.map +1 -0
  60. package/lib/module/ecdh.js +37 -0
  61. package/lib/module/ecdh.js.map +1 -1
  62. package/lib/module/index.js +9 -0
  63. package/lib/module/index.js.map +1 -1
  64. package/lib/module/keys/classes.js +9 -7
  65. package/lib/module/keys/classes.js.map +1 -1
  66. package/lib/module/keys/generateKeyPair.js +11 -0
  67. package/lib/module/keys/generateKeyPair.js.map +1 -1
  68. package/lib/module/prime.js +77 -0
  69. package/lib/module/prime.js.map +1 -0
  70. package/lib/module/specs/argon2.nitro.js +4 -0
  71. package/lib/module/specs/argon2.nitro.js.map +1 -0
  72. package/lib/module/specs/certificate.nitro.js +4 -0
  73. package/lib/module/specs/certificate.nitro.js.map +1 -0
  74. package/lib/module/specs/dhKeyPair.nitro.js +4 -0
  75. package/lib/module/specs/dhKeyPair.nitro.js.map +1 -0
  76. package/lib/module/specs/dsaKeyPair.nitro.js +4 -0
  77. package/lib/module/specs/dsaKeyPair.nitro.js.map +1 -0
  78. package/lib/module/specs/prime.nitro.js +4 -0
  79. package/lib/module/specs/prime.nitro.js.map +1 -0
  80. package/lib/module/subtle.js +102 -12
  81. package/lib/module/subtle.js.map +1 -1
  82. package/lib/module/utils/types.js.map +1 -1
  83. package/lib/tsconfig.tsbuildinfo +1 -1
  84. package/lib/typescript/argon2.d.ts +16 -0
  85. package/lib/typescript/argon2.d.ts.map +1 -0
  86. package/lib/typescript/certificate.d.ts +8 -0
  87. package/lib/typescript/certificate.d.ts.map +1 -0
  88. package/lib/typescript/cipher.d.ts +12 -0
  89. package/lib/typescript/cipher.d.ts.map +1 -1
  90. package/lib/typescript/dhKeyPair.d.ts +19 -0
  91. package/lib/typescript/dhKeyPair.d.ts.map +1 -0
  92. package/lib/typescript/dsa.d.ts +19 -0
  93. package/lib/typescript/dsa.d.ts.map +1 -0
  94. package/lib/typescript/ecdh.d.ts +3 -0
  95. package/lib/typescript/ecdh.d.ts.map +1 -1
  96. package/lib/typescript/index.d.ts +17 -0
  97. package/lib/typescript/index.d.ts.map +1 -1
  98. package/lib/typescript/keys/classes.d.ts +2 -0
  99. package/lib/typescript/keys/classes.d.ts.map +1 -1
  100. package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -1
  101. package/lib/typescript/prime.d.ts +19 -0
  102. package/lib/typescript/prime.d.ts.map +1 -0
  103. package/lib/typescript/specs/argon2.nitro.d.ts +9 -0
  104. package/lib/typescript/specs/argon2.nitro.d.ts.map +1 -0
  105. package/lib/typescript/specs/certificate.nitro.d.ts +10 -0
  106. package/lib/typescript/specs/certificate.nitro.d.ts.map +1 -0
  107. package/lib/typescript/specs/cipher.nitro.d.ts +9 -0
  108. package/lib/typescript/specs/cipher.nitro.d.ts.map +1 -1
  109. package/lib/typescript/specs/dhKeyPair.nitro.d.ts +14 -0
  110. package/lib/typescript/specs/dhKeyPair.nitro.d.ts.map +1 -0
  111. package/lib/typescript/specs/dsaKeyPair.nitro.d.ts +13 -0
  112. package/lib/typescript/specs/dsaKeyPair.nitro.d.ts.map +1 -0
  113. package/lib/typescript/specs/ecdh.nitro.d.ts +1 -0
  114. package/lib/typescript/specs/ecdh.nitro.d.ts.map +1 -1
  115. package/lib/typescript/specs/prime.nitro.d.ts +11 -0
  116. package/lib/typescript/specs/prime.nitro.d.ts.map +1 -0
  117. package/lib/typescript/subtle.d.ts +2 -0
  118. package/lib/typescript/subtle.d.ts.map +1 -1
  119. package/lib/typescript/utils/types.d.ts +13 -3
  120. package/lib/typescript/utils/types.d.ts.map +1 -1
  121. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +5 -0
  122. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +50 -0
  123. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +50 -0
  124. package/nitrogen/generated/shared/c++/CipherInfo.hpp +104 -0
  125. package/nitrogen/generated/shared/c++/HybridArgon2Spec.cpp +22 -0
  126. package/nitrogen/generated/shared/c++/HybridArgon2Spec.hpp +66 -0
  127. package/nitrogen/generated/shared/c++/HybridCertificateSpec.cpp +23 -0
  128. package/nitrogen/generated/shared/c++/HybridCertificateSpec.hpp +64 -0
  129. package/nitrogen/generated/shared/c++/HybridCipherSpec.cpp +1 -0
  130. package/nitrogen/generated/shared/c++/HybridCipherSpec.hpp +4 -0
  131. package/nitrogen/generated/shared/c++/HybridDhKeyPairSpec.cpp +27 -0
  132. package/nitrogen/generated/shared/c++/HybridDhKeyPairSpec.hpp +69 -0
  133. package/nitrogen/generated/shared/c++/HybridDsaKeyPairSpec.cpp +26 -0
  134. package/nitrogen/generated/shared/c++/HybridDsaKeyPairSpec.hpp +68 -0
  135. package/nitrogen/generated/shared/c++/HybridECDHSpec.cpp +1 -0
  136. package/nitrogen/generated/shared/c++/HybridECDHSpec.hpp +1 -0
  137. package/nitrogen/generated/shared/c++/HybridPrimeSpec.cpp +24 -0
  138. package/nitrogen/generated/shared/c++/HybridPrimeSpec.hpp +67 -0
  139. package/package.json +1 -1
  140. package/src/argon2.ts +83 -0
  141. package/src/certificate.ts +41 -0
  142. package/src/cipher.ts +24 -0
  143. package/src/dhKeyPair.ts +156 -0
  144. package/src/dsa.ts +129 -0
  145. package/src/ecdh.ts +59 -0
  146. package/src/index.ts +9 -0
  147. package/src/keys/classes.ts +16 -5
  148. package/src/keys/generateKeyPair.ts +14 -0
  149. package/src/prime.ts +134 -0
  150. package/src/specs/argon2.nitro.ts +29 -0
  151. package/src/specs/certificate.nitro.ts +8 -0
  152. package/src/specs/cipher.nitro.ts +14 -0
  153. package/src/specs/dhKeyPair.nitro.ts +14 -0
  154. package/src/specs/dsaKeyPair.nitro.ts +13 -0
  155. package/src/specs/ecdh.nitro.ts +1 -0
  156. package/src/specs/prime.nitro.ts +18 -0
  157. package/src/subtle.ts +271 -12
  158. package/src/utils/types.ts +22 -3
@@ -0,0 +1,68 @@
1
+ ///
2
+ /// HybridDsaKeyPairSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 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
+
17
+
18
+ #include <NitroModules/Promise.hpp>
19
+ #include <NitroModules/ArrayBuffer.hpp>
20
+
21
+ namespace margelo::nitro::crypto {
22
+
23
+ using namespace margelo::nitro;
24
+
25
+ /**
26
+ * An abstract base class for `DsaKeyPair`
27
+ * Inherit this class to create instances of `HybridDsaKeyPairSpec` in C++.
28
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
29
+ * @example
30
+ * ```cpp
31
+ * class HybridDsaKeyPair: public HybridDsaKeyPairSpec {
32
+ * public:
33
+ * HybridDsaKeyPair(...): HybridObject(TAG) { ... }
34
+ * // ...
35
+ * };
36
+ * ```
37
+ */
38
+ class HybridDsaKeyPairSpec: public virtual HybridObject {
39
+ public:
40
+ // Constructor
41
+ explicit HybridDsaKeyPairSpec(): HybridObject(TAG) { }
42
+
43
+ // Destructor
44
+ ~HybridDsaKeyPairSpec() override = default;
45
+
46
+ public:
47
+ // Properties
48
+
49
+
50
+ public:
51
+ // Methods
52
+ virtual std::shared_ptr<Promise<void>> generateKeyPair() = 0;
53
+ virtual void generateKeyPairSync() = 0;
54
+ virtual void setModulusLength(double modulusLength) = 0;
55
+ virtual void setDivisorLength(double divisorLength) = 0;
56
+ virtual std::shared_ptr<ArrayBuffer> getPublicKey() = 0;
57
+ virtual std::shared_ptr<ArrayBuffer> getPrivateKey() = 0;
58
+
59
+ protected:
60
+ // Hybrid Setup
61
+ void loadHybridMethods() override;
62
+
63
+ protected:
64
+ // Tag for logging
65
+ static constexpr auto TAG = "DsaKeyPair";
66
+ };
67
+
68
+ } // namespace margelo::nitro::crypto
@@ -21,6 +21,7 @@ namespace margelo::nitro::crypto {
21
21
  prototype.registerHybridMethod("setPrivateKey", &HybridECDHSpec::setPrivateKey);
22
22
  prototype.registerHybridMethod("getPublicKey", &HybridECDHSpec::getPublicKey);
23
23
  prototype.registerHybridMethod("setPublicKey", &HybridECDHSpec::setPublicKey);
24
+ prototype.registerHybridMethod("convertKey", &HybridECDHSpec::convertKey);
24
25
  });
25
26
  }
26
27
 
@@ -56,6 +56,7 @@ namespace margelo::nitro::crypto {
56
56
  virtual void setPrivateKey(const std::shared_ptr<ArrayBuffer>& privateKey) = 0;
57
57
  virtual std::shared_ptr<ArrayBuffer> getPublicKey() = 0;
58
58
  virtual void setPublicKey(const std::shared_ptr<ArrayBuffer>& publicKey) = 0;
59
+ virtual std::shared_ptr<ArrayBuffer> convertKey(const std::shared_ptr<ArrayBuffer>& key, const std::string& curve, double format) = 0;
59
60
 
60
61
  protected:
61
62
  // Hybrid Setup
@@ -0,0 +1,24 @@
1
+ ///
2
+ /// HybridPrimeSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridPrimeSpec.hpp"
9
+
10
+ namespace margelo::nitro::crypto {
11
+
12
+ void HybridPrimeSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("generatePrime", &HybridPrimeSpec::generatePrime);
18
+ prototype.registerHybridMethod("generatePrimeSync", &HybridPrimeSpec::generatePrimeSync);
19
+ prototype.registerHybridMethod("checkPrime", &HybridPrimeSpec::checkPrime);
20
+ prototype.registerHybridMethod("checkPrimeSync", &HybridPrimeSpec::checkPrimeSync);
21
+ });
22
+ }
23
+
24
+ } // namespace margelo::nitro::crypto
@@ -0,0 +1,67 @@
1
+ ///
2
+ /// HybridPrimeSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 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
+
17
+
18
+ #include <NitroModules/ArrayBuffer.hpp>
19
+ #include <NitroModules/Promise.hpp>
20
+ #include <optional>
21
+
22
+ namespace margelo::nitro::crypto {
23
+
24
+ using namespace margelo::nitro;
25
+
26
+ /**
27
+ * An abstract base class for `Prime`
28
+ * Inherit this class to create instances of `HybridPrimeSpec` in C++.
29
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
30
+ * @example
31
+ * ```cpp
32
+ * class HybridPrime: public HybridPrimeSpec {
33
+ * public:
34
+ * HybridPrime(...): HybridObject(TAG) { ... }
35
+ * // ...
36
+ * };
37
+ * ```
38
+ */
39
+ class HybridPrimeSpec: public virtual HybridObject {
40
+ public:
41
+ // Constructor
42
+ explicit HybridPrimeSpec(): HybridObject(TAG) { }
43
+
44
+ // Destructor
45
+ ~HybridPrimeSpec() override = default;
46
+
47
+ public:
48
+ // Properties
49
+
50
+
51
+ public:
52
+ // Methods
53
+ virtual std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> generatePrime(double size, bool safe, const std::optional<std::shared_ptr<ArrayBuffer>>& add, const std::optional<std::shared_ptr<ArrayBuffer>>& rem) = 0;
54
+ virtual std::shared_ptr<ArrayBuffer> generatePrimeSync(double size, bool safe, const std::optional<std::shared_ptr<ArrayBuffer>>& add, const std::optional<std::shared_ptr<ArrayBuffer>>& rem) = 0;
55
+ virtual std::shared_ptr<Promise<bool>> checkPrime(const std::shared_ptr<ArrayBuffer>& candidate, double checks) = 0;
56
+ virtual bool checkPrimeSync(const std::shared_ptr<ArrayBuffer>& candidate, double checks) = 0;
57
+
58
+ protected:
59
+ // Hybrid Setup
60
+ void loadHybridMethods() override;
61
+
62
+ protected:
63
+ // Tag for logging
64
+ static constexpr auto TAG = "Prime";
65
+ };
66
+
67
+ } // namespace margelo::nitro::crypto
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
package/src/argon2.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { Buffer } from '@craftzdog/react-native-buffer';
2
+ import { NitroModules } from 'react-native-nitro-modules';
3
+ import type { Argon2 as NativeArgon2 } from './specs/argon2.nitro';
4
+ import { binaryLikeToArrayBuffer } from './utils';
5
+ import type { BinaryLike } from './utils';
6
+
7
+ let native: NativeArgon2;
8
+ function getNative(): NativeArgon2 {
9
+ if (native == null) {
10
+ native = NitroModules.createHybridObject<NativeArgon2>('Argon2');
11
+ }
12
+ return native;
13
+ }
14
+
15
+ export interface Argon2Params {
16
+ message: BinaryLike;
17
+ nonce: BinaryLike;
18
+ parallelism: number;
19
+ tagLength: number;
20
+ memory: number;
21
+ passes: number;
22
+ secret?: BinaryLike;
23
+ associatedData?: BinaryLike;
24
+ version?: number;
25
+ }
26
+
27
+ const ARGON2_VERSION = 0x13; // v1.3
28
+
29
+ function validateAlgorithm(algorithm: string): void {
30
+ if (
31
+ algorithm !== 'argon2d' &&
32
+ algorithm !== 'argon2i' &&
33
+ algorithm !== 'argon2id'
34
+ ) {
35
+ throw new TypeError(`Unknown argon2 algorithm: ${algorithm}`);
36
+ }
37
+ }
38
+
39
+ function toAB(value: BinaryLike): ArrayBuffer {
40
+ return binaryLikeToArrayBuffer(value);
41
+ }
42
+
43
+ export function argon2Sync(algorithm: string, params: Argon2Params): Buffer {
44
+ validateAlgorithm(algorithm);
45
+ const version = params.version ?? ARGON2_VERSION;
46
+ const result = getNative().hashSync(
47
+ algorithm,
48
+ toAB(params.message),
49
+ toAB(params.nonce),
50
+ params.parallelism,
51
+ params.tagLength,
52
+ params.memory,
53
+ params.passes,
54
+ version,
55
+ params.secret ? toAB(params.secret) : undefined,
56
+ params.associatedData ? toAB(params.associatedData) : undefined,
57
+ );
58
+ return Buffer.from(result);
59
+ }
60
+
61
+ export function argon2(
62
+ algorithm: string,
63
+ params: Argon2Params,
64
+ callback: (err: Error | null, result: Buffer) => void,
65
+ ): void {
66
+ validateAlgorithm(algorithm);
67
+ const version = params.version ?? ARGON2_VERSION;
68
+ getNative()
69
+ .hash(
70
+ algorithm,
71
+ toAB(params.message),
72
+ toAB(params.nonce),
73
+ params.parallelism,
74
+ params.tagLength,
75
+ params.memory,
76
+ params.passes,
77
+ version,
78
+ params.secret ? toAB(params.secret) : undefined,
79
+ params.associatedData ? toAB(params.associatedData) : undefined,
80
+ )
81
+ .then(ab => callback(null, Buffer.from(ab)))
82
+ .catch((err: Error) => callback(err, Buffer.alloc(0)));
83
+ }
@@ -0,0 +1,41 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import { Buffer } from '@craftzdog/react-native-buffer';
3
+ import type { Certificate as NativeCertificate } from './specs/certificate.nitro';
4
+ import type { BinaryLike } from './utils';
5
+ import { binaryLikeToArrayBuffer } from './utils';
6
+
7
+ let native: NativeCertificate;
8
+ function getNative(): NativeCertificate {
9
+ if (native == null) {
10
+ native = NitroModules.createHybridObject<NativeCertificate>('Certificate');
11
+ }
12
+ return native;
13
+ }
14
+
15
+ function toArrayBuffer(
16
+ spkac: BinaryLike,
17
+ encoding?: BufferEncoding,
18
+ ): ArrayBuffer {
19
+ if (typeof spkac === 'string') {
20
+ return binaryLikeToArrayBuffer(spkac, encoding || 'utf8');
21
+ }
22
+ return binaryLikeToArrayBuffer(spkac);
23
+ }
24
+
25
+ export class Certificate {
26
+ static exportChallenge(spkac: BinaryLike, encoding?: BufferEncoding): Buffer {
27
+ return Buffer.from(
28
+ getNative().exportChallenge(toArrayBuffer(spkac, encoding)),
29
+ );
30
+ }
31
+
32
+ static exportPublicKey(spkac: BinaryLike, encoding?: BufferEncoding): Buffer {
33
+ return Buffer.from(
34
+ getNative().exportPublicKey(toArrayBuffer(spkac, encoding)),
35
+ );
36
+ }
37
+
38
+ static verifySpkac(spkac: BinaryLike, encoding?: BufferEncoding): boolean {
39
+ return getNative().verifySpkac(toArrayBuffer(spkac, encoding));
40
+ }
41
+ }
package/src/cipher.ts CHANGED
@@ -28,18 +28,42 @@ export type CipherOptions =
28
28
  | CipherGCMOptions
29
29
  | TransformOptions;
30
30
 
31
+ export interface CipherInfoResult {
32
+ name: string;
33
+ nid: number;
34
+ mode: string;
35
+ keyLength: number;
36
+ blockSize?: number;
37
+ ivLength?: number;
38
+ }
39
+
31
40
  class CipherUtils {
32
41
  private static native =
33
42
  NitroModules.createHybridObject<NativeCipher>('Cipher');
34
43
  public static getSupportedCiphers(): string[] {
35
44
  return this.native.getSupportedCiphers();
36
45
  }
46
+ public static getCipherInfo(
47
+ name: string,
48
+ keyLength?: number,
49
+ ivLength?: number,
50
+ ): CipherInfoResult | undefined {
51
+ return this.native.getCipherInfo(name, keyLength, ivLength);
52
+ }
37
53
  }
38
54
 
39
55
  export function getCiphers(): string[] {
40
56
  return CipherUtils.getSupportedCiphers();
41
57
  }
42
58
 
59
+ export function getCipherInfo(
60
+ name: string,
61
+ options?: { keyLength?: number; ivLength?: number },
62
+ ): CipherInfoResult | undefined {
63
+ if (typeof name !== 'string' || name.length === 0) return undefined;
64
+ return CipherUtils.getCipherInfo(name, options?.keyLength, options?.ivLength);
65
+ }
66
+
43
67
  interface CipherArgs {
44
68
  isCipher: boolean;
45
69
  cipherType: string;
@@ -0,0 +1,156 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import { Buffer } from '@craftzdog/react-native-buffer';
3
+ import { KeyObject, PublicKeyObject, PrivateKeyObject } from './keys';
4
+ import type { DhKeyPair } from './specs/dhKeyPair.nitro';
5
+ import type { GenerateKeyPairOptions, KeyPairGenConfig } from './utils/types';
6
+ import { KFormatType, KeyEncoding } from './utils';
7
+ import { DH_GROUPS } from './dh-groups';
8
+
9
+ export class DhKeyPairGen {
10
+ native: DhKeyPair;
11
+
12
+ constructor(options: GenerateKeyPairOptions) {
13
+ this.native = NitroModules.createHybridObject<DhKeyPair>('DhKeyPair');
14
+
15
+ const { groupName, prime, primeLength, generator } = options;
16
+
17
+ if (groupName) {
18
+ // Resolve named group to prime + generator
19
+ const group = DH_GROUPS[groupName];
20
+ if (!group) {
21
+ throw new Error(`Unknown DH group: ${groupName}`);
22
+ }
23
+ const primeBuf = Buffer.from(group.prime, 'hex');
24
+ this.native.setPrime(
25
+ primeBuf.buffer.slice(
26
+ primeBuf.byteOffset,
27
+ primeBuf.byteOffset + primeBuf.byteLength,
28
+ ) as ArrayBuffer,
29
+ );
30
+ const gen = parseInt(group.generator, 16);
31
+ this.native.setGenerator(gen);
32
+ } else if (prime) {
33
+ // Custom prime as Buffer
34
+ const primeBuf = Buffer.from(prime);
35
+ this.native.setPrime(
36
+ primeBuf.buffer.slice(
37
+ primeBuf.byteOffset,
38
+ primeBuf.byteOffset + primeBuf.byteLength,
39
+ ) as ArrayBuffer,
40
+ );
41
+ this.native.setGenerator(generator ?? 2);
42
+ } else if (primeLength) {
43
+ this.native.setPrimeLength(primeLength);
44
+ this.native.setGenerator(generator ?? 2);
45
+ } else {
46
+ throw new Error(
47
+ 'DH key generation requires one of: groupName, prime, or primeLength',
48
+ );
49
+ }
50
+ }
51
+
52
+ async generateKeyPair(): Promise<void> {
53
+ await this.native.generateKeyPair();
54
+ }
55
+
56
+ generateKeyPairSync(): void {
57
+ this.native.generateKeyPairSync();
58
+ }
59
+ }
60
+
61
+ function dh_prepareKeyGenParams(
62
+ options: GenerateKeyPairOptions | undefined,
63
+ ): DhKeyPairGen {
64
+ if (!options) {
65
+ throw new Error('Options are required for DH key generation');
66
+ }
67
+
68
+ return new DhKeyPairGen(options);
69
+ }
70
+
71
+ function dh_formatKeyPairOutput(
72
+ dh: DhKeyPairGen,
73
+ encoding: KeyPairGenConfig,
74
+ ): {
75
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
76
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
77
+ } {
78
+ const { publicFormat, privateFormat, cipher, passphrase } = encoding;
79
+
80
+ const publicKeyData = dh.native.getPublicKey();
81
+ const privateKeyData = dh.native.getPrivateKey();
82
+
83
+ const pub = KeyObject.createKeyObject(
84
+ 'public',
85
+ publicKeyData,
86
+ KFormatType.DER,
87
+ KeyEncoding.SPKI,
88
+ ) as PublicKeyObject;
89
+
90
+ const priv = KeyObject.createKeyObject(
91
+ 'private',
92
+ privateKeyData,
93
+ KFormatType.DER,
94
+ KeyEncoding.PKCS8,
95
+ ) as PrivateKeyObject;
96
+
97
+ let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
98
+ let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
99
+
100
+ if (publicFormat === -1) {
101
+ publicKey = pub;
102
+ } else {
103
+ const format =
104
+ publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
105
+ const exported = pub.handle.exportKey(format, KeyEncoding.SPKI);
106
+ if (format === KFormatType.PEM) {
107
+ publicKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
108
+ } else {
109
+ publicKey = exported;
110
+ }
111
+ }
112
+
113
+ if (privateFormat === -1) {
114
+ privateKey = priv;
115
+ } else {
116
+ const format =
117
+ privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
118
+ const exported = priv.handle.exportKey(
119
+ format,
120
+ KeyEncoding.PKCS8,
121
+ cipher,
122
+ passphrase,
123
+ );
124
+ if (format === KFormatType.PEM) {
125
+ privateKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
126
+ } else {
127
+ privateKey = exported;
128
+ }
129
+ }
130
+
131
+ return { publicKey, privateKey };
132
+ }
133
+
134
+ export async function dh_generateKeyPairNode(
135
+ options: GenerateKeyPairOptions | undefined,
136
+ encoding: KeyPairGenConfig,
137
+ ): Promise<{
138
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
139
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
140
+ }> {
141
+ const dh = dh_prepareKeyGenParams(options);
142
+ await dh.generateKeyPair();
143
+ return dh_formatKeyPairOutput(dh, encoding);
144
+ }
145
+
146
+ export function dh_generateKeyPairNodeSync(
147
+ options: GenerateKeyPairOptions | undefined,
148
+ encoding: KeyPairGenConfig,
149
+ ): {
150
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
151
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
152
+ } {
153
+ const dh = dh_prepareKeyGenParams(options);
154
+ dh.generateKeyPairSync();
155
+ return dh_formatKeyPairOutput(dh, encoding);
156
+ }
package/src/dsa.ts ADDED
@@ -0,0 +1,129 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import { Buffer } from '@craftzdog/react-native-buffer';
3
+ import { KeyObject, PublicKeyObject, PrivateKeyObject } from './keys';
4
+ import type { DsaKeyPair } from './specs/dsaKeyPair.nitro';
5
+ import type { GenerateKeyPairOptions, KeyPairGenConfig } from './utils/types';
6
+ import { KFormatType, KeyEncoding } from './utils';
7
+
8
+ export class Dsa {
9
+ native: DsaKeyPair;
10
+
11
+ constructor(modulusLength: number, divisorLength?: number) {
12
+ this.native = NitroModules.createHybridObject<DsaKeyPair>('DsaKeyPair');
13
+ this.native.setModulusLength(modulusLength);
14
+ if (divisorLength !== undefined && divisorLength >= 0) {
15
+ this.native.setDivisorLength(divisorLength);
16
+ }
17
+ }
18
+
19
+ async generateKeyPair(): Promise<void> {
20
+ await this.native.generateKeyPair();
21
+ }
22
+
23
+ generateKeyPairSync(): void {
24
+ this.native.generateKeyPairSync();
25
+ }
26
+ }
27
+
28
+ function dsa_prepareKeyGenParams(
29
+ options: GenerateKeyPairOptions | undefined,
30
+ ): Dsa {
31
+ if (!options) {
32
+ throw new Error('Options are required for DSA key generation');
33
+ }
34
+
35
+ const { modulusLength, divisorLength } = options;
36
+
37
+ if (!modulusLength || modulusLength <= 0) {
38
+ throw new Error('Invalid or missing modulusLength for DSA key generation');
39
+ }
40
+
41
+ return new Dsa(modulusLength, divisorLength);
42
+ }
43
+
44
+ function dsa_formatKeyPairOutput(
45
+ dsa: Dsa,
46
+ encoding: KeyPairGenConfig,
47
+ ): {
48
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
49
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
50
+ } {
51
+ const { publicFormat, privateFormat, cipher, passphrase } = encoding;
52
+
53
+ const publicKeyData = dsa.native.getPublicKey();
54
+ const privateKeyData = dsa.native.getPrivateKey();
55
+
56
+ const pub = KeyObject.createKeyObject(
57
+ 'public',
58
+ publicKeyData,
59
+ KFormatType.DER,
60
+ KeyEncoding.SPKI,
61
+ ) as PublicKeyObject;
62
+
63
+ const priv = KeyObject.createKeyObject(
64
+ 'private',
65
+ privateKeyData,
66
+ KFormatType.DER,
67
+ KeyEncoding.PKCS8,
68
+ ) as PrivateKeyObject;
69
+
70
+ let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
71
+ let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
72
+
73
+ if (publicFormat === -1) {
74
+ publicKey = pub;
75
+ } else {
76
+ const format =
77
+ publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
78
+ const exported = pub.handle.exportKey(format, KeyEncoding.SPKI);
79
+ if (format === KFormatType.PEM) {
80
+ publicKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
81
+ } else {
82
+ publicKey = exported;
83
+ }
84
+ }
85
+
86
+ if (privateFormat === -1) {
87
+ privateKey = priv;
88
+ } else {
89
+ const format =
90
+ privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
91
+ const exported = priv.handle.exportKey(
92
+ format,
93
+ KeyEncoding.PKCS8,
94
+ cipher,
95
+ passphrase,
96
+ );
97
+ if (format === KFormatType.PEM) {
98
+ privateKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
99
+ } else {
100
+ privateKey = exported;
101
+ }
102
+ }
103
+
104
+ return { publicKey, privateKey };
105
+ }
106
+
107
+ export async function dsa_generateKeyPairNode(
108
+ options: GenerateKeyPairOptions | undefined,
109
+ encoding: KeyPairGenConfig,
110
+ ): Promise<{
111
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
112
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
113
+ }> {
114
+ const dsa = dsa_prepareKeyGenParams(options);
115
+ await dsa.generateKeyPair();
116
+ return dsa_formatKeyPairOutput(dsa, encoding);
117
+ }
118
+
119
+ export function dsa_generateKeyPairNodeSync(
120
+ options: GenerateKeyPairOptions | undefined,
121
+ encoding: KeyPairGenConfig,
122
+ ): {
123
+ publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
124
+ privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
125
+ } {
126
+ const dsa = dsa_prepareKeyGenParams(options);
127
+ dsa.generateKeyPairSync();
128
+ return dsa_formatKeyPairOutput(dsa, encoding);
129
+ }