diff --git a/cpp/NativeBcryptCppTurboModule.cpp b/cpp/NativeBcryptCppTurboModule.cpp index aebcff4..948c23d 100644 --- a/cpp/NativeBcryptCppTurboModule.cpp +++ b/cpp/NativeBcryptCppTurboModule.cpp @@ -1,25 +1,77 @@ #include "NativeBcryptCppTurboModule.h" -//#import +#include +namespace facebook::react +{ +NativeBcryptCppTurboModule::NativeBcryptCppTurboModule(std::shared_ptr jsinvoker) : NativeBcryptCppCxxSpec(std::move(jsinvoker)) {} - -namespace facebook::react { -NativeBcryptCppTurboModule::NativeBcryptCppTurboModule(std::shared_ptr jsinvoker): NativeBcryptCppCxxSpec(std::move(jsinvoker)) {} - -jsi::Value NativeBcryptCppTurboModule::generateHash(jsi::Runtime &rt, std::string password, double workload) { - return jsi::Value::undefined(); +jsi::Value NativeBcryptCppTurboModule::generateHash(jsi::Runtime &rt, std::string password, double workload) +{ + jsi::Function promiseConstructor = rt.global().getPropertyAsFunction(rt, "Promise"); + + return promiseConstructor.callAsConstructor(rt, + jsi::Function::createFromHostFunction( + rt, + jsi::PropNameID::forAscii(rt, "promiseArg"), + 2, + [password, workload, jsInvoker = jsInvoker_]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + std::size_t count) -> jsi::Value + { + auto resolverValue = std::make_shared((arguments[0].asObject(runtime))); + + std::thread([password, workload, resolverValue = std::move(resolverValue), jsInvoker, &runtime]() + { + std::string hash = bcrypt::generateHash(password, workload); + // Post back to JS thread + jsInvoker->invokeAsync([resolverValue, hash, &runtime]() { + resolverValue->asObject(runtime).asFunction(runtime).call(runtime, hash); + }); }) + .detach(); + return jsi::Value::undefined(); + }) + + ); } - -jsi::Value NativeBcryptCppTurboModule::validatePassword(jsi::Runtime &rt, std::string password, std::string hash) { - return jsi::Value::undefined(); +jsi::Value NativeBcryptCppTurboModule::validatePassword(jsi::Runtime &rt, std::string password, std::string hash) +{ + jsi::Function promiseConstructor = rt.global().getPropertyAsFunction(rt, "Promise"); + + return promiseConstructor.callAsConstructor(rt, + jsi::Function::createFromHostFunction( + rt, + jsi::PropNameID::forAscii(rt, "promiseArg"), + 2, + [password, hash, jsInvoker = jsInvoker_]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + std::size_t count) -> jsi::Value + { + auto resolverValue = std::make_shared((arguments[0].asObject(runtime))); + + std::thread([password, hash, resolverValue = std::move(resolverValue), jsInvoker, &runtime]() + { + bool isValid = bcrypt::validatePassword(password, hash); + // Post back to JS thread + jsInvoker->invokeAsync([resolverValue, isValid, &runtime]() { + resolverValue->asObject(runtime).asFunction(runtime).call(runtime, isValid); + }); }) + .detach(); + return jsi::Value::undefined(); + }) + + ); } -std::string NativeBcryptCppTurboModule::generateHashSync(jsi::Runtime &rt, std::string password, double workload){ - - std::string generatedHash = bcrypt::generateHash(password, workload); - return generatedHash; +std::string NativeBcryptCppTurboModule::generateHashSync(jsi::Runtime &rt, std::string password, double workload) +{ + return bcrypt::generateHash(password, workload); } -bool NativeBcryptCppTurboModule::validatePasswordSync(jsi::Runtime &rt, std::string password, std::string hash) { +bool NativeBcryptCppTurboModule::validatePasswordSync(jsi::Runtime &rt, std::string password, std::string hash) +{ return bcrypt::validatePassword(password, hash); } diff --git a/example/ios/BcryptCppExample.xcodeproj/project.pbxproj b/example/ios/BcryptCppExample.xcodeproj/project.pbxproj index d4a95c1..0d3ce22 100644 --- a/example/ios/BcryptCppExample.xcodeproj/project.pbxproj +++ b/example/ios/BcryptCppExample.xcodeproj/project.pbxproj @@ -31,7 +31,7 @@ 00E356EE1AD99517003FC87E /* BcryptCppExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BcryptCppExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 00E356F21AD99517003FC87E /* BcryptCppExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BcryptCppExampleTests.m; sourceTree = ""; }; - 0580ABFB39ADC1A50C1EF3C7 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; name = PrivacyInfo.xcprivacy; path = BcryptCppExample/PrivacyInfo.xcprivacy; sourceTree = ""; }; + 0580ABFB39ADC1A50C1EF3C7 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = BcryptCppExample/PrivacyInfo.xcprivacy; sourceTree = ""; }; 13B07F961A680F5B00A75B9A /* BcryptCppExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BcryptCppExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = BcryptCppExample/AppDelegate.h; sourceTree = ""; }; 13B07FB01A68108700A75B9A /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = BcryptCppExample/AppDelegate.mm; sourceTree = ""; }; @@ -472,6 +472,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 99NF33M5QW; ENABLE_BITCODE = NO; INFOPLIST_FILE = BcryptCppExample/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -499,6 +500,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 99NF33M5QW; INFOPLIST_FILE = BcryptCppExample/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -590,10 +592,7 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = ( - "$(inherited)", - " ", - ); + OTHER_LDFLAGS = "$(inherited) "; REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; @@ -665,10 +664,7 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = ( - "$(inherited)", - " ", - ); + OTHER_LDFLAGS = "$(inherited) "; REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; diff --git a/example/src/App.tsx b/example/src/App.tsx index f9b1ebe..892ce98 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -5,45 +5,61 @@ import { validatePassword, validatePasswordSync, } from 'react-native-bcrypt-cpp'; +import { MovingRectangle } from './MovingRectangle'; + +const workload = 15; +const password = 'asdcds-sdjakl12313841skdnanczdeioaj'; +const hash = '$2b$15$wX0mKtpwGwzdfbz099nUnu5R.NN/huUK9XBra0sS4xj4XfjfPOkzO'; + +async function measureTime(fn: () => T): Promise { + const start = performance.now(); + const res = await fn(); + const end = performance.now(); + const timeTaken = end - start; + console.log('Time taken :', timeTaken, 'ms'); + return res; +} export default function App() { return ( +