Skip to content
Closed
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
47 changes: 47 additions & 0 deletions bun.lock

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
},
"workspaces": [
"packages/react-native-quick-crypto",
"packages/quick-crypto-nitro",
"example"
],
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/quick-crypto-nitro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ios/openssl/
ios/openssl.*/
cpp/internal/
27 changes: 27 additions & 0 deletions packages/quick-crypto-nitro/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
**react-native-quick-crypto**

MIT License

Copyright (c) 2021 Margelo GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

**NodeJS Crypto**

See https://github.com/nodejs/node/blob/main/LICENSE
38 changes: 38 additions & 0 deletions packages/quick-crypto-nitro/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// swift-tools-version: 6.4

import PackageDescription

let generated = "nitrogen/generated"
let package = Package(
name: "QuickCryptoNitro",
platforms: [.iOS(.v16), .macOS(.v13)],
products: [.library(name: "QuickCryptoNitro", targets: ["QuickCryptoNitro"])],
dependencies: [.package(path: "../nitro-core")],
targets: [
.binaryTarget(
name: "QuickCryptoOpenSSL",
url: "https://github.com/margelo/react-native-quick-crypto/releases/download/openssl-apple-3.6.2/QuickCryptoOpenSSL-3.6.2.zip",
checksum: "a50e3c8473b0526b159ad8d105e97a90d1153a4a230388cc731ee23a7d0ad3a4"
),
.target(
name: "QuickCryptoNitro",
dependencies: [.product(name: "NitroModules", package: "nitro-core"), "QuickCryptoOpenSSL"],
path: ".",
exclude: ["\(generated)/apple/c++/include/module.modulemap"],
sources: [
"backend", "cpp", "\(generated)/shared/c++", "\(generated)/apple/c++",
"\(generated)/apple/QuickCryptoNitroAutolinking.cpp",
],
publicHeadersPath: "\(generated)/apple/c++/include",
cxxSettings: [
.headerSearchPath("backend"),
.headerSearchPath("cpp/include"),
.headerSearchPath("\(generated)/shared/c++/include"),
.headerSearchPath("ios/openssl/include"),
.headerSearchPath("ios/openssl"),
.unsafeFlags(["-include", "quickcrypto_openssl_prefix.h"]),
]
),
],
cxxLanguageStandard: .cxx20
)
13 changes: 13 additions & 0 deletions packages/quick-crypto-nitro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Quick Crypto for standalone Nitro

This package exposes incremental, fixed-length OpenSSL hashes to a standalone Nitro v1 runtime on iOS and Android. It shares the digest operations used by `react-native-quick-crypto`. The current API is `createHash(algorithm)`, `update(string | ArrayBufferView, inputEncoding?)`, and `digest(encoding?)`. Calling `digest()` returns a Buffer and finalizes that hash instance.

```ts
import { createHash } from 'quick-crypto-nitro';

const hash = createHash('sha256');
hash.update('abc');
const hex = hash.digest('hex');
```

When working from a checkout, run `bash scripts/prepare-sources.sh` and `bash scripts/prepare-openssl-apple.sh` in this directory before building a native app. The package tarball includes those headers.
3 changes: 3 additions & 0 deletions packages/quick-crypto-nitro/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
implementation 'io.github.ronickg:openssl-static:3.6.2-3'
}
13 changes: 13 additions & 0 deletions packages/quick-crypto-nitro/android/react-nitro.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
find_package(opensslstatic REQUIRED CONFIG)

# The host runtime's libwebsockets supplies OpenSSL-compatible symbols of its own.
# Give Quick Crypto a private copy of libcrypto behind the hash backend's ABI.
set(quickcrypto_backend_dir "${CMAKE_CURRENT_LIST_DIR}/../backend")
add_library(QuickCryptoHashBackend SHARED "${quickcrypto_backend_dir}/HashBackend.cpp")
set_target_properties(QuickCryptoHashBackend PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES)
target_include_directories(QuickCryptoHashBackend PUBLIC "${quickcrypto_backend_dir}")
target_link_libraries(QuickCryptoHashBackend PRIVATE opensslstatic::crypto)
target_link_options(QuickCryptoHashBackend PRIVATE "LINKER:--exclude-libs,libcrypto.a")

target_include_directories(QuickCryptoNitro PRIVATE "${CMAKE_CURRENT_LIST_DIR}/../cpp/include")
target_link_libraries(QuickCryptoNitro QuickCryptoHashBackend)
32 changes: 32 additions & 0 deletions packages/quick-crypto-nitro/backend/HashBackend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "HashBackend.hpp"
#include "../cpp/internal/HashContext.hpp"

namespace margelo::quickcrypto {

HashBackend::HashBackend(const std::string& algorithm) : context(createDigestContext(algorithm)) {}

void HashBackend::update(const uint8_t* data, size_t length) {
if (!context) {
throw std::logic_error("Digest already called");
}
updateDigest(context.get(), data, length);
}

std::vector<uint8_t> HashBackend::digest() {
if (!context) {
throw std::logic_error("Digest already called");
}
const int length = EVP_MD_CTX_get_size(context.get());
if (length <= 0) {
throw std::runtime_error("Expected a fixed-length digest");
}
std::vector<uint8_t> output(length);
auto active = std::move(context);
const auto written = finishDigest(active.get(), output.data());
if (written != output.size()) {
throw std::runtime_error("Unexpected hash digest length");
}
return output;
}

} // namespace margelo::quickcrypto
23 changes: 23 additions & 0 deletions packages/quick-crypto-nitro/backend/HashBackend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

struct evp_md_ctx_st;

namespace margelo::quickcrypto {

class __attribute__((visibility("default"))) HashBackend final {
public:
explicit HashBackend(const std::string& algorithm);
void update(const uint8_t* data, size_t length);
std::vector<uint8_t> digest();

private:
std::unique_ptr<evp_md_ctx_st, void (*)(evp_md_ctx_st*)> context;
};

} // namespace margelo::quickcrypto
16 changes: 16 additions & 0 deletions packages/quick-crypto-nitro/cpp/HybridHash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <QuickCryptoNitro/HybridHash.hpp>

namespace margelo::nitro::quickcrypto {

HybridHash::HybridHash(const std::string& algorithm) : backend(algorithm) {}

void HybridHash::update(const ArrayBuffer& data) {
backend.update(data.data(), data.size());
}

ArrayBuffer HybridHash::digest() {
auto bytes = backend.digest();
return ArrayBuffer::move(std::move(bytes));
}

} // namespace margelo::nitro::quickcrypto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "HashBackend.hpp"
#include <QuickCryptoNitro/HybridHashSpec.hpp>

namespace margelo::nitro::quickcrypto {

class HybridHash final : public HybridHashSpec {
public:
explicit HybridHash(const std::string& algorithm);
void update(const ArrayBuffer& data) override;
ArrayBuffer digest() override;

private:
::margelo::quickcrypto::HashBackend backend;
};

} // namespace margelo::nitro::quickcrypto
Loading