moved md5 to slsSupportlib, added md5_helper.c

This commit is contained in:
2021-09-16 11:34:16 +02:00
parent ba122fe2ad
commit 2ff50750f5
8 changed files with 67 additions and 25 deletions

View File

@ -11,6 +11,8 @@ set(SOURCES
src/UdpRxSocket.cpp
src/sls_detector_exceptions.cpp
# src/sls_detector_defs.cpp
src/md5.c
src/md5_helper.cpp
)
# Header files to install as a part of the library
@ -44,6 +46,8 @@ if(SLS_DEVEL_HEADERS)
include/sls/versionAPI.h
include/sls/ZmqSocket.h
include/sls/bit_utils.h
include/sls/mdf5.h
include/sls/md5_helper.h
)
endif()

View File

@ -222,7 +222,13 @@
# include <stddef.h>
# ifdef __cplusplus
/*
* Modifications 2021 Paul Scherrer Institut
* namespace sls added
*/
namespace sls {
extern "C" {
# endif
@ -372,5 +378,8 @@ int MD5_Final(unsigned char *md, MD5_CTX *c);
void md5_block_data_order(MD5_CTX *c, const void *p, size_t num);
# ifdef __cplusplus
}
} // namespace sls
# endif
# endif

View File

@ -0,0 +1,9 @@
#pragma once
#include "sls/md5.h"
#include <string>
namespace sls {
std::string md5_calculate_checksum(char *buffer, ssize_t bytes);
} // namespace sls

View File

@ -224,13 +224,17 @@
#include <stdio.h>
/**
* Modification 2021 Paul Scherrer Institut
* Header included was md5_local.h
* and included string.h header
/**
* Modification 2021 Paul Scherrer Institut
* Header included was md5_local.h
* and string.h header was included
* sls namespace added
*/
#include "sls/md5.h"
#include <string.h>
#include "md5.h"
#ifdef __cplusplus
namespace sls {
#endif
/**
* Modification 2021 Paul Scherrer Institut
@ -504,4 +508,8 @@ int HASH_FINAL(unsigned char *md, HASH_CTX *c)
HASH_MAKE_STRING(c, md);
return 1;
}
}
#ifdef __cplusplus
} // namespace sls
#endif

View File

@ -0,0 +1,28 @@
#include "sls/md5_helper.h"
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace sls {
std::string md5_calculate_checksum(char *buffer, ssize_t bytes) {
MD5_CTX c;
if (!MD5_Init(&c)) {
throw std::runtime_error(
"Could not calculate md5 checksum.[initializing]");
}
if (!MD5_Update(&c, buffer, bytes)) {
throw std::runtime_error("Could not calculate md5 checksum.[Updating]");
}
unsigned char out[MD5_DIGEST_LENGTH];
if (!MD5_Final(out, &c)) {
throw std::runtime_error("Could not calculate md5 checksum.[Final]");
}
std::ostringstream oss;
for (int i = 0; i != MD5_DIGEST_LENGTH; ++i)
oss << std::hex << std::setw(2) << std::setfill('0') << +out[i];
return oss.str();
}
} // namespace sls