E-MailRelay
gmd5.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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
28namespace 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///
48class G::Md5
49{
50public:
51 G_EXCEPTION( Error , "internal md5 error" ) ;
52 G_EXCEPTION_CLASS( InvalidState , "invalid md5 hash state" ) ;
53 using big_t = std::size_t ; // To hold at least 32 bits.
54 using small_t = std::size_t ; // To hold at least a std::size_t and no bigger than a big_t.
55 struct digest_state /// Holds the four parts of the md5 state.
56 { big_t a ; big_t b ; big_t c ; big_t d ; } ;
57 struct digest_stream_state /// Holds the md5 state plus unprocessed residual data.
58 { digest_state d ; small_t n ; std::string s ; } ;
59 static_assert( sizeof(big_t) >= 4 , "" ) ;
60 static_assert( sizeof(small_t) >= sizeof(std::size_t) && sizeof(small_t) <= sizeof(big_t) , "" ) ;
61
62 Md5() ;
63 ///< Default constructor.
64
65 explicit Md5( const std::string & state ) ;
66 ///< Constructor using an intermediate state() string.
67 ///< Precondition: state.size() == 20
68
69 std::string state() const ;
70 ///< Returns the current intermediate state as a 20
71 ///< character string, although this requires the size of
72 ///< the added data is a multiple of the blocksize().
73 ///< Note that the trailing 4 characters represent
74 ///< the total size of the added data.
75 /// \see G::HashState
76
77 void add( const std::string & data ) ;
78 ///< Adds more data.
79
80 std::string value() ;
81 ///< Returns the hash value as a 16-character string. No
82 ///< more add()s are allowed. The resulting string is not
83 ///< generally printable and it may have embedded nulls.
84 /// \see G::HashState, G::Hash::printable().
85
86 static std::size_t blocksize() ;
87 ///< Returns the block size in bytes (64).
88
89 static std::size_t valuesize() ;
90 ///< Returns the value() size in bytes (16).
91
92 static std::size_t statesize() ;
93 ///< Returns the size of the state() string (20).
94
95 static std::string digest( const std::string & input ) ;
96 ///< A convenience function that returns a digest from
97 ///< one input.
98
99 static std::string digest( const std::string & input_1 , const std::string & input_2 ) ;
100 ///< A convenience function that returns a digest from
101 ///< two inputs.
102
103 static std::string digest2( const std::string & input_1 , const std::string & input_2 ) ;
104 ///< A non-overloaded name for the digest() overload
105 ///< taking two parameters.
106
107 static std::string predigest( const std::string & padded_key ) ;
108 ///< A convenience function that add()s the given string
109 ///< of length blocksize() (typically a padded key) and
110 ///< returns the resulting state() truncated to valuesize()
111 ///< characters.
112
113 static std::string postdigest( const std::string & state_pair , const std::string & message ) ;
114 ///< A convenience function that returns the value()
115 ///< from an outer digest that is initialised with the
116 ///< second half of the state pair, and with the value()
117 ///< of an inner digest add()ed; the inner digest being
118 ///< initialised with the first half of the state pair,
119 ///< and with the given message add()ed. The result is
120 ///< a string of 32 non-printing characters. Throws
121 ///< InvalidState if the state-pair string is not valid.
122
123public:
124 ~Md5() = default ;
125 Md5( const Md5 & ) = delete ;
126 Md5( Md5 && ) = delete ;
127 void operator=( const Md5 & ) = delete ;
128 void operator=( Md5 && ) = delete ;
129
130private:
131 std::size_t m_n{0U} ;
132 digest_state m_d ;
133 std::string m_s ;
134} ;
135
136#endif
MD5 message digest class.
Definition: gmd5.h:49
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:562
static std::size_t blocksize()
Returns the block size in bytes (64).
Definition: gmd5.cpp:580
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:554
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:549
static std::string digest(const std::string &input)
A convenience function that returns a digest from one input.
Definition: gmd5.cpp:535
void add(const std::string &data)
Adds more data.
Definition: gmd5.cpp:510
std::string value()
Returns the hash value as a 16-character string.
Definition: gmd5.cpp:525
static std::size_t statesize()
Returns the size of the state() string (20).
Definition: gmd5.cpp:590
static std::size_t valuesize()
Returns the value() size in bytes (16).
Definition: gmd5.cpp:585
Md5()
Default constructor.
Definition: gmd5.cpp:493
std::string state() const
Returns the current intermediate state as a 20 character string, although this requires the size of t...
Definition: gmd5.cpp:504
Low-level classes.
Definition: galign.h:28
Holds the four parts of the md5 state.
Definition: gmd5.h:56
Holds the md5 state plus unprocessed residual data.
Definition: gmd5.h:58