增加SHA25hmac

This commit is contained in:
xx
2025-03-13 17:11:49 +08:00
parent 026c8fb666
commit 0e13205fce
2 changed files with 14 additions and 0 deletions

View File

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

View File

@@ -26,6 +26,7 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#if USE_OPENSSL== 1
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#endif
#ifdef _WIN32
#include <windows.h>
@@ -55,6 +56,18 @@ ylib::buffer ylib::codec::sha256(const ylib::buffer& data)
SHA256_Final((unsigned char*)result.data(), &sha256);
return result;
}
// 带密钥的 HMAC-SHA256 计算
ylib::buffer ylib::codec::hmac_sha256(const ylib::buffer& key, const ylib::buffer& data)
{
unsigned int len = 32; // HMAC-SHA256 的输出长度为 32 字节
ylib::buffer result;
result.resize(len);
// 使用 HMAC 计算EVP_sha256() 指定使用 SHA256 算法
HMAC(EVP_sha256(), key.data(), key.size(),
(const unsigned char*)data.data(), data.size(),
(unsigned char*)result.data(), &len);
return result;
}
#endif
std::string ylib::codec::to_utf8(const std::string &gbk)
{