VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gmd5.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 class G::Md5
37 {
38 public:
39  G_EXCEPTION( InvalidMaskedKey , "invalid md5 key" ) ;
40  G_EXCEPTION( Error , "internal md5 error" ) ;
41  struct Masked /// An overload discriminator for G::Md5::hmac()
42  {} ;
43 
44  static std::string digest( const std::string & input ) ;
45  ///< Creates an MD5 digest. The resulting string is not generally printable
46  ///< and it may have embedded NULs.
47 
48  static std::string digest( const std::string & input_1 , const std::string & input_2 ) ;
49  ///< An overload which processes two input strings.
50 
51  static std::string printable( const std::string & input ) ;
52  ///< Converts a binary string into a printable form, using a
53  ///< lowercase hexadecimal encoding. See also RFC2095.
54 
55  static std::string hmac( const std::string & key , const std::string & input ) ;
56  ///< Computes a Hashed Message Authentication Code using MD5 as the hash
57  ///< function. This is typically for challenge-response authentication
58  ///< where the plaintext input is an arbitrary challenge string from the
59  ///< server that the client has to hmac() using their shared private key.
60  ///<
61  ///< See also RFC2104 [HMAC-MD5].
62  ///<
63  ///< For hash function H with block size B (64) and output size L (16),
64  ///< using shared key SK:
65  ///<
66  ///< \code
67  ///< K = large(SK) ? H(SK) : SK
68  ///< ipad = 0x36 repeated B times
69  ///< opad = 0x5C repeated B times
70  ///< HMAC = H( K XOR opad , H( K XOR ipad , plaintext ) )
71  ///< \endcode
72  ///<
73  ///< The H() function processes a stream of blocks; the first parameter
74  ///< above represents the first block, and the second parameter is the
75  ///< rest of the stream (zero-padded up to a block boundary).
76  ///<
77  ///< The shared key can be up to B bytes, or if more than B bytes then
78  ///< K is the L-byte result of hashing the shared-key. K is zero-padded
79  ///< up to B bytes for XOR-ing.
80 
81  static std::string hmac( const std::string & masked_key , const std::string & input , Masked ) ;
82  ///< An hmac() overload using a masked key.
83  ///<
84  ///< A masked key (MK) is the result of doing the initial, plaintext-independent
85  ///< parts of HMAC computation, taking the intermediate results of both the
86  ///< inner and outer hash functions.
87  ///<
88  ///< \code
89  ///< K = large(SK) ? H(SK) : SK
90  ///< HKipad = H( K XOR ipad , )
91  ///< HKopad = H( K XOR opad , )
92  ///< MK := ( HKipad , HKopad )
93  ///< \endcode
94 
95  static std::string mask( const std::string & key ) ;
96  ///< Computes a masked key for hmac() from the given shared key, returning
97  ///< a printable string.
98 
99 private:
100  static std::string digest( const std::string & input_1 , const std::string * input_2 ) ;
101  static std::string mask( const std::string & k64 , const std::string & pad ) ;
102  static std::string xor_( const std::string & , const std::string & ) ;
103  static std::string key64( std::string ) ;
104  static std::string ipad() ;
105  static std::string opad() ;
106  Md5() ;
107 } ;
108 
109 #endif
An overload discriminator for G::Md5::hmac()
Definition: gmd5.h:41
static std::string hmac(const std::string &key, const std::string &input)
Computes a Hashed Message Authentication Code using MD5 as the hash function.
static std::string mask(const std::string &key)
Computes a masked key for hmac() from the given shared key, returning a printable string...
static std::string digest(const std::string &input)
Creates an MD5 digest.
MD5 message digest class.
Definition: gmd5.h:36
static std::string printable(const std::string &input)
Converts a binary string into a printable form, using a lowercase hexadecimal encoding.