增加SHA256

This commit is contained in:
xx
2025-03-13 16:56:38 +08:00
parent 472433e7fc
commit 026c8fb666
2 changed files with 12 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ namespace ylib
namespace codec namespace codec
{ {
ylib::buffer sha1(const ylib::buffer& data); ylib::buffer sha1(const ylib::buffer& data);
ylib::buffer sha256(const ylib::buffer& data);
std::string md5(const ylib::buffer& data); std::string md5(const ylib::buffer& data);
std::string to_utf8(const std::string& gbk); std::string to_utf8(const std::string& gbk);
std::string to_gbk(const std::string& utf8); std::string to_gbk(const std::string& utf8);

View File

@@ -43,11 +43,17 @@ ylib::buffer ylib::codec::sha1(const ylib::buffer& data)
SHA1_Final((unsigned char*)result.data(), &sha1); SHA1_Final((unsigned char*)result.data(), &sha1);
return result; return result;
//std::stringstream ss; }
//for (int i = 0; i < SHA_DIGEST_LENGTH; i++) { ylib::buffer ylib::codec::sha256(const ylib::buffer& data)
// ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; {
//} ylib::buffer result;
//return ss.str(); // SHA256 输出 32 字节
result.resize(32);
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, data.data(), data.size());
SHA256_Final((unsigned char*)result.data(), &sha256);
return result;
} }
#endif #endif
std::string ylib::codec::to_utf8(const std::string &gbk) std::string ylib::codec::to_utf8(const std::string &gbk)