E-MailRelay
gmd5.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 /// \file gmd5.h
19 ///
20 
21 #ifndef G_MD5_H
22 #define G_MD5_H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include <string>
27 
28 namespace G
29 {
30  class Md5 ;
31 }
32 
33 /// \class G::Md5
34 /// MD5 message digest class.
35 ///
36 /// Eg:
37 /// \code
38 /// Md5 h1 ;
39 /// std::string x = ... ;
40 /// std::string y = ... ;
41 /// assert( (x.size() % 64U) == 0 ) ;
42 /// h1.add( x ) ;
43 /// Md5 h2( h1.state() ) ;
44 /// h2.add( y ) ;
45 /// assert( h2.value() == Md5::digest(x+y) ) ;
46 /// \endcode
47 ///
48 class G::Md5
49 {
50 public:
51  G_EXCEPTION( Error , "internal md5 error" ) ;
52  typedef size_t big_t ; // To hold at least 32 bits.
53  typedef size_t small_t ; // To hold at least a size_t and no bigger than a big_t.
54  struct digest_state /// Holds the four parts of the md5 state.
55  { big_t a ; big_t b ; big_t c ; big_t d ; } ;
56  struct digest_stream_state /// Holds the md5 state plus unprocessed residual data.
57  { digest_state d ; small_t n ; std::string s ; } ;
58 
59  Md5() ;
60  ///< Default constructor.
61 
62  explicit Md5( const std::string & state ) ;
63  ///< Constructor using an intermediate state() string.
64 
65  std::string state() const ;
66  ///< Returns the current intermediate state as a 20
67  ///< character string, although this requires the size of
68  ///< the added data is a multiple of the blocksize().
69  ///< Note that the trailing 4 characters represent
70  ///< the total size of the added data.
71  /// \see G::HashState
72 
73  void add( const std::string & data ) ;
74  ///< Adds more data.
75 
76  std::string value() ;
77  ///< Returns the hash value as a 16-character string. No
78  ///< more add()s are allowed. The resulting string is not
79  ///< generally printable and it may have embedded nulls.
80  /// \see G::HashState, G::Hash::printable().
81 
82  static size_t blocksize() ;
83  ///< Returns the block size in bytes (64).
84 
85  static size_t valuesize() ;
86  ///< Returns the value() size in bytes (16).
87 
88  static size_t statesize() ;
89  ///< Returns the size of the state() string (20).
90 
91  static std::string digest( const std::string & input ) ;
92  ///< A convenience function that returns a digest from
93  ///< one input.
94 
95  static std::string digest( const std::string & input_1 , const std::string & input_2 ) ;
96  ///< A convenience function that returns a digest from
97  ///< two inputs.
98 
99  static std::string digest2( const std::string & input_1 , const std::string & input_2 ) ;
100  ///< A non-overloaded name for the digest() overload
101  ///< taking two parameters.
102 
103  static std::string predigest( const std::string & padded_key ) ;
104  ///< A convenience function that add()s the given string
105  ///< of length blocksize() (typically a padded key) and
106  ///< returns the resulting state() truncated to valuesize()
107  ///< characters.
108 
109  static std::string postdigest( const std::string & state_pair , const std::string & message ) ;
110  ///< A convenience function that returns the value()
111  ///< from an outer digest that is initialised with the
112  ///< second half of the state pair, and with the value()
113  ///< of an inner digest add()ed; the inner digest being
114  ///< initialised with the first half of the state pair,
115  ///< and with the given message add()ed. The result is
116  ///< a string of 32 non-printing characters.
117 
118 private:
119  Md5( const Md5 & ) ;
120  void operator=( const Md5 & ) ;
121 
122 private:
123  digest_state m_d ;
124  size_t m_n ;
125  std::string m_s ;
126 } ;
127 
128 #endif
std::string state() const
Returns the current intermediate state as a 20 character string, although this requires the size of t...
Definition: gmd5.cpp:491
void add(const std::string &data)
Adds more data.
Definition: gmd5.cpp:497
static size_t blocksize()
Returns the block size in bytes (64).
Definition: gmd5.cpp:564
Md5()
Default constructor.
Definition: gmd5.cpp:478
Low-level classes.
Definition: garg.h:29
std::string value()
Returns the hash value as a 16-character string.
Definition: gmd5.cpp:512
static std::string postdigest(const std::string &state_pair, const std::string &message)
A convenience function that returns the value() from an outer digest that is initialised with the sec...
Definition: gmd5.cpp:549
static std::string digest(const std::string &input)
A convenience function that returns a digest from one input.
Definition: gmd5.cpp:522
static std::string predigest(const std::string &padded_key)
A convenience function that add()s the given string of length blocksize() (typically a padded key) an...
Definition: gmd5.cpp:541
static size_t valuesize()
Returns the value() size in bytes (16).
Definition: gmd5.cpp:569
static size_t statesize()
Returns the size of the state() string (20).
Definition: gmd5.cpp:574
Holds the md5 state plus unprocessed residual data.
Definition: gmd5.h:56
static std::string digest2(const std::string &input_1, const std::string &input_2)
A non-overloaded name for the digest() overload taking two parameters.
Definition: gmd5.cpp:536
MD5 message digest class.
Definition: gmd5.h:48
Holds the four parts of the md5 state.
Definition: gmd5.h:54