From 4fe60e008b8c7ae6487398358b7fc2155445f624 Mon Sep 17 00:00:00 2001 From: taynpg Date: Mon, 30 Dec 2024 15:59:20 +0800 Subject: [PATCH] =?UTF-8?q?feature=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=94=AF?= =?UTF-8?q?=E6=8C=81sub=E7=9B=AE=E5=BD=95dll=E5=8A=A0=E8=BD=BD=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=B1=A1=E6=9F=93=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 14 +++++++++++++- CMakeLists.txt | 8 ++++---- md5.cpp | 38 +++++++++++++++++++++++++++++--------- sha256.cpp | 15 ++++++++++++++- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7f62b22..228bf63 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -84,6 +84,18 @@ "bit": "cpp", "compare": "cpp", "concepts": "cpp", - "format": "cpp" + "format": "cpp", + "algorithm": "cpp", + "chrono": "cpp", + "filesystem": "cpp", + "forward_list": "cpp", + "fstream": "cpp", + "iomanip": "cpp", + "list": "cpp", + "optional": "cpp", + "ratio": "cpp", + "sstream": "cpp", + "string_view": "cpp", + "vector": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 99ac8ce..08ff861 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,8 +16,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE} find_package(OpenSSL REQUIRED) add_executable(sha256 sha256.cpp) -target_link_libraries(sha256 PRIVATE OpenSSL::SSL) -target_link_libraries(sha256 PRIVATE OpenSSL::Crypto) +target_link_libraries(sha256 PRIVATE OpenSSL::SSL OpenSSL::Crypto delayimp) +target_link_options(sha256 PRIVATE "/DELAYLOAD:libcrypto-3-x64.dll") add_executable(md5 md5.cpp) -target_link_libraries(md5 PRIVATE OpenSSL::SSL) -target_link_libraries(md5 PRIVATE OpenSSL::Crypto) \ No newline at end of file +target_link_libraries(md5 PRIVATE OpenSSL::SSL OpenSSL::Crypto delayimp) +target_link_options(md5 PRIVATE "/DELAYLOAD:libcrypto-3-x64.dll") \ No newline at end of file diff --git a/md5.cpp b/md5.cpp index 519ba99..658fb08 100644 --- a/md5.cpp +++ b/md5.cpp @@ -1,25 +1,37 @@ -#include #include -#include #include +#include +#include +#include #include #include -std::string to_hex_string(const unsigned char* hash, size_t length) { +#ifdef _WIN32 +#include +#include +namespace fs = std::filesystem; +#endif + +std::string to_hex_string(const unsigned char* hash, size_t length) +{ std::ostringstream oss; for (size_t i = 0; i < length; ++i) { - oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); + oss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); } return oss.str(); } -std::string md5_string(const std::string& input) { +std::string md5_string(const std::string& input) +{ unsigned char hash[MD5_DIGEST_LENGTH]; - MD5(reinterpret_cast(input.c_str()), input.size(), hash); + MD5(reinterpret_cast(input.c_str()), input.size(), + hash); return to_hex_string(hash, MD5_DIGEST_LENGTH); } -std::string md5_file(const std::string& filename) { +std::string md5_file(const std::string& filename) +{ std::ifstream file(filename, std::ios::binary); if (!file) { throw std::runtime_error("Cannot open file: " + filename); @@ -40,10 +52,18 @@ std::string md5_file(const std::string& filename) { return to_hex_string(hash, MD5_DIGEST_LENGTH); } -int main(int argc, char* argv[]) { +int main(int argc, char* argv[]) +{ + +#ifdef _WIN32 +#include + std::string load_dll_dir = fs::path(argv[0]).append("mdsha_dll").string(); + SetDllDirectory(load_dll_dir.c_str()); +#endif + if (argc != 3) { std::cerr << "Usage: " << argv[0] << " " << std::endl; - std::cerr << "Modes:" << std::endl; + std::cerr << "Support DelayLoad SubDirectory:mdsha_dll" << std::endl; std::cerr << " -s : Calculate MD5 for a string" << std::endl; std::cerr << " -f : Calculate MD5 for a file" << std::endl; return 1; diff --git a/sha256.cpp b/sha256.cpp index 9425958..4caa9cb 100644 --- a/sha256.cpp +++ b/sha256.cpp @@ -5,6 +5,12 @@ #include #include +#ifdef _WIN32 +#include +#include +namespace fs = std::filesystem; +#endif + std::string to_hex_string(const unsigned char* hash, size_t length) { std::ostringstream oss; for (size_t i = 0; i < length; ++i) { @@ -41,9 +47,16 @@ std::string sha256_file(const std::string& filename) { } int main(int argc, char* argv[]) { + +#ifdef _WIN32 +#include + std::string load_dll_dir = fs::path(argv[0]).append("mdsha_dll").string(); + SetDllDirectory(load_dll_dir.c_str()); +#endif + if (argc != 3) { std::cerr << "Usage: " << argv[0] << " " << std::endl; - std::cerr << "Modes:" << std::endl; + std::cerr << "Support DelayLoad SubDirectory:mdsha_dll" << std::endl; std::cerr << " -s : Calculate SHA-256 for a string" << std::endl; std::cerr << " -f : Calculate SHA-256 for a file" << std::endl; return 1;