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 /// \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 namespace G
30 {
31 	class Md5 ;
32 }
33 
34 /// \class G::Md5
35 /// MD5 class.
36 ///
37 class G::Md5
38 {
39 public:
40 	G_EXCEPTION( InvalidMaskedKey , "invalid md5 key" ) ;
41 	G_EXCEPTION( Error , "internal md5 error" ) ;
42 	/// An overload discriminator for G::Md5::hmac()
43 	struct Masked
44 		{} ;
45 
46 	static std::string digest( const std::string & input ) ;
47 		///< Creates an MD5 digest. The resulting string is not
48 		///< generally printable and may have embedded NULs.
49 
50 	static std::string digest( const std::string & input_1 , const std::string & input_2 ) ;
51 		///< An overload which processes two input strings.
52 
53 	static std::string printable( const std::string & input ) ;
54 		///< Converts a binary string into a printable
55 		///< form, using a lowercase hexadecimal encoding.
56 		///< See also RFC2095.
57 
58 	static std::string hmac( const std::string & key , const std::string & input ) ;
59 		///< Computes a Hashed Message Authentication Code
60 		///< using MD5 as the hash function.
61 		///< See also RFC2104 [HMAC-MD5].
62 
63 	static std::string hmac( const std::string & masked_key , const std::string & input , Masked ) ;
64 		///< An hmac() overload using a masked key.
65 
66 	static std::string mask( const std::string & key ) ;
67 		///< Masks an HMAC key so that it can be stored more safely.
68 
69 private:
70 	static std::string digest( const std::string & input_1 , const std::string * input_2 ) ;
71 	static std::string mask( const std::string & k64 , const std::string & pad ) ;
72 	static std::string xor_( const std::string & , const std::string & ) ;
73 	static std::string key64( std::string ) ;
74 	static std::string ipad() ;
75 	static std::string opad() ;
76 	Md5() ;
77 } ;
78 
79 #endif
80