scire
Sadh's C++ Impromptu Routines Ensemble
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hash_comn.hpp
Go to the documentation of this file.
1 // scire/hash/hashcomn
2 
3 // Copyright (c) 2014, Khan 'Sadh' Mostafa (http://nafSadh.com/Khan)
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying text at http://www.boost.org/LICENSE_1_0.txt)
6 
7 /**
8  scire/hash/hashcomn.hpp
9 
10  hash common interface and utilities
11  - IHashAlgo : hash algorithm interface
12  - Hash : static utility functions (disabled by NO_HASH_UTILITY)
13 
14  other required scire files:
15  none
16 
17  author:
18  ~nafSadh
19  */
20 #ifndef SCIRE_hash_common_HPP
21 #define SCIRE_hash_common_HPP
22 
23 #ifndef NO_HASH_UTILITY
24 #include <iostream> // std::cout
25 #include <fstream> // std::ifstream
26 #include <cstdint>
27 #endif
28 #include <string>
29 
30 namespace scire
31 {
32 
34 #define SCIRE_HashAlgo_INTFC
35  /**
36  * Common interface for Hash Algorithms
37  */
38  template<typename SzType = size_t, /* size type, integer */
39  typename Ui32t = uint32_t,/* std 32bit unsigned integer */
40  typename ByteT = uint8_t,/* byte type, 8bit unsigned integer */
41  typename SByteT = int8_t/* signed byte type */>
42  class IHashAlgo
43  {
44  public:
45  /**
46  * initialization.
47  */
48  virtual void Init() = 0;
49 
50  /**
51  * block update operation
52  * @param input an array of bytes
53  * @param lenght number of bytes in input chunk
54  */
55  virtual bool Update(const ByteT* input, SzType lenght) = 0;
56  /** @copydoc HashAlgo::Update */
57  virtual bool Update(const SByteT* input, SzType lenght) = 0;
58 
59  /**
60  * final hash compute, prepare digest.
61  */
62  virtual void Final() = 0;
63 
64  /** fingerprint is the 128bit message-digest */
65  virtual std::string Fingerprint() const = 0;
66 
67  /** String representation of Four State Words (ABCD) */
68  virtual std::string StatePhrase() const = 0;
69 
70  /** string reprsentation | fingerpring on finalize, else status words */
71  virtual std::string ToString() const = 0;
72  };
73 #endif//SCIRE_HashAlgo_INTFC
74 
76 
78 #define SCIRE_Hash_FUNCS
79  /** static utility routines for hash algorithms */
80  class Hash
81  {
82  public:
83  /**
84  * Compute hash of msg using passed algo. Digest is to be stored in hash
85  * object. This routine inits hash object and then invokes Update and Final
86  */
87  template<typename SzT, typename U32, typename B8, typename SB8>
88  static void Compute(
89  IHashAlgo<SzT, U32, B8, SB8>& iHA,/**< algo to compute with */
90  std::string message/**< message string to compute hash for */
91  )
92  {
93  iHA.Init();
94  iHA.Update(reinterpret_cast<const B8*>(message.c_str()),message.length());
95  iHA.Final();
96  }
97 
98  enum { BlockSz = 2048 };
99 
100  /**
101  * Compute hash of file using passed algo. Digest is to be stored in hash
102  * object. This routine inits hash object and then invokes Update and Final
103  */
104  template<typename SzT, typename U32, typename B8, typename SB8>
105  static bool Compute(
106  IHashAlgo<SzT, U32, B8, SB8>& iHA,/**< algo to compute with */
107  std::ifstream& ifs/**< std::ifstream object to compute hash for */
108  )
109  {
110  iHA.Init();
111  if (!ifs) return false;
112 
113  // get length of file:
114  ifs.seekg(0, ifs.end);
116  ifs.seekg(0, ifs.beg);
117 
118  //compute
119  std::streamsize processed = 0;
120  char * buffer = new char[BlockSz];
121  bool finished = false;
122 
123  while (!finished && (processed < filesize)) {
124  // decide read size
126  ? (filesize - processed) : BlockSz;
127  // request read from file
128  ifs.read(buffer, readq);
129 
130  // read results
132  if (readc < 0 || !ifs) {
133  finished = true;
134  } else {
135  iHA.Update(reinterpret_cast<const B8*>(buffer), (SzT)readc);
136  processed += readc;
137  }
138  }
139  iHA.Final();
140  return true;
141  }
142 
143  /**
144  * Compute hash of file using passed algo. Digest is to be stored in hash
145  * object. This routine inits hash object and then invokes Update and Final
146  */
147  template<typename SzT, typename U32, typename B8, typename SB8>
148  static bool ComputeFile(
149  IHashAlgo<SzT, U32, B8, SB8>& iHA,/**< algo to compute with */
150  std::string filepath/**< path to file to compute hash for */
151  )
152  {
154  if (!ifs) return false;
155  bool result = Compute(iHA, ifs);
156  ifs.close();
157  return result;
158  }
159  /** @copydoc Hash::ComputeFilre */
160  template<typename SzT, typename U32, typename B8, typename SB8>
161  static bool ComputeFile(
162  IHashAlgo<SzT, U32, B8, SB8>& iHA,/**< algo to compute with */
163  char* filepath/**< path to file to compute hash for */
164  )
165  {
166  return ComputeFile(iHA, std::string(filepath));
167  }
168 
169  };
170 #endif//SCIRE_Hash_FUNCS
171 #endif//YES_HASH_UTILITY
172 }//scire namespace
173 #endif//SCIRE_hash_common_HPP
virtual void Init()=0
initialization.
static void Compute(IHashAlgo< SzT, U32, B8, SB8 > &iHA, std::string message)
Compute hash of msg using passed algo.
Definition: hash_comn.hpp:88
static bool ComputeFile(IHashAlgo< SzT, U32, B8, SB8 > &iHA, std::string filepath)
Compute hash of file using passed algo.
Definition: hash_comn.hpp:148
static utility routines for hash algorithms
Definition: hash_comn.hpp:80
Common interface for Hash Algorithms.
Definition: hash_comn.hpp:42
virtual std::string Fingerprint() const =0
fingerprint is the 128bit message-digest
virtual std::string StatePhrase() const =0
String representation of Four State Words (ABCD)
static bool Compute(IHashAlgo< SzT, U32, B8, SB8 > &iHA, std::ifstream &ifs)
Compute hash of file using passed algo.
Definition: hash_comn.hpp:105
scire/graph/gale_shapley.hpp
virtual void Final()=0
final hash compute, prepare digest.
static bool ComputeFile(IHashAlgo< SzT, U32, B8, SB8 > &iHA, char *filepath)
Definition: hash_comn.hpp:161
virtual std::string ToString() const =0
string reprsentation | fingerpring on finalize, else status words
virtual bool Update(const ByteT *input, SzType lenght)=0
block update operation
virtual bool Update(const SByteT *input, SzType lenght)=0