This source file includes following definitions.
- SHA1HashString
#include "base/sha1.h"
#include <windows.h>
#include <wincrypt.h>
#include "base/logging.h"
namespace base {
std::string SHA1HashString(const std::string& str) {
ScopedHCRYPTPROV provider;
if (!CryptAcquireContext(provider.receive(), NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
DLOG_GETLASTERROR(ERROR) << "CryptAcquireContext failed";
return std::string(kSHA1Length, '\0');
}
{
ScopedHCRYPTHASH hash;
if (!CryptCreateHash(provider, CALG_SHA1, 0, 0, hash.receive())) {
DLOG_GETLASTERROR(ERROR) << "CryptCreateHash failed";
return std::string(kSHA1Length, '\0');
}
if (!CryptHashData(hash, reinterpret_cast<CONST BYTE*>(str.data()),
static_cast<DWORD>(str.length()), 0)) {
DLOG_GETLASTERROR(ERROR) << "CryptHashData failed";
return std::string(kSHA1Length, '\0');
}
DWORD hash_len = 0;
DWORD buffer_size = sizeof hash_len;
if (!CryptGetHashParam(hash, HP_HASHSIZE,
reinterpret_cast<unsigned char*>(&hash_len),
&buffer_size, 0)) {
DLOG_GETLASTERROR(ERROR) << "CryptGetHashParam(HP_HASHSIZE) failed";
return std::string(kSHA1Length, '\0');
}
std::string result;
if (!CryptGetHashParam(hash, HP_HASHVAL,
reinterpret_cast<BYTE*>(WriteInto(&result, hash_len + 1)), &hash_len,
0))) {
DLOG_GETLASTERROR(ERROR) << "CryptGetHashParam(HP_HASHVAL) failed";
return std::string(kSHA1Length, '\0');
}
if (hash_len != kSHA1Length) {
DLOG(ERROR) << "Returned hash value is wrong length: " << hash_len
<< " should be " << kSHA1Length;
return std::string(kSHA1Length, '\0');
}
return result;
}
}
}