mirror of https://github.com/dnomd343/md5sum.git
Dnomd343
8 months ago
12 changed files with 126 additions and 41 deletions
@ -0,0 +1,2 @@ |
|||||
|
/.idea/ |
||||
|
/cmake-build*/ |
@ -0,0 +1,6 @@ |
|||||
|
[submodule "third_party/googletest"] |
||||
|
path = third_party/googletest |
||||
|
url = https://github.com/google/googletest.git |
||||
|
[submodule "third_party/benchmark"] |
||||
|
path = third_party/benchmark |
||||
|
url = https://github.com/google/benchmark.git |
@ -0,0 +1,26 @@ |
|||||
|
cmake_minimum_required(VERSION 3.5) |
||||
|
project(md5sum LANGUAGES CXX) |
||||
|
|
||||
|
option(MD5_ENABLE_TESTING "Enable testing of the md5sum library." ON) |
||||
|
option(MD5_ENABLE_BENCHMARK "Enable benchmark of the md5sum library." ON) |
||||
|
|
||||
|
set(CMAKE_CXX_STANDARD 20) |
||||
|
|
||||
|
add_compile_options(-fno-rtti -fno-exceptions -Wall -Wextra) |
||||
|
|
||||
|
add_library(md5sum STATIC src/md5_core.cc) |
||||
|
target_include_directories(md5sum INTERFACE src/) |
||||
|
|
||||
|
include(third_party/ThirdParty.cmake) |
||||
|
add_library(md5sum::md5sum ALIAS md5sum) |
||||
|
|
||||
|
if (MD5_ENABLE_TESTING) |
||||
|
enable_testing() |
||||
|
add_executable(md5_test test/md5_update.cc) |
||||
|
target_link_libraries(md5_test PRIVATE md5sum::md5sum GTest::gtest_main) |
||||
|
endif() |
||||
|
|
||||
|
if (MD5_ENABLE_BENCHMARK) |
||||
|
add_executable(md5_benchmark benchmark.cc) |
||||
|
target_link_libraries(md5_benchmark PRIVATE md5sum::md5sum benchmark::benchmark_main) |
||||
|
endif() |
@ -0,0 +1,26 @@ |
|||||
|
#include <benchmark/benchmark.h> |
||||
|
|
||||
|
#include "md5.h" |
||||
|
|
||||
|
std::string test_data() { |
||||
|
char data[64]; |
||||
|
for (char i = 0; i < 64; ++i) { |
||||
|
data[i] = i; |
||||
|
} |
||||
|
return {data, data + 64}; |
||||
|
} |
||||
|
|
||||
|
static void BM_MD5_NEXT(benchmark::State &state) { |
||||
|
const auto data = test_data(); |
||||
|
|
||||
|
md5::MD5::md5_ctx c; |
||||
|
md5::MD5::md5_reset(&c); |
||||
|
|
||||
|
for (auto _ : state) { |
||||
|
md5::MD5::md5_update(&c, data.c_str(), 64); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
BENCHMARK(BM_MD5_NEXT); |
||||
|
|
||||
|
BENCHMARK_MAIN(); |
@ -1,29 +0,0 @@ |
|||||
#pragma once |
|
||||
|
|
||||
#include <cstdint> |
|
||||
|
|
||||
namespace md5 { |
|
||||
|
|
||||
constexpr uint32_t MD5_A = 0x67452301; |
|
||||
constexpr uint32_t MD5_B = 0xefcdab89; |
|
||||
constexpr uint32_t MD5_C = 0x98badcfe; |
|
||||
constexpr uint32_t MD5_D = 0x10325476; |
|
||||
|
|
||||
struct md5_ctx { |
|
||||
uint32_t A = MD5_A; |
|
||||
uint32_t B = MD5_B; |
|
||||
uint32_t C = MD5_C; |
|
||||
uint32_t D = MD5_D; |
|
||||
uint64_t size = 0; |
|
||||
}; |
|
||||
|
|
||||
/// Reset md5 ctx with algorithm constants.
|
|
||||
void md5_reset(md5_ctx *ctx); |
|
||||
|
|
||||
/// Update md5 ctx with specified data, note that `len` is a multiple of 64.
|
|
||||
void md5_update(md5_ctx *ctx, const void *buffer, uint64_t len); |
|
||||
|
|
||||
/// Update and end the md5 hash with the specified data, the value of `len` has no limit.
|
|
||||
void md5_final(md5_ctx *ctx, const void *buffer, uint64_t len); |
|
||||
|
|
||||
} // namespace md5
|
|
@ -0,0 +1,40 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <cstdint> |
||||
|
|
||||
|
namespace md5 { |
||||
|
|
||||
|
class MD5 { |
||||
|
public: |
||||
|
MD5() = default; |
||||
|
|
||||
|
public: |
||||
|
static constexpr uint32_t MD5_A = 0x67452301; |
||||
|
static constexpr uint32_t MD5_B = 0xefcdab89; |
||||
|
static constexpr uint32_t MD5_C = 0x98badcfe; |
||||
|
static constexpr uint32_t MD5_D = 0x10325476; |
||||
|
|
||||
|
struct md5_ctx { |
||||
|
uint32_t A = MD5_A; |
||||
|
uint32_t B = MD5_B; |
||||
|
uint32_t C = MD5_C; |
||||
|
uint32_t D = MD5_D; |
||||
|
uint64_t size = 0; |
||||
|
}; |
||||
|
|
||||
|
/// Reset md5 ctx with algorithm constants.
|
||||
|
static void md5_reset(md5_ctx *ctx); |
||||
|
|
||||
|
/// Update md5 ctx with specified data, note that `len` is a multiple of 64.
|
||||
|
static void md5_update(md5_ctx *ctx, const void *buffer, uint64_t len); |
||||
|
|
||||
|
/// Update and end the md5 hash with the specified data, the value of `len` has no limit.
|
||||
|
static void md5_final(md5_ctx *ctx, const void *buffer, uint64_t len); |
||||
|
|
||||
|
private: |
||||
|
md5_ctx ctx_; |
||||
|
char buffer_[64] = {}; |
||||
|
char buffer_size_ = 0; |
||||
|
}; |
||||
|
|
||||
|
} // namespace md5
|
@ -0,0 +1,10 @@ |
|||||
|
if (MD5_ENABLE_TESTING) |
||||
|
add_subdirectory(third_party/googletest/ EXCLUDE_FROM_ALL) |
||||
|
endif() |
||||
|
|
||||
|
if (MD5_ENABLE_BENCHMARK) |
||||
|
set(BENCHMARK_ENABLE_LTO ON) |
||||
|
set(BENCHMARK_ENABLE_TESTING OFF) |
||||
|
set(BENCHMARK_ENABLE_EXCEPTIONS OFF) |
||||
|
add_subdirectory(third_party/benchmark/ EXCLUDE_FROM_ALL) |
||||
|
endif() |
Loading…
Reference in new issue