Re: Simple to use hash function?
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:uoTwxtUNIHA.4480@TK2MSFTNGP06.phx.gbl...
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:GLI4j.103$Aw4.85@newsfe07.lga...
I want to hash a users password in the client and send the hashed value
over the wire and store the hashed value on the server, the server never
needs to know the real password. I'm looking for a simple to use hash
function for VC++ .net 2003 and can't find one.
Take your pick:
<http://www.koders.com/cpp/fid587D455902E6408B01FB16F6A3C962C697386E36.aspx>
Those are all nice but I decided in the end to wrap Microsofts calls. This
is what I'm going with I think.
#include <windows.h>
#include <wincrypt.h>
#include <iostream>
#include <string>
#include <sstream>
std::string HashString( const std::string& Input )
{
HCRYPTPROV hProv = 0;
//Get handle to the crypto provider
if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
DWORD Status = GetLastError();
std::ostringstream Error;
Error << "CryptAcquireContext failed: " << Status;
throw std::exception(Error.str().c_str());
}
HCRYPTHASH hHash = 0;
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
DWORD Status = GetLastError();
std::ostringstream Error;
Error << "CryptCreateHash failed: " << Status;
CryptReleaseContext(hProv, 0);
throw std::exception(Error.str().c_str());
}
if (!CryptHashData(hHash, reinterpret_cast<const unsigned char*>(
Input.c_str() ), static_cast<DWORD>( Input.size() ), 0))
{
DWORD Status = GetLastError();
std::ostringstream Error;
Error << "CryptHashData failed: " << Status;
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
throw std::exception(Error.str().c_str());
}
const int MD5LEN(16); // Non variable
DWORD HashLength = MD5LEN; // In and out
BYTE RawHash[MD5LEN]; // To store the raw hash data
std::string HexHash;
if (CryptGetHashParam(hHash, HP_HASHVAL, RawHash, &HashLength, 0))
{
CHAR HexDigits[] = "0123456789abcdef";
// Convert to Hex
for (DWORD i = 0; i < HashLength; i++)
{
HexHash += HexDigits[RawHash[i] >> 4];
HexHash += HexDigits[RawHash[i] & 0xf];
}
}
else
{
DWORD dwStatus = GetLastError();
std::ostringstream Error;
Error << "CryptGetHashParam failed: " << dwStatus;
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
throw std::exception(Error.str().c_str());
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
return HexHash;
}
int main()
{
try
{
std::cout << HashString( "" ) << "\n";
std::cout << HashString( " " ) << "\n";
std::cout << HashString( " " ) << "\n";
std::cout << HashString( "Test String\n\n\n" ) << "\n";
std::cout << HashString( "Test String\n\n" ) << "\n";
}
catch (std::exception e)
{
std::cout << e.what() << "\n";
}
}