E-MailRelay
gmd5.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 "gstringview.h"
27#include <string>
28
29namespace G
30{
31 class Md5 ;
32}
33
34//| \class G::Md5
35/// MD5 message digest class.
36///
37/// Eg:
38/// \code
39/// Md5 h1 ;
40/// std::string x = ... ;
41/// std::string y = ... ;
42/// assert( (x.size() % 64U) == 0 ) ;
43/// h1.add( x ) ;
44/// Md5 h2( h1.state() ) ;
45/// h2.add( y ) ;
46/// assert( h2.value() == Md5::digest(x+y) ) ;
47/// \endcode
48///
49class G::Md5
50{
51public:
52 G_EXCEPTION( Error , tx("internal md5 error") )
53 G_EXCEPTION_CLASS( InvalidState , tx("invalid md5 hash state") )
54 using big_t = std::size_t ; // To hold at least 32 bits.
55 using small_t = std::size_t ; // To hold at least a std::size_t and no bigger than a big_t.
56 struct digest_state /// Holds the four parts of the md5 state.
57 { big_t a ; big_t b ; big_t c ; big_t d ; } ;
58 struct digest_stream_state /// Holds the md5 state plus unprocessed residual data.
59 { digest_state d ; small_t n ; std::string s ; } ;
60 static_assert( sizeof(big_t) >= 4 , "" ) ;
61 static_assert( sizeof(small_t) >= sizeof(std::size_t) && sizeof(small_t) <= sizeof(big_t) , "" ) ;
62
63 Md5() ;
64 ///< Default constructor.
65
66 explicit Md5( const std::string & state ) ;
67 ///< Constructor using an intermediate state() string.
68 ///< Precondition: state.size() == 20
69
70 std::string state() const ;
71 ///< Returns the current intermediate state as a 20
72 ///< character string, although this requires the size of
73 ///< the added data is a multiple of the blocksize().
74 ///< Note that the trailing 4 characters represent
75 ///< the total size of the added data.
76 /// \see G::HashState
77
78 void add( const std::string & data ) ;
79 ///< Adds more data.
80
81 void add( const char * data , std::size_t size ) ;
82 ///< Adds more data.
83
84 std::string value() ;
85 ///< Returns the hash value as a 16-character string. No
86 ///< more add()s are allowed. The resulting string is not
87 ///< generally printable and it may have embedded nulls.
88 /// \see G::HashState, G::Hash::printable().
89
90 static std::size_t blocksize() ;
91 ///< Returns the block size in bytes (64).
92
93 static std::size_t valuesize() ;
94 ///< Returns the value() size in bytes (16).
95
96 static std::size_t statesize() ;
97 ///< Returns the size of the state() string (20).
98
99 static std::string digest( const std::string & input ) ;
100 ///< A convenience function that returns a digest from
101 ///< one input.
102
103 static std::string digest( std::string_view input ) ;
104 ///< A convenience function that returns a digest from
105 ///< one input.
106
107 static std::string digest( const std::string & input_1 , const std::string & input_2 ) ;
108 ///< A convenience function that returns a digest from
109 ///< two inputs.
110
111 static std::string digest( std::string_view input_1 , std::string_view input_2 ) ;
112 ///< A convenience function that returns a digest from
113 ///< two inputs.
114
115 static std::string digest2( const std::string & input_1 , const std::string & input_2 ) ;
116 ///< A non-overloaded name for the digest() overload
117 ///< taking two parameters.
118
119 static std::string predigest( const std::string & padded_key ) ;
120 ///< A convenience function that add()s the given string
121 ///< of length blocksize() (typically a padded key) and
122 ///< returns the resulting state() truncated to valuesize()
123 ///< characters.
124
125 static std::string postdigest( const std::string & state_pair , const std::string & message ) ;
126 ///< A convenience function that returns the value()
127 ///< from an outer digest that is initialised with the
128 ///< second half of the state pair, and with the value()
129 ///< of an inner digest add()ed; the inner digest being
130 ///< initialised with the first half of the state pair,
131 ///< and with the given message add()ed. The result is
132 ///< a string of 32 non-printing characters. Throws
133 ///< InvalidState if the state-pair string is not valid.
134
135public:
136 ~Md5() = default ;
137 Md5( const Md5 & ) = delete ;
138 Md5( Md5 && ) = delete ;
139 Md5 & operator=( const Md5 & ) = delete ;
140 Md5 & operator=( Md5 && ) = delete ;
141
142private:
143 void consume() ;
144
145private:
146 std::size_t m_n{0U} ;
147 digest_state m_d ;
148 std::string m_s ;
149} ;
150
151#endif
MD5 message digest class.
Definition: gmd5.h:50
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:610
static std::size_t blocksize()
Returns the block size in bytes (64).
Definition: gmd5.cpp:628
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:602
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:597
static std::string digest(const std::string &input)
A convenience function that returns a digest from one input.
Definition: gmd5.cpp:566
void add(const std::string &data)
Adds more data.
Definition: gmd5.cpp:535
std::string value()
Returns the hash value as a 16-character string.
Definition: gmd5.cpp:555
static std::size_t statesize()
Returns the size of the state() string (20).
Definition: gmd5.cpp:639
static std::size_t valuesize()
Returns the value() size in bytes (16).
Definition: gmd5.cpp:633
Md5()
Default constructor.
Definition: gmd5.cpp:511
std::string state() const
Returns the current intermediate state as a 20 character string, although this requires the size of t...
Definition: gmd5.cpp:522
Low-level classes.
Definition: garg.h:36
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
Holds the four parts of the md5 state.
Definition: gmd5.h:57
Holds the md5 state plus unprocessed residual data.
Definition: gmd5.h:59