初始化内容
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* @file test_crypto.cpp
|
||||
* @brief memory 模块单元测试(Crypto 加密哈希)
|
||||
*
|
||||
*
|
||||
* @version 1.0
|
||||
* @author CodeBuddy
|
||||
* @date 26-09-03
|
||||
*
|
||||
* @note
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "memory/dl_crypto.h"
|
||||
#include "base/dl_string.h"
|
||||
|
||||
TEST(CryptoTest, HashMd5)
|
||||
{
|
||||
std::string str = "abc";
|
||||
dl::Buffer hash = dl::Crypto::HashMd5(dl::CreateBufferView(str));
|
||||
EXPECT_EQ(dl::to_string_hex(dl::CreateBufferView(hash)), "900150983cd24fb0d6963f7d28e17f72");
|
||||
}
|
||||
|
||||
TEST(CryptoTest, HashSha256)
|
||||
{
|
||||
std::string str = "abc";
|
||||
dl::Buffer hash = dl::Crypto::HashSha256(dl::CreateBufferView(str));
|
||||
EXPECT_EQ(dl::to_string_hex(dl::CreateBufferView(hash)),
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
}
|
||||
|
||||
TEST(CryptoTest, HashSha512)
|
||||
{
|
||||
std::string str = "abc";
|
||||
dl::Buffer hash = dl::Crypto::HashSha512(dl::CreateBufferView(str));
|
||||
// sha512 结果固定长度 64 字节
|
||||
EXPECT_EQ(hash.size(), 64u);
|
||||
}
|
||||
|
||||
TEST(CryptoTest, HashCrc32)
|
||||
{
|
||||
std::string str = "abc";
|
||||
uint32_t crc = dl::Crypto::HashCrc32(dl::CreateBufferView(str));
|
||||
EXPECT_NE(crc, 0u);
|
||||
// 同一数据结果一致
|
||||
EXPECT_EQ(crc, dl::Crypto::HashCrc32(dl::CreateBufferView(str)));
|
||||
}
|
||||
|
||||
TEST(CryptoTest, Base64)
|
||||
{
|
||||
std::string str = "hello world";
|
||||
std::string enc = dl::Crypto::EncodeBase64(dl::CreateBufferView(str));
|
||||
EXPECT_EQ(enc, "aGVsbG8gd29ybGQ=");
|
||||
|
||||
dl::Buffer dec;
|
||||
EXPECT_TRUE(dl::Crypto::DecodeBase64(enc, dec));
|
||||
EXPECT_EQ(dl::to_string(dec), str);
|
||||
}
|
||||
|
||||
TEST(CryptoTest, Xor)
|
||||
{
|
||||
std::string str = "hello dl";
|
||||
dl::Buffer buf = dl::CreateBuffer(dl::CreateBufferView(str));
|
||||
dl::BufferView view = dl::CreateBufferView(buf);
|
||||
|
||||
dl::Crypto::EncryptXor(view);
|
||||
EXPECT_NE(dl::to_string(view), str); // 加密后应不同
|
||||
|
||||
dl::Crypto::DecryptXor(view);
|
||||
EXPECT_EQ(dl::to_string(view), str); // 解密后还原
|
||||
}
|
||||
|
||||
TEST(CryptoTest, Aes256)
|
||||
{
|
||||
std::string str = "hello dl aes256";
|
||||
dl::Crypto::Aes256Key key = dl::Crypto::GenerateKeyAes256();
|
||||
|
||||
dl::Buffer enc = dl::Crypto::EncryptAes256(dl::CreateBufferView(str), key);
|
||||
EXPECT_FALSE(enc.empty());
|
||||
|
||||
dl::Buffer dec = dl::Crypto::DecryptAes256(enc, key);
|
||||
EXPECT_EQ(dl::to_string(dec), str);
|
||||
}
|
||||
Reference in New Issue
Block a user