wxCryptoHash::GetValue() method added

This commit is contained in:
Simon Rozman 2016-03-29 15:33:08 +02:00
parent 9c7bd5c849
commit 91026185c6
2 changed files with 71 additions and 0 deletions

View File

@ -152,6 +152,18 @@ public:
} }
///
/// Finish hashing and return hash data.
///
/// \param[out] hash Hash data
///
/// \returns
/// - true if succeeded
/// - false otherwise
///
virtual bool GetValue(wxMemoryBuffer &hash);
/// ///
/// Signs the hash using session key /// Signs the hash using session key
/// ///
@ -185,6 +197,18 @@ class WXEXTEND_API wxCryptoHashSHA1 : public wxCryptoHash
{ {
public: public:
wxCryptoHashSHA1(wxCryptoSession &session); wxCryptoHashSHA1(wxCryptoSession &session);
///
/// Finish hashing and return hash data.
///
/// \param[out] hash Hash data
///
/// \returns
/// - true if succeeded
/// - false otherwise
///
virtual bool GetValue(wxMemoryBuffer &hash);
}; };

View File

@ -78,6 +78,33 @@ bool wxCryptoHash::Hash(const void *data, size_t size)
} }
bool wxCryptoHash::GetValue(wxMemoryBuffer &hash)
{
wxASSERT_MSG(m_h, wxT("object uninitialized"));
// Query hash size first.
DWORD size, length = sizeof(size);
if (::CryptGetHashParam(m_h, HP_HASHSIZE, (BYTE*)&size, &length, 0)) {
wxASSERT(length == sizeof(size));
// Prepare buffer.
length = size;
hash.SetBufSize(length);
// Query hash value.
if (::CryptGetHashParam(m_h, HP_HASHVAL, (BYTE*)hash.GetData(), &length, 0)) {
hash.SetDataLen(length);
return true;
} else
wxLogLastError(wxT("CryptGetHashParam(HP_HASHVAL)"));
} else
wxLogLastError(wxT("CryptGetHashParam(HP_HASHSIZE)"));
hash.Clear();
return false;
}
bool wxCryptoHash::Sign(wxMemoryBuffer &signature) bool wxCryptoHash::Sign(wxMemoryBuffer &signature)
{ {
// Try with the current buffer size first. // Try with the current buffer size first.
@ -118,6 +145,26 @@ wxCryptoHashSHA1::wxCryptoHashSHA1(wxCryptoSession &session)
} }
bool wxCryptoHashSHA1::GetValue(wxMemoryBuffer &hash)
{
wxASSERT_MSG(m_h, wxT("object uninitialized"));
// Prepare buffer.
DWORD length = 20;
hash.SetBufSize(length);
// Query hash value.
if (::CryptGetHashParam(m_h, HP_HASHVAL, (BYTE*)hash.GetData(), &length, 0)) {
hash.SetDataLen(length);
return true;
} else
wxLogLastError(wxT("CryptGetHashParam(HP_HASHVAL)"));
hash.Clear();
return false;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// wxCryptoKey // wxCryptoKey
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////