md5.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // md5.h
19 //
20 // An implementation of the RFC-1321 message digest algorithm.
21 //
22 // This code was developed from main body of RFC 1321 without reference to the
23 // RSA reference implementation in the appendix.
24 //
25 // A minor portability advantage over the RSA implementation is that there is no
26 // need to define a datatype that is exactly 32 bits: the requirement is that
27 // 'big_t' is at least 32 bits, but it can be more.
28 //
29 // Stylistic advantages are that it is written in C++ with an enclosing namespace,
30 // it does not use preprocessor macros, and there is an element of layering with
31 // digest_stream built on top of the low-level digest class.
32 //
33 
34 #ifndef MD5_GHW_H
35 #define MD5_GHW_H
36 
37 #include <string> // std::string
38 #include <cstdlib> // std::size_t
39 
40 namespace md5
41 {
42  typedef std::string string_type ;
44  typedef size_type big_t ;
45  typedef size_type small_t ;
46  typedef char assert_big_t_is_big_enough[sizeof(big_t)>=4U?1:-1] ;
47  typedef char assert_small_t_is_big_enough[sizeof(small_t)>=sizeof(size_type)?1:-1];
48  class digest ;
49  class digest_stream ;
50  class format ;
51  class block ;
52 }
53 
78 {
79 public:
80  struct state_type
81  { big_t a ; big_t b ; big_t c ; big_t d ; } ;
82 
83  digest() ;
87 
88  explicit digest( const string_type & s ) ;
92 
93  explicit digest( state_type ) ;
100 
101  state_type state() const ;
104 
105  void add( const block & ) ;
107 
108 private:
109  typedef big_t (*aux_fn_t)( big_t , big_t , big_t ) ;
110  enum Permutation { ABCD , DABC , CDAB , BCDA } ;
111  big_t a ;
112  big_t b ;
113  big_t c ;
114  big_t d ;
115 
116 private:
117  explicit digest( const block & ) ;
118  void add( const digest & ) ;
119  void init() ;
120  void calculate( const block & ) ;
121  static big_t T( small_t i ) ;
122  static big_t rot32( small_t places , big_t n ) ;
123  void operator()( const block & , aux_fn_t , Permutation , small_t , small_t , small_t ) ;
124  static big_t op( const block & , aux_fn_t , big_t , big_t , big_t , big_t , small_t , small_t , small_t ) ;
125  void round1( const block & ) ;
126  void round2( const block & ) ;
127  void round3( const block & ) ;
128  void round4( const block & ) ;
129  static big_t F( big_t x , big_t y , big_t z ) ;
130  static big_t G( big_t x , big_t y , big_t z ) ;
131  static big_t H( big_t x , big_t y , big_t z ) ;
132  static big_t I( big_t x , big_t y , big_t z ) ;
133 } ;
134 
142 {
143 public:
144  static string_type rfc( const digest & ) ;
146 
147  static string_type rfc( const digest::state_type & ) ;
149 
150  static string_type raw( const digest::state_type & ) ;
154 
155 private:
156  static string_type raw( big_t n ) ;
157  static string_type str( big_t n ) ;
158  static string_type str2( small_t n ) ;
159  static string_type str1( small_t n ) ;
160  format() ; // not implemented
161 } ;
162 
168 {
169 public:
170  block( const string_type & s , small_t block_offset , big_t end_value ) ;
185 
186  static big_t end( small_t data_length ) ;
191 
192  static small_t blocks( small_t data_length ) ;
197 
198  big_t X( small_t ) const ;
200 
201 private:
202  block( const block & ) ; // not implemented
203  void operator=( const block & ) ; // not implemented
204  small_t x( small_t ) const ;
205  static small_t rounded( small_t n ) ;
206 
207 private:
208  const string_type & m_s ;
209  small_t m_block ;
210  big_t m_end_value ;
211 } ;
212 
239 {
240 public:
241  struct state_type
243 
244  digest_stream() ;
246 
253 
254  void add( const string_type & ) ;
256 
257  void close() ;
259 
260  small_t size() const ;
263 
264  state_type state() const ;
267 
268 private:
269  digest m_digest ;
270  string_type m_buffer ;
271  small_t m_length ;
272 } ;
273 
274 #endif
275 
void add(const string_type &)
Adds more message data.
Definition: md5.cpp:392
A class that calculates an md5 digest from a data stream using the algorithm described by RFC 1321...
Definition: md5.h:238
size_type big_t
To hold at least 32 bits, maybe more. Try unsigned long on small systems.
Definition: md5.h:44
void close()
Called after the last add().
Definition: md5.cpp:405
size_type small_t
To hold at least a size_t. Must fit in a big_t.
Definition: md5.h:45
digest::state_type d
Definition: md5.h:242
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
big_t X(small_t) const
Returns a value from within the block. See RFC 1321.
Definition: md5.cpp:341
block(const string_type &s, small_t block_offset, big_t end_value)
Constructor.
Definition: md5.cpp:315
Low-level classes.
digest()
Default constructor.
Definition: md5.cpp:35
Holds the md5 algorithm state. Used by md5::digest.
Definition: md5.h:80
A helper class used by the md5::digest implementation to represent a 64-character data block...
Definition: md5.h:167
A static string-formatting class for the output of md5::digest.
Definition: md5.h:141
std::string string_type
A string type.
Definition: md5.h:42
static small_t blocks(small_t data_length)
Takes the total number of bytes in the input message and returns the number of 64-byte blocks...
Definition: md5.cpp:335
char assert_small_t_is_big_enough[sizeof(small_t)>=sizeof(size_type)?1:-1]
A static assertion check.
Definition: md5.h:47
A class that calculates an md5 digest from one or more 64-byte blocks of data using the algorithm des...
Definition: md5.h:77
small_t size() const
Returns how many data bytes have been accumulated so far.
Definition: md5.cpp:421
A standalone implementation of the MD5 hashing algorithm.
Definition: md5.h:40
state_type state() const
Returns the internal state.
Definition: md5.cpp:48
state_type state() const
Returns the current state.
Definition: md5.cpp:412
static big_t end(small_t data_length)
Takes the total number of bytes in the input message and returns a value which can be passed to the c...
Definition: md5.cpp:322
digest_stream()
Default constructor.
Definition: md5.cpp:381
static string_type rfc(const digest &)
Returns the digest string in the RFC format.
Definition: md5.cpp:274
char assert_big_t_is_big_enough[sizeof(big_t)>=4U?1:-1]
A static assertion check.
Definition: md5.h:46
void add(const block &)
Adds a 64-byte block of the message.
Definition: md5.cpp:73
< Holds the state of an md5 digest stream. Used by md5::digest_stream.
Definition: md5.h:241
static string_type raw(const digest::state_type &)
Returns the raw digest data as a std::string.
Definition: md5.cpp:252