Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MD5.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/Has/MD5.cpp --------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
18 #include <algorithm>
19 #include <iomanip>
20 #include <iterator>
21 #include <sstream>
22 
23 #ifdef SERIALBOX_HAS_OPENSSL
24 #include <openssl/md5.h>
25 #endif
26 
27 namespace serialbox {
28 
29 const char* MD5::Name = "MD5";
30 
31 std::string MD5::hash(const void* data, int length) {
32  std::string hash;
33 
34 #ifdef SERIALBOX_HAS_OPENSSL
35 
36  unsigned char digest[MD5_DIGEST_LENGTH];
37  ::MD5((unsigned char*)data, length, digest);
38 
39  std::ostringstream ss;
40  ss << std::hex << std::setfill('0') << std::setw(2) << std::uppercase;
41  std::copy(digest, digest + MD5_DIGEST_LENGTH, std::ostream_iterator<int>(ss));
42  hash = ss.str();
43 
44 #else
45  throw Exception("MD5 hash is only available with OpenSSL support");
46 #endif
47 
48  return hash;
49 }
50 
51 } // namespace serialbox
virtual std::string hash(const void *data, int length) override
Compute 128 bit hash using MD5.
Definition: MD5.cpp:31
Namespace of the serialbox library.
Definition: Archive.h:25
static const char * Name
Identifier of the hash.
Definition: MD5.h:31
Implementation of MD5 cryptographic hash algorithm.
Definition: MD5.h:28
Exception class which stores a human-readable error description.
Definition: Exception.h:30