Skip to content

Commit 11fd07d

Browse files
committed
add getSupportedCiphers
1 parent c69956f commit 11fd07d

File tree

8 files changed

+49
-1
lines changed

8 files changed

+49
-1
lines changed

example/src/tests/cipher/cipher_tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Cipher,
3+
CipherUtils,
34
createCipheriv,
45
randomFillSync,
56
} from 'react-native-quick-crypto';
@@ -21,3 +22,9 @@ test(SUITE, 'cipher - invalid algorithm', async () => {
2122
createCipheriv('aes-128-boorad', key, iv, {});
2223
}).to.throw(/Invalid Cipher Algorithm: aes-128-boorad/);
2324
});
25+
26+
test(SUITE, 'cipher - getSupportedCiphers', async () => {
27+
const ciphers = CipherUtils.getSupportedCiphers();
28+
expect(ciphers).to.be.instanceOf(Array);
29+
expect(ciphers).to.have.length.greaterThan(0);
30+
});

packages/react-native-quick-crypto/QuickCrypto.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Pod::Spec.new do |s|
3737
add_dependency(s, "React-jsinspector", :framework_name => "jsinspector_modern")
3838
add_dependency(s, "React-rendererconsistency", :framework_name => "React_rendererconsistency")
3939
end
40-
40+
4141
# Add all files generated by Nitrogen
4242
load 'nitrogen/generated/ios/QuickCrypto+autolinking.rb'
4343
add_nitrogen_files(s)

packages/react-native-quick-crypto/cpp/cipher/HybridCipher.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <memory>
2+
#include <vector>
3+
#include <openssl/evp.h>
24

35
#include "HybridCipher.hpp"
46

@@ -67,6 +69,25 @@ HybridCipher::init() {
6769
if (cipher == nullptr) {
6870
throw std::runtime_error("Invalid Cipher Algorithm: " + args.cipherType);
6971
}
72+
73+
void collect_ciphers(EVP_CIPHER *cipher, void *arg) {
74+
auto ciphers = static_cast<std::vector<std::string>*>(arg);
75+
const char* name = EVP_CIPHER_get0_name(cipher);
76+
if (name != nullptr) {
77+
ciphers->push_back(name);
78+
}
79+
}
80+
std::vector<std::string>
81+
HybridCipher::getSupportedCiphers() {
82+
std::vector<std::string> ciphers;
83+
84+
EVP_CIPHER_do_all_provided(
85+
nullptr, // nullptr is default library context
86+
collect_ciphers,
87+
&ciphers
88+
);
89+
90+
return ciphers;
7091
}
7192

7293
} // namespace margelo::nitro::crypto

packages/react-native-quick-crypto/cpp/cipher/HybridCipher.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <memory>
22
#include <openssl/evp.h>
33
#include <optional>
4+
#include <vector>
45

56
#include "HybridCipherSpec.hpp"
67
#include "CipherArgs.hpp"
@@ -50,6 +51,9 @@ class HybridCipher : public HybridCipherSpec {
5051
std::shared_ptr<ArrayBuffer>
5152
getAuthTag() override;
5253

54+
std::vector<std::string>
55+
getSupportedCiphers() override;
56+
5357
private:
5458
// Methods
5559
void init();

packages/react-native-quick-crypto/nitrogen/generated/shared/c++/HybridCipherSpec.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace margelo::nitro::crypto {
2222
prototype.registerHybridMethod("setAutoPadding", &HybridCipherSpec::setAutoPadding);
2323
prototype.registerHybridMethod("setAuthTag", &HybridCipherSpec::setAuthTag);
2424
prototype.registerHybridMethod("getAuthTag", &HybridCipherSpec::getAuthTag);
25+
prototype.registerHybridMethod("getSupportedCiphers", &HybridCipherSpec::getSupportedCiphers);
2526
});
2627
}
2728

packages/react-native-quick-crypto/nitrogen/generated/shared/c++/HybridCipherSpec.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace margelo::nitro::crypto { struct CipherArgs; }
2121
#include <NitroModules/ArrayBuffer.hpp>
2222
#include "CipherArgs.hpp"
2323
#include <optional>
24+
#include <vector>
25+
#include <string>
2426

2527
namespace margelo::nitro::crypto {
2628

@@ -61,6 +63,7 @@ namespace margelo::nitro::crypto {
6163
virtual bool setAutoPadding(bool autoPad) = 0;
6264
virtual bool setAuthTag(const std::shared_ptr<ArrayBuffer>& tag) = 0;
6365
virtual std::shared_ptr<ArrayBuffer> getAuthTag() = 0;
66+
virtual std::vector<std::string> getSupportedCiphers() = 0;
6467

6568
protected:
6669
// Hybrid Setup

packages/react-native-quick-crypto/src/cipher.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ import {
2828
validateEncoding,
2929
} from './utils/cipher';
3030

31+
export class CipherUtils {
32+
private static native = NitroModules.createHybridObject<NativeCipher>('Cipher');
33+
public static getSupportedCiphers(): string[] {
34+
return this.native.getSupportedCiphers();
35+
}
36+
}
37+
3138
interface CipherArgs {
3239
cipherType: string;
3340
cipherKey: BinaryLikeNode;
@@ -149,6 +156,10 @@ class CipherCommon extends Stream.Transform {
149156
}
150157
return this;
151158
}
159+
160+
public getSupportedCiphers(): string[] {
161+
return this.native.getSupportedCiphers();
162+
}
152163
}
153164

154165
export class Cipher extends CipherCommon {

packages/react-native-quick-crypto/src/specs/cipher.nitro.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export interface Cipher extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
1717
setAutoPadding(autoPad: boolean): boolean;
1818
setAuthTag(tag: ArrayBuffer): boolean;
1919
getAuthTag(): ArrayBuffer;
20+
getSupportedCiphers(): string[];
2021
}

0 commit comments

Comments
 (0)