mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
feat: implement MD5 algorithm
This commit is contained in:
parent
d4ee19c6af
commit
bd5cc06b68
5 changed files with 330 additions and 6 deletions
|
|
@ -6,6 +6,24 @@ struct TestContainer
|
|||
uint32_t value[10];
|
||||
};
|
||||
|
||||
class TestClass
|
||||
{
|
||||
public:
|
||||
uint32_t* m_value = nullptr;
|
||||
|
||||
TestClass(uint32_t* value) {
|
||||
this->m_value = value;
|
||||
if (this->m_value) {
|
||||
*this->m_value = 1;
|
||||
}
|
||||
}
|
||||
~TestClass() {
|
||||
if (this->m_value) {
|
||||
*this->m_value = 2;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("CDataAllocator::CDataAllocator", "[dataallocator]") {
|
||||
SECTION("constructs new data allocator") {
|
||||
CDataAllocator allocator(sizeof(TestContainer), 2);
|
||||
|
|
@ -23,6 +41,9 @@ TEST_CASE("CDataAllocator::GetData", "[dataallocator]") {
|
|||
REQUIRE(container2);
|
||||
REQUIRE(container3);
|
||||
REQUIRE(allocator.DataUsed() == 3);
|
||||
allocator.PutData(container, __FILE__, __LINE__);
|
||||
allocator.PutData(container2, __FILE__, __LINE__);
|
||||
allocator.PutData(container3, __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,5 +56,20 @@ TEST_CASE("CDataAllocator::PutData", "[dataallocator]") {
|
|||
allocator.PutData(container, __FILE__, __LINE__);
|
||||
allocator.PutData(container3, __FILE__, __LINE__);
|
||||
REQUIRE(allocator.DataUsed() == 1);
|
||||
allocator.PutData(container2, __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("CDataAllocator Class Handling", "[dataallocator]") {
|
||||
SECTION("test class creation/destruction") {
|
||||
CDataAllocator allocator(sizeof(TestClass), 2);
|
||||
uint32_t value = 0;
|
||||
|
||||
auto testClass = ALLOCATOR_NEW(allocator, TestClass, &value);
|
||||
REQUIRE(testClass->m_value == &value);
|
||||
REQUIRE(value == 1);
|
||||
|
||||
ALLOCATOR_DEL(allocator, testClass);
|
||||
REQUIRE(value == 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
55
test/MD5.cpp
Normal file
55
test/MD5.cpp
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include "common/MD5.hpp"
|
||||
#include "test/Test.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
static bool test_hash(const char* data, const char* expected, int length = -1) {
|
||||
const size_t digestLength = 16;
|
||||
|
||||
uint8_t digest[digestLength] = {};
|
||||
char hex[(2 * digestLength) + 1];
|
||||
|
||||
if (length < 0) {
|
||||
length = strlen(data);
|
||||
}
|
||||
|
||||
/* calculate hash */
|
||||
MD5_CTX context;
|
||||
MD5Init(&context);
|
||||
MD5Update(&context, data, static_cast<size_t>(length));
|
||||
MD5Final(digest, &context);
|
||||
|
||||
/* copy hash to string */
|
||||
for (size_t i = 0; i < digestLength; i++) {
|
||||
sprintf(&(hex[i * 2]), "%02x", digest[i]);
|
||||
}
|
||||
hex[digestLength * 2] = '\0';
|
||||
|
||||
/* compare with result string */
|
||||
return strcmp(hex, expected) == 0;
|
||||
}
|
||||
|
||||
TEST_CASE("MD5", "[md5]") {
|
||||
SECTION("zero data") {
|
||||
REQUIRE(test_hash("", "d41d8cd98f00b204e9800998ecf8427e"));
|
||||
}
|
||||
SECTION("test 1") {
|
||||
REQUIRE(test_hash("a", "0cc175b9c0f1b6a831c399e269772661"));
|
||||
}
|
||||
SECTION("test 2") {
|
||||
REQUIRE(test_hash("abc", "900150983cd24fb0d6963f7d28e17f72"));
|
||||
}
|
||||
SECTION("test 3") {
|
||||
REQUIRE(test_hash(
|
||||
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
|
||||
"57edf4a22be3c955ac49da2e2107b67a"));
|
||||
}
|
||||
SECTION("binary array test") {
|
||||
char data[8192];
|
||||
for (size_t i = 0; i < sizeof(data); ++i) {
|
||||
data[i] = static_cast<char>(i % 256);
|
||||
}
|
||||
test_hash(data, "6556112372898c69e1de0bf689d8db26", sizeof(data));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue