AnCH Framework  0.1
Another C++ Hack Framework
md5.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_MD5_H_
21 #define _ANCH_CRYPTO_MD5_H_
22 
23 #include "crypto/hash/hash.hpp"
24 
25 #include <cstring>
26 
27 
28 namespace anch {
29  namespace crypto {
30 
31  template<typename H> H HMAC(const std::string&, const std::string&);
32 
40  class MD5: public Hash<16,64> {
41 
42  friend MD5 anch::crypto::HMAC<MD5>(const std::string&, const std::string&);
43 
44  private:
50  struct Context {
52  uint32_t handle[2];
53 
55  uint32_t buffer[4];
56 
58  uint32_t input[16];
59 
61  std::array<uint8_t,16> digest;
62 
66  Context() {
67  reset();
68  }
69 
73  void reset() {
74  static uint32_t HANDLE_INIT[2] = { 0, 0 };
75  std::memcpy(handle, HANDLE_INIT, 2 * sizeof(uint32_t));
76  static uint32_t BUFFER_INIT[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, };
77  std::memcpy(buffer, BUFFER_INIT, 4 * sizeof(uint32_t));
78  }
79  };
80 
81  // Attributes +
82  private:
84  Context _context;
85  // Attributes -
86 
87  // Constructors +
88  public:
92  MD5();
93 
99  template<class CharT, class Traits, class Allocator>
100  MD5(const std::basic_string<CharT,Traits,Allocator>& data) {
101  Hash::digest(data);
102  }
103 
109  template<class CharT, class Traits>
110  MD5(std::basic_istream<CharT,Traits>& stream) {
111  Hash::digest(stream);
112  }
113 
114  private:
121  MD5(const uint8_t* data, std::size_t len) {
122  Hash::digest(data, len);
123  }
124  // Constructors -
125 
126  // Destructor +
127  public:
131  virtual ~MD5();
132  // Destructor -
133 
134  // Methods +
135  public:
141  virtual const std::array<uint8_t,16>& digest() const override;
142 
143  protected:
147  virtual void reset() override {
148  _context.reset();
149  }
150 
157  virtual void addData(const uint8_t* data, std::size_t len) override;
158 
162  virtual void finalize() override;
163 
164  private:
168  void transform();
169 
179  template<class Core>
180  static inline void transform(uint32_t& a,
181  uint32_t b,
182  uint32_t c,
183  uint32_t d,
184  uint32_t in,
185  int bits) {
186  a += Core::apply(b,c,d) + in;
187  a = (a << bits | a >> (32 - bits)) + b;
188  }
189  // Methods -
190 
191  // Core functions +
192  private:
193  class Core1 {
194  public:
195  inline static uint32_t apply(uint32_t a, uint32_t b, uint32_t c) {
196  return (c ^ (a & (b ^ c)));
197  }
198  };
199 
200  class Core2 {
201  public:
202  inline static uint32_t apply(uint32_t a, uint32_t b, uint32_t c) {
203  return Core1::apply(c, a, b);
204  }
205  };
206 
207  class Core3 {
208  public:
209  inline static uint32_t apply(uint32_t a, uint32_t b, uint32_t c) {
210  return (a ^ b ^ c);
211  }
212  };
213 
214  class Core4 {
215  public:
216  inline static uint32_t apply(uint32_t a, uint32_t b, uint32_t c) {
217  return (b ^ (a | ~c));
218  }
219  };
220  // Core functions -
221 
222  };
223 
224  extern template MD5 HMAC<MD5>(const std::string&, const std::string&);
225 
226  }
227 }
228 
229 #endif // _ANCH_CRYPTO_MD5_H_
virtual const std::array< uint8_t, O > & digest() const =0
H HMAC(const std::string &, const std::string &)
Definition: hmac.hpp:38
MD5(std::basic_istream< CharT, Traits > &stream)
Definition: md5.hpp:110
MD5(const std::basic_string< CharT, Traits, Allocator > &data)
Definition: md5.hpp:100
virtual void addData(const uint8_t *data, std::size_t len) override
Definition: md5.cpp:84
AnCH framework base namespace.
Definition: base64.hpp:28
virtual void finalize() override
Definition: md5.cpp:124
MD5 hash algorithm implementation.
Definition: md5.hpp:40
Hash algorithm abstract class.
Definition: hash.hpp:41
virtual void reset() override
Definition: md5.hpp:147
MD5()
Definition: md5.cpp:50
virtual ~MD5()
Definition: md5.cpp:60
virtual const std::array< uint8_t, 16 > & digest() const override
Definition: md5.cpp:73