Pol  Revision:4b29d2b
clib_MD5.cpp
Go to the documentation of this file.
1 
10 #include "clib_MD5.h"
11 
12 #include <iomanip>
13 #include <sstream>
14 
15 #include "logfacility.h"
16 #include "pol_global_config.h"
17 #include "stlutil.h"
18 
19 #ifdef WINDOWS
20 #include "Header_Windows.h"
21 #include <wincrypt.h>
22 
23 namespace Pol
24 {
25 namespace Clib
26 {
27 static HCRYPTPROV hProv = NULL;
28 
29 bool MD5_Encrypt( const std::string& in, std::string& out )
30 {
31  // bool bResult = true;
32 
33  // HCRYPTKEY hKey = NULL;
34  // HCRYPTKEY hXchgKey = NULL;
35  HCRYPTHASH hHash = NULL;
36 
37  if ( !hProv )
38  {
39  if ( !CryptAcquireContext( &hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) )
40  {
41  if ( !CryptAcquireContext( &hProv, NULL, NULL, PROV_RSA_FULL, 0 ) )
42  {
43  ERROR_PRINT << "Error " << GetLastError() << " acquiring crypt context\n";
44  return false;
45  }
46  }
47  }
48 
49  if ( !CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash ) )
50  {
51  ERROR_PRINT << "Error " << GetLastError() << " creating hash\n";
52  return false;
53  }
54 
55  // Hash password string.
56  if ( !CryptHashData( hHash, (const unsigned char*)( in.data() ),
57  static_cast<unsigned long>( in.length() ), 0 ) )
58  {
59  ERROR_PRINT << "Error " << GetLastError() << " adding data to hash\n";
60  return false;
61  }
62  unsigned long len = 16;
63  unsigned char buf[16];
64  if ( !CryptGetHashParam( hHash, HP_HASHVAL, buf, &len, 0 ) )
65  {
66  ERROR_PRINT << "Error " << GetLastError() << " getting hash value\n";
67  return false;
68  }
69 
70  std::ostringstream os;
71  for ( unsigned int i = 0; i < len; i++ )
72  {
73  os << std::setfill( '0' ) << std::setw( 2 ) << std::hex << (int)buf[i];
74  }
75  out = os.str();
76 
77  CryptDestroyHash( hHash );
78  return true;
79 }
80 
81 void MD5_Cleanup()
82 {
83  if ( hProv )
84  {
85  CryptReleaseContext( hProv, 0 );
86  }
87 }
88 
89 #else
90 
91 #include <openssl/md5.h>
92 
93 namespace Pol
94 {
95 namespace Clib
96 {
97 bool MD5_Encrypt( const std::string& in, std::string& out )
98 {
99  unsigned char sum[16];
100 
101  MD5( reinterpret_cast<const unsigned char*>( in.c_str() ), in.length(), sum );
102 
103  std::ostringstream os;
104  for ( auto& elem : sum )
105  {
106  os << std::setfill( '0' ) << std::setw( 2 ) << std::hex << (int)elem;
107  }
108  out = os.str();
109 
110  return true;
111 }
113 {
114  // OpenSSL cleanup, if any
115 }
116 
117 #endif
118 
119 bool MD5_Compare( const std::string& a, const std::string& b )
120 {
121  bool ret = false;
122  if ( stringicmp( a, b ) == 0 )
123  ret = true;
124 
125  return ret;
126 }
127 }
128 }
bool MD5_Encrypt(const std::string &in, std::string &out)
Definition: clib_MD5.cpp:97
void MD5_Cleanup()
Definition: clib_MD5.cpp:112
int stringicmp(const S1 &str1, const S2 &str2)
Definition: stlutil.h:46
bool MD5_Compare(const std::string &a, const std::string &b)
Definition: clib_MD5.cpp:119
#define ERROR_PRINT
Definition: logfacility.h:230
Definition: berror.cpp:12