Browse Source

feat: namespace and cmake support

legacy
Dnomd343 1 year ago
parent
commit
9691765f72
  1. 6
      CMakeLists.txt
  2. 10
      main.cc
  3. 8
      md5.cc
  4. 4
      md5_impl.cc
  5. 4
      md5def.h
  6. 7
      md5sum.h

6
CMakeLists.txt

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.5)
project(md5sum LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
add_library(md5sum STATIC md5.cc md5_impl.cc)

10
main.cc

@ -1,10 +0,0 @@
#include <iostream>
#include "md5sum.h"
int main() {
auto ret = md5sum("dnomd343");
std::cout << ret << std::endl;
auto val = md5file("/Users/jucong.lin/klotski/third_party/md5sum/343.txt");
std::cout << val << std::endl;
}

8
md5.cc

@ -1,9 +1,11 @@
#include "md5sum.h" #include "md5sum.h"
#include "md5def.h" #include "md5def.h"
namespace md5 {
static char hb2hex(uint8_t hb) { static char hb2hex(uint8_t hb) {
hb &= 0xF; hb &= 0xF;
return hb < 10 ? '0' + hb : hb - 10 + 'a'; return char(hb < 10 ? hb + '0' : hb - 10 + 'a');
} }
std::string md5sum(const std::string &dat) { std::string md5sum(const std::string &dat) {
@ -33,8 +35,8 @@ std::string md5file(std::FILE *file) {
MD5_CTX c; MD5_CTX c;
md5_init(&c); md5_init(&c);
size_t len;
uint8_t out[16]; uint8_t out[16];
size_t len = 0;
char buff[BUFSIZ]; char buff[BUFSIZ];
while ((len = std::fread(buff, sizeof(char), BUFSIZ, file)) > 0) { while ((len = std::fread(buff, sizeof(char), BUFSIZ, file)) > 0) {
md5_update(&c, buff, len); md5_update(&c, buff, len);
@ -48,3 +50,5 @@ std::string md5file(std::FILE *file) {
} }
return res; return res;
} }
} // namespace md5

4
md5_impl.cc

@ -23,6 +23,8 @@
#define GET(n) (ctx->block[(n)]) #define GET(n) (ctx->block[(n)])
#endif #endif
namespace md5 {
static const void* md5_body(MD5_CTX *ctx, const void *data, uint32_t size) { static const void* md5_body(MD5_CTX *ctx, const void *data, uint32_t size) {
const auto *ptr = (uint8_t*)data; const auto *ptr = (uint8_t*)data;
@ -208,3 +210,5 @@ void md5_bin(const void *dat, size_t len, uint8_t out[16]) {
md5_update(&c, dat, len); md5_update(&c, dat, len);
md5_final(out, &c); md5_final(out, &c);
} }
} // namespace md5

4
md5def.h

@ -1,5 +1,7 @@
#pragma once #pragma once
namespace md5 {
typedef struct { typedef struct {
uint32_t lo, hi; uint32_t lo, hi;
uint32_t a, b, c, d; uint32_t a, b, c, d;
@ -11,3 +13,5 @@ void md5_init(MD5_CTX *ctx);
void md5_final(uint8_t *result, MD5_CTX *ctx); void md5_final(uint8_t *result, MD5_CTX *ctx);
void md5_update(MD5_CTX *ctx, const void *data, uint32_t size); void md5_update(MD5_CTX *ctx, const void *data, uint32_t size);
void md5_bin(const void *dat, size_t len, uint8_t out[16]); void md5_bin(const void *dat, size_t len, uint8_t out[16]);
} // namespace md5

7
md5sum.h

@ -1,12 +1,13 @@
#ifndef MD5_H #pragma once
#define MD5_H
#include <string> #include <string>
namespace md5 {
std::string md5sum(const std::string &dat); std::string md5sum(const std::string &dat);
std::string md5sum(const void *dat, size_t len); std::string md5sum(const void *dat, size_t len);
std::string md5file(std::FILE *file); std::string md5file(std::FILE *file);
std::string md5file(const char *filename); std::string md5file(const char *filename);
#endif // end of MD5_H } // namespace md5

Loading…
Cancel
Save