fix:修正搜索路径。
This commit is contained in:
parent
4fe60e008b
commit
913ad63188
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@ -96,6 +96,14 @@
|
||||
"ratio": "cpp",
|
||||
"sstream": "cpp",
|
||||
"string_view": "cpp",
|
||||
"vector": "cpp"
|
||||
"vector": "cpp",
|
||||
"array": "cpp",
|
||||
"functional": "cpp",
|
||||
"hash_map": "cpp",
|
||||
"set": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"xhash": "cpp",
|
||||
"xtree": "cpp",
|
||||
"span": "cpp"
|
||||
}
|
||||
}
|
11
md5.cpp
11
md5.cpp
@ -10,6 +10,15 @@
|
||||
#include <filesystem>
|
||||
#include <windows.h>
|
||||
namespace fs = std::filesystem;
|
||||
std::string GetExecutablePath()
|
||||
{
|
||||
char buffer[MAX_PATH];
|
||||
DWORD length = GetModuleFileName(nullptr, buffer, MAX_PATH);
|
||||
if (length == 0) {
|
||||
return "";
|
||||
}
|
||||
return std::string(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string to_hex_string(const unsigned char* hash, size_t length)
|
||||
@ -57,7 +66,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
std::string load_dll_dir = fs::path(argv[0]).append("mdsha_dll").string();
|
||||
std::string load_dll_dir = fs::path(GetExecutablePath()).parent_path().append("mdsha_dll").string();
|
||||
SetDllDirectory(load_dll_dir.c_str());
|
||||
#endif
|
||||
|
||||
|
36
sha256.cpp
36
sha256.cpp
@ -1,7 +1,7 @@
|
||||
#include <openssl/sha.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <openssl/sha.h>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
@ -9,23 +9,37 @@
|
||||
#include <filesystem>
|
||||
#include <windows.h>
|
||||
namespace fs = std::filesystem;
|
||||
std::string GetExecutablePath()
|
||||
{
|
||||
char buffer[MAX_PATH];
|
||||
DWORD length = GetModuleFileName(nullptr, buffer, MAX_PATH);
|
||||
if (length == 0) {
|
||||
return "";
|
||||
}
|
||||
return std::string(buffer);
|
||||
}
|
||||
#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;
|
||||
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();
|
||||
}
|
||||
|
||||
std::string sha256_string(const std::string& input) {
|
||||
std::string sha256_string(const std::string& input)
|
||||
{
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
SHA256(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), hash);
|
||||
SHA256(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(),
|
||||
hash);
|
||||
return to_hex_string(hash, SHA256_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
std::string sha256_file(const std::string& filename) {
|
||||
std::string sha256_file(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::binary);
|
||||
if (!file) {
|
||||
throw std::runtime_error("Cannot open file: " + filename);
|
||||
@ -46,11 +60,12 @@ std::string sha256_file(const std::string& filename) {
|
||||
return to_hex_string(hash, SHA256_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();
|
||||
std::string load_dll_dir = fs::path(GetExecutablePath()).parent_path().append("mdsha_dll").string();
|
||||
SetDllDirectory(load_dll_dir.c_str());
|
||||
#endif
|
||||
|
||||
@ -67,7 +82,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
try {
|
||||
if (mode == "-s") {
|
||||
std::cout << "SHA-256(string): " << sha256_string(input) << std::endl;
|
||||
std::cout << "SHA-256(string): " << sha256_string(input)
|
||||
<< std::endl;
|
||||
} else if (mode == "-f") {
|
||||
std::cout << "SHA-256(file): " << sha256_file(input) << std::endl;
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user