feature:添加支持sub目录dll加载,避免污染环境变量。
This commit is contained in:
parent
6d474abac0
commit
4fe60e008b
14
.vscode/settings.json
vendored
14
.vscode/settings.json
vendored
@ -84,6 +84,18 @@
|
|||||||
"bit": "cpp",
|
"bit": "cpp",
|
||||||
"compare": "cpp",
|
"compare": "cpp",
|
||||||
"concepts": "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"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,8 +16,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
|
|||||||
|
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
add_executable(sha256 sha256.cpp)
|
add_executable(sha256 sha256.cpp)
|
||||||
target_link_libraries(sha256 PRIVATE OpenSSL::SSL)
|
target_link_libraries(sha256 PRIVATE OpenSSL::SSL OpenSSL::Crypto delayimp)
|
||||||
target_link_libraries(sha256 PRIVATE OpenSSL::Crypto)
|
target_link_options(sha256 PRIVATE "/DELAYLOAD:libcrypto-3-x64.dll")
|
||||||
add_executable(md5 md5.cpp)
|
add_executable(md5 md5.cpp)
|
||||||
target_link_libraries(md5 PRIVATE OpenSSL::SSL)
|
target_link_libraries(md5 PRIVATE OpenSSL::SSL OpenSSL::Crypto delayimp)
|
||||||
target_link_libraries(md5 PRIVATE OpenSSL::Crypto)
|
target_link_options(md5 PRIVATE "/DELAYLOAD:libcrypto-3-x64.dll")
|
38
md5.cpp
38
md5.cpp
@ -1,25 +1,37 @@
|
|||||||
#include <openssl/md5.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <openssl/md5.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
std::string to_hex_string(const unsigned char* hash, size_t length) {
|
#ifdef _WIN32
|
||||||
|
#include <filesystem>
|
||||||
|
#include <windows.h>
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::string to_hex_string(const unsigned char* hash, size_t length)
|
||||||
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
for (size_t i = 0; i < length; ++i) {
|
for (size_t i = 0; i < length; ++i) {
|
||||||
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
|
oss << std::hex << std::setw(2) << std::setfill('0')
|
||||||
|
<< static_cast<int>(hash[i]);
|
||||||
}
|
}
|
||||||
return oss.str();
|
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];
|
unsigned char hash[MD5_DIGEST_LENGTH];
|
||||||
MD5(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), hash);
|
MD5(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(),
|
||||||
|
hash);
|
||||||
return to_hex_string(hash, MD5_DIGEST_LENGTH);
|
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);
|
std::ifstream file(filename, std::ios::binary);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw std::runtime_error("Cannot open file: " + filename);
|
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);
|
return to_hex_string(hash, MD5_DIGEST_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
std::string load_dll_dir = fs::path(argv[0]).append("mdsha_dll").string();
|
||||||
|
SetDllDirectory(load_dll_dir.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
std::cerr << "Usage: " << argv[0] << " <mode> <input>" << std::endl;
|
std::cerr << "Usage: " << argv[0] << " <mode> <input>" << 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 << " -s : Calculate MD5 for a string" << std::endl;
|
||||||
std::cerr << " -f : Calculate MD5 for a file" << std::endl;
|
std::cerr << " -f : Calculate MD5 for a file" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
15
sha256.cpp
15
sha256.cpp
@ -5,6 +5,12 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <filesystem>
|
||||||
|
#include <windows.h>
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string to_hex_string(const unsigned char* hash, size_t length) {
|
std::string to_hex_string(const unsigned char* hash, size_t length) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
for (size_t i = 0; i < length; ++i) {
|
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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
std::string load_dll_dir = fs::path(argv[0]).append("mdsha_dll").string();
|
||||||
|
SetDllDirectory(load_dll_dir.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
std::cerr << "Usage: " << argv[0] << " <mode> <input>" << std::endl;
|
std::cerr << "Usage: " << argv[0] << " <mode> <input>" << 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 << " -s : Calculate SHA-256 for a string" << std::endl;
|
||||||
std::cerr << " -f : Calculate SHA-256 for a file" << std::endl;
|
std::cerr << " -f : Calculate SHA-256 for a file" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user