diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f5a99..6b7528b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,8 +46,13 @@ else() endif() # Handle Crypto++ dependency -set(CRYPTOPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/installed/x64-windows/include") -set(CRYPTOPP_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/installed/x64-windows/lib/cryptopp.lib") +find_package(cryptopp CONFIG QUIET) + +if(cryptopp_FOUND) + message(STATUS "Found Crypto++ package via vcpkg") +else() + message(WARNING "Crypto++ not found via vcpkg. Make sure to use the vcpkg toolchain file.") +endif() # Add library sources set(SOURCES @@ -72,11 +77,12 @@ add_library(privatebinapi SHARED ${SOURCES} ${HEADERS}) # Include directories target_include_directories(privatebinapi PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include - ${CRYPTOPP_INCLUDE_DIRS} ) # Link Crypto++ -target_link_libraries(privatebinapi PRIVATE ${CRYPTOPP_LIBRARIES}) +if(cryptopp_FOUND) + target_link_libraries(privatebinapi PRIVATE cryptopp::cryptopp) +endif() # Include nlohmann/json if(nlohmann_json_FOUND) diff --git a/build.bat b/build.bat index f4b85ea..abc4556 100644 --- a/build.bat +++ b/build.bat @@ -5,8 +5,8 @@ REM Create build directory if not exist "build" mkdir build cd build -REM Generate build files with CMake -cmake .. -G "Visual Studio 17 2022" +REM Generate build files with CMake and vcpkg toolchain +cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake REM Build the project cmake --build . --config Release diff --git a/src/crypto.cpp b/src/crypto.cpp index 7df5772..1edf16f 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -3,7 +3,6 @@ #include #include -#ifndef NO_CRYPTO // Crypto++ includes #include "cryptlib.h" #include "osrng.h" // AutoSeededRandomPool @@ -14,10 +13,8 @@ #include "zlib.h" // Zlib compression using namespace CryptoPP; -#endif std::vector Crypto::generate_key(size_t length) { -#ifndef NO_CRYPTO std::vector key(length); // Use Crypto++ AutoSeededRandomPool for cryptographically secure random numbers @@ -25,26 +22,12 @@ std::vector Crypto::generate_key(size_t length) { rng.GenerateBlock(key.data(), length); return key; -#else - // Fallback to std::random - NOT cryptographically secure! - std::vector key(length); - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> dis(0, 255); - - for (size_t i = 0; i < length; ++i) { - key[i] = static_cast(dis(gen)); - } - - return key; -#endif } std::vector Crypto::encrypt(const std::vector& plaintext, const std::vector& key, const std::vector& iv, std::vector& auth_tag) { -#ifndef NO_CRYPTO try { // Create GCM mode encryption object GCM::Encryption encryption; @@ -76,23 +59,12 @@ std::vector Crypto::encrypt(const std::vector& pla catch(const CryptoPP::Exception& e) { throw std::runtime_error("Encryption failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library like Crypto++ or OpenSSL - // to perform AES-GCM encryption - - // For demonstration purposes, we'll just return the plaintext - // In a real implementation, this would be the actual encryption - auth_tag.resize(16, 0); // 128-bit authentication tag - return plaintext; -#endif } std::vector Crypto::decrypt(const std::vector& ciphertext, const std::vector& key, const std::vector& iv, const std::vector& auth_tag) { -#ifndef NO_CRYPTO try { // Create GCM mode decryption object GCM::Decryption decryption; @@ -125,22 +97,12 @@ std::vector Crypto::decrypt(const std::vector& cip catch(const CryptoPP::Exception& e) { throw std::runtime_error("Decryption failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library like Crypto++ or OpenSSL - // to perform AES-GCM decryption - - // For demonstration purposes, we'll just return the ciphertext - // In a real implementation, this would be the actual decryption - return ciphertext; -#endif } std::vector Crypto::pbkdf2_hmac_sha256(const std::string& password, const std::vector& salt, int iterations, size_t key_length) { -#ifndef NO_CRYPTO try { std::vector derived_key(key_length); @@ -163,23 +125,9 @@ std::vector Crypto::pbkdf2_hmac_sha256(const std::string& passwor catch(const CryptoPP::Exception& e) { throw std::runtime_error("PBKDF2 key derivation failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library to perform PBKDF2-HMAC-SHA256 - - // For demonstration purposes, we'll just return a key of the requested length - // filled with a simple pattern - std::vector key(key_length, 0); - for (size_t i = 0; i < key_length; i++) { - key[i] = static_cast((i * 17) % 256); - } - - return key; -#endif } std::vector Crypto::compress(const std::vector& data) { -#ifndef NO_CRYPTO try { std::string compressed; @@ -199,17 +147,9 @@ std::vector Crypto::compress(const std::vector& da catch(const CryptoPP::Exception& e) { throw std::runtime_error("Compression failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use zlib or another compression library - - // For demonstration purposes, we'll just return the data as-is - return data; -#endif } std::vector Crypto::decompress(const std::vector& data) { -#ifndef NO_CRYPTO try { std::string decompressed; @@ -229,11 +169,4 @@ std::vector Crypto::decompress(const std::vector& catch(const CryptoPP::Exception& e) { throw std::runtime_error("Decompression failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use zlib or another decompression library - - // For demonstration purposes, we'll just return the data as-is - return data; -#endif } \ No newline at end of file