ewmscp  ..
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
checksum::md5sum Class Reference

class to calculate md5sums. More...

#include <md5sum.h>

Inheritance diagram for checksum::md5sum:
[legend]
Collaboration diagram for checksum::md5sum:
[legend]

Public Member Functions

 md5sum (const std::string &name)
 
void update (void *data, size_t size) override
 proces size bytes in the block at data More...
 
void update (size_t size) override
 proces size many zero bytes More...
 
void finish () override
 finish calculation ad write to result More...
 
- Public Member Functions inherited from checksum::base
virtual ~base ()=default
 
virtual bool parallelizable () const
 
const std::string & getName () const
 
const std::string & getResult () const
 
void setExpectedResult (const std::string &expectedResult)
 
const std::string & getExpectedResult () const
 
bool checkExpectation () const
 

Private Attributes

struct md5_ctx ctx
 

Static Private Attributes

static factoryTemplate< md5sumfactory
 

Additional Inherited Members

- Static Public Member Functions inherited from checksum::base
static factoryClassnewFactory (const std::string &aName)
 
template<class T >
static void addAllowedNamesToOption (T &option)
 
- Protected Member Functions inherited from checksum::base
 base (const std::string &aName)
 
- Static Protected Member Functions inherited from checksum::base
static std::map< std::string, factoryClass * > & getFactoryMap ()
 
- Protected Attributes inherited from checksum::base
const std::string & name
 
std::string result
 
std::string expected
 

Detailed Description

class to calculate md5sums.

Wrappers either the openssl implementation or a copy of the md5 implementation from the gnulib as it is used in the md5sum command.

Definition at line 18 of file md5sum.h.

Constructor & Destructor Documentation

◆ md5sum()

checksum::md5sum::md5sum ( const std::string &  name)

Definition at line 10 of file md5sum.cpp.

10  : base(aName) {
11  #ifdef USE_OPENSSL
12  ctx = EVP_MD_CTX_create();
13  mdType = EVP_md5();
14  EVP_MD_CTX_init(ctx);
15  EVP_DigestInit_ex(ctx, mdType, nullptr);
16  #else
18  #endif
19  }

References __md5_init_ctx, and ctx.

Member Function Documentation

◆ finish()

void checksum::md5sum::finish ( )
overridevirtual

finish calculation ad write to result

Implements checksum::base.

Definition at line 70 of file md5sum.cpp.

70  {
71  #ifdef USE_OPENSSL
72  std::vector<unsigned char> md(EVP_MD_size(mdType));
73  EVP_DigestFinal_ex(ctx, md.data(), nullptr);
74  EVP_MD_CTX_cleanup(ctx);
75  EVP_MD_CTX_destroy(ctx);
76  #else
77  unsigned char md[16];
78  __md5_finish_ctx(&ctx, md);
79  #endif
80  std::ostringstream hashBuffer;
81 
82  for (auto c : md) {
83  hashBuffer << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(c);
84  }
85 
86  result = hashBuffer.str();
87  }

References __md5_finish_ctx, ctx, and checksum::base::result.

◆ update() [1/2]

void checksum::md5sum::update ( size_t  size)
overridevirtual

proces size many zero bytes

Implements checksum::base.

Definition at line 44 of file md5sum.cpp.

44  { // case for block of zeroes
45  #ifndef USE_OPENSSL
46  if ((size & (64 - 1)) == 0) {
47  __md5_process_zero(size, &ctx);
48  } else
49  #endif
50  {
51  for (size_t bytes_done = 0; bytes_done < size;) {
52  auto missing = size - bytes_done;
53  auto bytes = std::min(missing, block::nullBufferSize());
54  #ifdef USE_OPENSSL
55  EVP_DigestUpdate(ctx, block::nullBuffer(), bytes);
56  #else
57 
58  if (size & (64 - 1)) { // use processor that needs no 64Byte size
60  } else {
61  __md5_process_zero(bytes, &ctx);
62  }
63 
64  #endif
65  bytes_done += bytes;
66  }
67  }
68  }

References __md5_process_bytes, __md5_process_zero, ctx, block::nullBuffer(), and block::nullBufferSize().

Here is the call graph for this function:

◆ update() [2/2]

void checksum::md5sum::update ( void *  data,
size_t  size 
)
overridevirtual

proces size bytes in the block at data

Implements checksum::base.

Definition at line 20 of file md5sum.cpp.

20  {
21  for (size_t bytes_done = 0; bytes_done < size;) {
22  size_t bytes = 4096;
23  auto missing = size - bytes_done;
24 
25  if (missing < bytes) {
26  bytes = missing;
27  }
28 
29  auto dataStart = static_cast<void*>(static_cast<char*>(data) + bytes_done);
30  #ifdef USE_OPENSSL
31  EVP_DigestUpdate(ctx, dataStart, bytes);
32  #else
33 
34  if (size & (64 - 1)) { // use processor that needs no 64Byte size
35  __md5_process_bytes(dataStart, bytes, &ctx);
36  } else {
37  __md5_process_block(dataStart, bytes, &ctx);
38  }
39 
40  #endif
41  bytes_done += bytes;
42  }
43  }

References __md5_process_block, __md5_process_bytes, and ctx.

Member Data Documentation

◆ ctx

struct md5_ctx checksum::md5sum::ctx
private

Definition at line 25 of file md5sum.h.

Referenced by finish(), md5sum(), and update().

◆ factory

factoryTemplate<md5sum> checksum::md5sum::factory
staticprivate

Definition at line 20 of file md5sum.h.


The documentation for this class was generated from the following files:
block::nullBuffer
static const void * nullBuffer()
Definition: block.cpp:37
checksum::md5sum::ctx
struct md5_ctx ctx
Definition: md5sum.h:25
__md5_process_bytes
#define __md5_process_bytes
Definition: md5.h:56
__md5_finish_ctx
#define __md5_finish_ctx
Definition: md5.h:52
__md5_process_block
#define __md5_process_block
Definition: md5.h:55
checksum::base::base
base(const std::string &aName)
Definition: checksumBase.h:40
block::nullBufferSize
static size_t nullBufferSize()
Definition: block.cpp:34
checksum::base::result
std::string result
Definition: checksumBase.h:38
__md5_init_ctx
#define __md5_init_ctx
Definition: md5.h:53
__md5_process_zero
#define __md5_process_zero
Definition: md5.h:54