Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
416 changes: 259 additions & 157 deletions bun.lock

Large diffs are not rendered by default.

Binary file modified docs/test_suite_results_ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"@noble/curves": "^1.7.0",
"@noble/hashes": "^1.5.0",
"@react-navigation/bottom-tabs": "^6.6.1",
"@react-navigation/native": "6.1.18",
"@react-navigation/native-stack": "7.3.25",
"@react-navigation/native": "^6.1.18",
"@react-navigation/native-stack": "^6.11.0",
"buffer": "6.0.3",
"chai": "<5.0.0",
"crypto-browserify": "^3.12.0",
Expand Down
11 changes: 6 additions & 5 deletions example/src/tests/hash/hash_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
createHash,
getHashes,
type Encoding,
keccak256,
} from 'react-native-quick-crypto';
import { expect } from 'chai';
import { test } from '../util';
Expand Down Expand Up @@ -45,21 +44,23 @@ test(SUITE, 'check openssl version', () => {
}).to.not.throw();
});

test(SUITE, 'keccak256 function using provider-aware API', () => {
test(SUITE, 'KECCAK-256 using createHash with provider-aware API', () => {
// Test with a simple string
const result1 = keccak256('test');
const result1 = createHash('KECCAK-256').update('test').digest();
expect(result1.toString('hex')).to.equal(
'9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658',
);

// Test with empty string
const result2 = keccak256('');
const result2 = createHash('KECCAK-256').update('').digest();
expect(result2.toString('hex')).to.equal(
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
);

// Test with Buffer
const result3 = keccak256(Buffer.from('hello world'));
const result3 = createHash('KECCAK-256')
.update(Buffer.from('hello world'))
.digest();
expect(result3.toString('hex')).to.equal(
'47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad',
);
Expand Down
57 changes: 18 additions & 39 deletions packages/react-native-quick-crypto/cpp/hash/HybridHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ HybridHash::~HybridHash() {
EVP_MD_CTX_free(ctx);
ctx = nullptr;
}
if (md && md_fetched) {
EVP_MD_free(md);
md = nullptr;
}
}

void HybridHash::createHash(const std::string& hashAlgorithmArg, const std::optional<double> outputLengthArg) {
Expand All @@ -28,18 +32,24 @@ void HybridHash::createHash(const std::string& hashAlgorithmArg, const std::opti
throw std::runtime_error("Failed to create hash context: " + std::to_string(ERR_get_error()));
}

// Get the message digest by name
md = EVP_get_digestbyname(algorithm.c_str());
// Fetch the message digest using modern provider-based API
md = EVP_MD_fetch(nullptr, algorithm.c_str(), nullptr);
if (!md) {
EVP_MD_CTX_free(ctx);
ctx = nullptr;
throw std::runtime_error("Unknown hash algorithm: " + algorithm);
}
md_fetched = true;

// Initialize the digest
if (EVP_DigestInit_ex(ctx, md, nullptr) != 1) {
EVP_MD_CTX_free(ctx);
ctx = nullptr;
if (md_fetched) {
EVP_MD_free(md);
md = nullptr;
md_fetched = false;
}
throw std::runtime_error("Failed to initialize hash digest: " + std::to_string(ERR_get_error()));
}
}
Expand Down Expand Up @@ -107,7 +117,7 @@ std::shared_ptr<margelo::nitro::crypto::HybridHashSpec> HybridHash::copy(const s
throw std::runtime_error("Failed to copy hash context: " + std::to_string(ERR_get_error()));
}

return std::make_shared<HybridHash>(newCtx, md, algorithm, outputLengthArg);
return std::make_shared<HybridHash>(newCtx, md, algorithm, outputLengthArg, false);
}

std::vector<std::string> HybridHash::getSupportedHashAlgorithms() {
Expand Down Expand Up @@ -144,6 +154,11 @@ void HybridHash::setParams() {
if (EVP_MD_CTX_set_params(ctx, params) != 1) {
EVP_MD_CTX_free(ctx);
ctx = nullptr;
if (md && md_fetched) {
EVP_MD_free(md);
md = nullptr;
md_fetched = false;
}
throw std::runtime_error("Failed to set XOF length (outputLength) parameter: " + std::to_string(ERR_get_error()));
}
}
Expand All @@ -153,40 +168,4 @@ std::string HybridHash::getOpenSSLVersion() {
return OpenSSL_version(OPENSSL_VERSION);
}

std::shared_ptr<ArrayBuffer> HybridHash::keccak256(const std::shared_ptr<ArrayBuffer>& data) {
// 1. Obtain the Keccak-256 message-digest implementation from any loaded provider.
const EVP_MD* md = EVP_MD_fetch(nullptr, "KECCAK-256", nullptr);
if (!md) {
throw std::runtime_error("KECCAK-256 digest not available in the current OpenSSL build (provider not loaded?)");
}

// 2. Create and initialise a digest context.
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
if (!ctx) {
throw std::runtime_error("Failed to allocate EVP_MD_CTX");
}
auto ctx_guard = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>(ctx, &EVP_MD_CTX_free);

if (EVP_DigestInit_ex(ctx, md, nullptr) != 1) {
throw std::runtime_error("Failed to initialise KECCAK-256 digest");
}

// 3. Feed the data.
if (EVP_DigestUpdate(ctx, data->data(), data->size()) != 1) {
throw std::runtime_error("Failed to update KECCAK-256 digest");
}

// 4. Finalise and collect the output.
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int out_len = 0;
if (EVP_DigestFinal_ex(ctx, hash, &out_len) != 1) {
throw std::runtime_error("Failed to finalise KECCAK-256 digest");
}

// 5. Move the result into a managed ArrayBuffer.
unsigned char* out_buf = new unsigned char[out_len];
std::memcpy(out_buf, hash, out_len);
return std::make_shared<NativeArrayBuffer>(out_buf, out_len, [out_buf]() { delete[] out_buf; });
}

} // namespace margelo::nitro::crypto
8 changes: 4 additions & 4 deletions packages/react-native-quick-crypto/cpp/hash/HybridHash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ using namespace facebook;
class HybridHash : public HybridHashSpec {
public:
HybridHash() : HybridObject(TAG) {}
HybridHash(EVP_MD_CTX* ctx, const EVP_MD* md, const std::string& algorithm, const std::optional<double> outputLength)
: HybridObject(TAG), ctx(ctx), md(md), algorithm(algorithm), outputLength(outputLength) {}
HybridHash(EVP_MD_CTX* ctx, EVP_MD* md, const std::string& algorithm, const std::optional<double> outputLength, bool md_fetched = false)
: HybridObject(TAG), ctx(ctx), md(md), md_fetched(md_fetched), algorithm(algorithm), outputLength(outputLength) {}
~HybridHash();

public:
Expand All @@ -26,7 +26,6 @@ class HybridHash : public HybridHashSpec {
std::shared_ptr<margelo::nitro::crypto::HybridHashSpec> copy(const std::optional<double> outputLength) override;
std::vector<std::string> getSupportedHashAlgorithms() override;
std::string getOpenSSLVersion() override;
std::shared_ptr<ArrayBuffer> keccak256(const std::shared_ptr<ArrayBuffer>& data) override;

private:
// Methods
Expand All @@ -35,7 +34,8 @@ class HybridHash : public HybridHashSpec {
private:
// Properties
EVP_MD_CTX* ctx = nullptr;
const EVP_MD* md = nullptr;
EVP_MD* md = nullptr;
bool md_fetched = false;
std::string algorithm = "";
std::optional<double> outputLength = std::nullopt;
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions packages/react-native-quick-crypto/src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@ class HashUtils {
public static getSupportedHashAlgorithms(): string[] {
return this.native.getSupportedHashAlgorithms();
}
public static keccak256(data: BinaryLike): Buffer {
const nativeDigest = this.native.keccak256(
binaryLikeToArrayBuffer(data, 'utf8'),
);
return Buffer.from(nativeDigest);
}
}

export function getHashes() {
return HashUtils.getSupportedHashAlgorithms();
}

export function keccak256(data: BinaryLike): Buffer {
return HashUtils.keccak256(data);
}

interface HashOptions extends TransformOptions {
/**
* For XOF hash functions such as `shake256`, the
Expand Down Expand Up @@ -175,19 +165,6 @@ class Hash extends Stream.Transform {
return this.native.getOpenSSLVersion();
}

/**
* Computes KECCAK-256 hash of the provided data using OpenSSL's provider-aware API
* @since v1.0.0
* @param data The data to hash
* @returns Buffer containing the KECCAK-256 hash
*/
keccak256(data: BinaryLike): Buffer {
const nativeDigest = this.native.keccak256(
binaryLikeToArrayBuffer(data, 'utf8'),
);
return Buffer.from(nativeDigest);
}

// stream interface
_transform(
chunk: BinaryLike,
Expand Down Expand Up @@ -236,5 +213,4 @@ export function createHash(algorithm: string, options?: HashOptions): Hash {
export const hashExports = {
createHash,
getHashes,
keccak256,
};
1 change: 0 additions & 1 deletion packages/react-native-quick-crypto/src/specs/hash.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export interface Hash extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
copy(outputLength?: number): Hash;
getSupportedHashAlgorithms(): string[];
getOpenSSLVersion(): string;
keccak256(data: ArrayBuffer): ArrayBuffer;
}
Loading