AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
hash.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_CRYPTO_HASH_H_
21 #define _ANCH_CRYPTO_HASH_H_
22 
23 #include <iostream>
24 #include <ostream>
25 #include <iomanip>
26 #include <array>
27 
28 
29 namespace anch {
30  namespace crypto {
31 
40  template<std::size_t O, std::size_t B>
41  class Hash {
42 
43  // Methods +
44  public:
50  virtual const std::array<uint8_t,O>& digest() const = 0;
51 
59  template<class CharT, class Traits, class Allocator>
60  const std::array<uint8_t,O>& digest(const std::basic_string<CharT,Traits,Allocator>& data) {
61  reset();
62  addData(reinterpret_cast<const uint8_t*>(data.data()), data.length());
63  finalize();
64  return digest();
65  }
66 
74  template<std::size_t N>
75  const std::array<uint8_t,O>& digest(const std::array<uint8_t,N>& data) {
76  reset();
77  addData(data.data(), N);
78  finalize();
79  return digest();
80  }
81 
90  const std::array<uint8_t,O>& digest(const uint8_t* data, std::size_t len) {
91  reset();
92  addData(data, len);
93  finalize();
94  return digest();
95  }
96 
104  template<class CharT, class Traits>
105  const std::array<uint8_t,O>& digest(std::basic_istream<CharT,Traits>& stream) {
106  reset();
107  if(stream) {
108  char data[1024];
109  while(!stream.eof()) {
110  stream.read(data, 1024);
111  addData(reinterpret_cast<uint8_t*>(data), stream.gcount());
112  }
113  finalize();
114  }
115  return digest();
116  }
117 
118  protected:
122  virtual void reset() = 0;
123 
130  virtual void addData(const uint8_t* data, std::size_t len) = 0;
131 
135  virtual void finalize() = 0;
136  // Methods -
137 
138 
139  // Accessors +
140  public:
146  inline static constexpr std::size_t getOutputSize() {
147  return O;
148  }
149 
155  inline static constexpr std::size_t getBlockSize() {
156  return B;
157  }
158  // Accessors -
159 
160  };
161 
162  }
163 }
164 
174 template<class CharT, class Traits, std::size_t O, std::size_t B>
175 std::basic_ostream<CharT, Traits>&
176 operator << (std::basic_ostream<CharT, Traits>& out, const anch::crypto::Hash<O,B>& hash) {
177  std::ios_base::fmtflags flags = out.flags(); // Save current flags
178  out << std::hex;
179  for(const uint8_t& byte : hash.digest()) {
180  out << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(byte);
181  }
182  out.flags(flags); // Restore flags
183  return out;
184 }
185 
186 #endif // _ANCH_CRYPTO_HASH_H_
virtual const std::array< uint8_t, O > & digest() const =0
const std::array< uint8_t, O > & digest(std::basic_istream< CharT, Traits > &stream)
Definition: hash.hpp:105
static constexpr std::size_t getBlockSize()
Definition: hash.hpp:155
AnCH framework base namespace.
Definition: base64.hpp:28
const std::array< uint8_t, O > & digest(const uint8_t *data, std::size_t len)
Definition: hash.hpp:90
static constexpr std::size_t getOutputSize()
Definition: hash.hpp:146
const std::array< uint8_t, O > & digest(const std::basic_string< CharT, Traits, Allocator > &data)
Definition: hash.hpp:60
Hash algorithm abstract class.
Definition: hash.hpp:41
virtual void reset()=0
virtual void addData(const uint8_t *data, std::size_t len)=0
virtual void finalize()=0
const std::array< uint8_t, O > & digest(const std::array< uint8_t, N > &data)
Definition: hash.hpp:75