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 
63 
67 template<> class MHO_MD5HashGeneratorSingleType< std::string >
68 {
69  public:
72 
74  {
75  char ch;
76  for(unsigned int i = 0; i < obj.size(); i++)
77  {
78  ch = obj.at(i);
79  _picohash_md5_update(s.GetMD5CTXPtr(), reinterpret_cast< const void* >(&ch), sizeof(char));
80  }
81  return s.Self();
82  }
83 
84  protected:
98  virtual MHO_MD5HashGenerator& Self() = 0;
99 };
100 
105 template< typename... XValueTypeS > class MHO_MD5HashGeneratorMultiType;
106 
113 template< typename XValueType >
114 class MHO_MD5HashGeneratorMultiType< XValueType >: public MHO_MD5HashGeneratorSingleType< XValueType >
115 {};
116 
121 template< typename XValueType, typename... XValueTypeS >
122 class MHO_MD5HashGeneratorMultiType< XValueType, XValueTypeS... >: public MHO_MD5HashGeneratorMultiType< XValueType >,
123  public MHO_MD5HashGeneratorMultiType< XValueTypeS... >
124 {};
125 
126 //construct the multi-type streamer for most basic POD types
127 typedef MHO_MD5HashGeneratorMultiType< bool, char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long,
128  long long, unsigned long long, float, double, long double, std::string >
130 
135 {
136  public:
138 
140 
144  void Initialize()
145  {
146  _picohash_md5_init(&fHashStruct);
147  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
148  {
149  fDigest[i] = 0;
150  }
151  }
152 
156  void Finalize() { _picohash_md5_final(&fHashStruct, (void*)fDigest); }
157 
163  std::string GetDigest()
164  {
165  std::stringstream ss;
166  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
167  {
168  uint32_t tmp = fDigest[i];
169  std::stringstream hss;
170  hss << std::setw(2) << std::setfill('0') << std::hex << (int)(tmp);
171  std::string hexstr = hss.str();
172  ss << hexstr;
173  }
174  return ss.str();
175  }
176 
183  {
184  MHO_UUID uuid;
185  for(unsigned int i = 0; i < PICOHASH_MD5_DIGEST_LENGTH; i++)
186  {
187  uuid[i] = fDigest[i];
188  }
189  return uuid;
190  }
191 
192  protected:
199  virtual _picohash_md5_ctx_t* GetMD5CTXPtr() override { return &fHashStruct; }
200 
206  MHO_MD5HashGenerator& Self() override { return *this; }
207 
210 };
211 
212 } // namespace hops
213 
214 #endif
Class MHO_MD5HashGeneratorMultiType declares a multi-type streamer with a variadic template parameter...
Definition: MHO_MD5HashGenerator.hh:105
Class MHO_MD5HashGeneratorSingleType<std::string> specialization for string type.
Definition: MHO_MD5HashGenerator.hh:68
virtual _picohash_md5_ctx_t * GetMD5CTXPtr()=0
Getter for md5ctxptr.
MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:70
virtual ~MHO_MD5HashGeneratorSingleType()
Definition: MHO_MD5HashGenerator.hh:71
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:73
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:135
uint8_t fDigest[PICOHASH_MD5_DIGEST_LENGTH]
Definition: MHO_MD5HashGenerator.hh:209
MHO_MD5HashGenerator()
Definition: MHO_MD5HashGenerator.hh:137
void Finalize()
Finalizes the MD5 hash calculation and stores the result in fDigest.
Definition: MHO_MD5HashGenerator.hh:156
std::string GetDigest()
Getter for digest.
Definition: MHO_MD5HashGenerator.hh:163
MHO_MD5HashGenerator & Self() override
Returns a reference to the current instance of MHO_MD5HashGenerator.
Definition: MHO_MD5HashGenerator.hh:206
MHO_UUID GetDigestAsUUID()
Getter for digest as uuid.
Definition: MHO_MD5HashGenerator.hh:182
_picohash_md5_ctx_t fHashStruct
Definition: MHO_MD5HashGenerator.hh:208
virtual _picohash_md5_ctx_t * GetMD5CTXPtr() override
Getter for md5ctxptr.
Definition: MHO_MD5HashGenerator.hh:199
void Initialize()
Recursive function to initialize a game state.
Definition: MHO_MD5HashGenerator.hh:144
virtual ~MHO_MD5HashGenerator()
Definition: MHO_MD5HashGenerator.hh:139
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
Definition: MHO_ChannelLabeler.hh:17
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:129
#define PICOHASH_MD5_DIGEST_LENGTH
Definition: picohash.h:44
Definition: picohash.h:46