mirror of
https://github.com/zs-yg/kortapp-z.git
synced 2025-12-06 16:10:42 +08:00
提供更多文本转换模式
This commit is contained in:
45
others/C++/text converter/src/SHA512Converter.cpp
Normal file
45
others/C++/text converter/src/SHA512Converter.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "../include/SHA512Converter.hpp"
|
||||
#include <openssl/evp.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <stdexcept>
|
||||
|
||||
std::string SHA512Converter::convert(const std::string& input) {
|
||||
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
|
||||
if (!mdctx) {
|
||||
throw std::runtime_error("Failed to create EVP_MD_CTX");
|
||||
}
|
||||
|
||||
const EVP_MD* md = EVP_sha512();
|
||||
if (!md) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("SHA512 not supported by this OpenSSL version");
|
||||
}
|
||||
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int digest_len;
|
||||
|
||||
if (EVP_DigestInit_ex(mdctx, md, nullptr) != 1) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Failed to initialize digest");
|
||||
}
|
||||
|
||||
if (EVP_DigestUpdate(mdctx, input.c_str(), input.length()) != 1) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Failed to update digest");
|
||||
}
|
||||
|
||||
if (EVP_DigestFinal_ex(mdctx, digest, &digest_len) != 1) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Failed to finalize digest");
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
std::stringstream ss;
|
||||
for(unsigned int i = 0; i < digest_len; i++) {
|
||||
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
Reference in New Issue
Block a user