HOPS
HOPS class reference
MHO_MD5HashGenerator.hh
Go to the documentation of this file.
1 #ifndef MHO_MD5HashGenerator_HH__
2 #define MHO_MD5HashGenerator_HH__
3 
4 #include <complex>
5 #include <cstddef>
6 #include <fstream>
7 #include <iomanip>
8 #include <iostream>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <string>
12 
13 #include "MHO_UUID.hh"
14 #include "picohash.h"
15 
16 namespace hops
17 {
18 
27 //forward declare our md5 hashing streamer (for plain old data types ((POD))
28 class MHO_MD5HashGenerator;
29 
33 template< typename XValueType > class MHO_MD5HashGeneratorSingleType
34 {
35  public:
38 
40  {
41  //run the hash update
42  _picohash_md5_update(s.GetMD5CTXPtr(), reinterpret_cast< const void* >(&obj), sizeof(XValueType));
43  return s.Self();
44  }
45 
46  protected:
60  virtual MHO_MD5HashGenerator& Self() = 0;
61 };
62 
66 template<> class MHO_MD5HashGeneratorSingleType< std::string >
67 {
68  public:
71 
73  {
74  char ch;
75  for(unsigned int i = 0; i < obj.size(); i++)
76  {
77  ch = obj.at(i);
78  _picohash_md5_update(s.GetMD5CTXPtr(), reinterpret_cast< const void* >(&ch), sizeof(char));
79  }
80  return s.Self();
81  }
82 
83  protected:
97  virtual MHO_MD5HashGenerator& Self() = 0;
98 };
99 
104 template< typename... XValueTypeS > class MHO_MD5HashGeneratorMultiType;
105 
112 template< typename XValueType >
113 class MHO_MD5HashGeneratorMultiType< XValueType >: public MHO_MD5HashGeneratorSingleType< XValueType >
114 {};
115 
120 template< typename XValueType, typename... XValueTypeS >
121 class MHO_MD5HashGeneratorMultiType< XValueType, XValueTypeS... >: public MHO_MD5HashGeneratorMultiType< XValueType >,
122  public MHO_MD5HashGeneratorMultiType< XValueTypeS... >
123 {};
124 
125 //construct the multi-type streamer for most basic POD types
126 typedef MHO_MD5HashGeneratorMultiType< bool, char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long,
127  long long, unsigned long long, float, double, long double, std::string >
129 
134 {
135  public:
137 
139 
143  void Initialize()
144  {
145  _picohash_md5_init(&fHashStruct);
146  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
147  {
148  fDigest[i] = 0;
149  }
150  }
151 
155  void Finalize() { _picohash_md5_final(&fHashStruct, (void*)fDigest); }
156 
162  std::string GetDigest()
163  {
164  std::stringstream ss;
165  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
166  {
167  uint32_t tmp = fDigest[i];
168  std::stringstream hss;
169  hss << std::setw(2) << std::setfill('0') << std::hex << (int)(tmp);
170  std::string hexstr = hss.str();
171  ss << hexstr;
172  }
173  return ss.str();
174  }
175 
182  {
183  MHO_UUID uuid;
184  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
185  {
186  uuid[i] = fDigest[i];
187  }
188  return uuid;
189  }
190 
191  protected:
198  virtual _picohash_md5_ctx_t* GetMD5CTXPtr() override { return &fHashStruct; }
199 
205  MHO_MD5HashGenerator& Self() override { return *this; }
206 
209 };
210 
211 } // namespace hops
212 
213 #endif
Class MHO_MD5HashGeneratorMultiType declares a multi-type streamer with a variadic template parameter...
Definition: MHO_MD5HashGenerator.hh:104
Class MHO_MD5HashGeneratorSingleType<std::string> specialization for string type.
Definition: MHO_MD5HashGenerator.hh:67
virtual _picohash_md5_ctx_t * GetMD5CTXPtr()=0
Getter for md5ctxptr.
MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:69
virtual ~MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:70
virtual MHO_MD5HashGenerator & Self()=0
Returns a reference to the current instance of MHO_MD5HashGenerator.
friend MHO_MD5HashGenerator & operator<<(MHO_MD5HashGeneratorSingleType< std::string > &s, std::string &obj)
Definition: MHO_MD5HashGenerator.hh:72
Class MHO_MD5HashGeneratorSingleType - template class for a single-type streamer, generic for most PO...
Definition: MHO_MD5HashGenerator.hh:34
virtual MHO_MD5HashGenerator & Self()=0
Returns a reference to the current instance of MHO_MD5HashGenerator.
friend MHO_MD5HashGenerator & operator<<(MHO_MD5HashGeneratorSingleType< XValueType > &s, const XValueType &obj)
Definition: MHO_MD5HashGenerator.hh:39
virtual _picohash_md5_ctx_t * GetMD5CTXPtr()=0
Getter for md5ctxptr.
MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:36
virtual ~MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:37
Class MHO_MD5HashGenerator declares the concrete class which does the work for file streams.
Definition: MHO_MD5HashGenerator.hh:134
uint8_t fDigest[PICOHASH_MD5_DIGEST_LENGTH]
Definition: MHO_MD5HashGenerator.hh:208
MHO_MD5HashGenerator()
Definition: MHO_MD5HashGenerator.hh:136
void Finalize()
Finalizes the MD5 hash calculation and stores the result in fDigest.
Definition: MHO_MD5HashGenerator.hh:155
std::string GetDigest()
Getter for digest.
Definition: MHO_MD5HashGenerator.hh:162
MHO_MD5HashGenerator & Self() override
Returns a reference to the current instance of MHO_MD5HashGenerator.
Definition: MHO_MD5HashGenerator.hh:205
MHO_UUID GetDigestAsUUID()
Getter for digest as uuid.
Definition: MHO_MD5HashGenerator.hh:181
_picohash_md5_ctx_t fHashStruct
Definition: MHO_MD5HashGenerator.hh:207
virtual _picohash_md5_ctx_t * GetMD5CTXPtr() override
Getter for md5ctxptr.
Definition: MHO_MD5HashGenerator.hh:198
void Initialize()
Recursive function to initialize a game state.
Definition: MHO_MD5HashGenerator.hh:143
virtual ~MHO_MD5HashGenerator()
Definition: MHO_MD5HashGenerator.hh:138
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
Definition: MHO_AdhocFlagging.hh:18
MHO_MD5HashGeneratorMultiType< bool, char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, long double, std::string > MHO_MD5HashGeneratorBasicTypes
Definition: MHO_MD5HashGenerator.hh:128
#define PICOHASH_MD5_DIGEST_LENGTH
Definition: picohash.h:44
Definition: picohash.h:46