diff --git a/include/wxex/crypto.h b/include/wxex/crypto.h index e8f5a60..1dc79f7 100644 --- a/include/wxex/crypto.h +++ b/include/wxex/crypto.h @@ -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 /// @@ -185,6 +197,18 @@ class WXEXTEND_API wxCryptoHashSHA1 : public wxCryptoHash { public: 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); }; diff --git a/src/crypto.cpp b/src/crypto.cpp index f90b517..63d9c05 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -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) { // 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 //////////////////////////////////////////////////////////////////////////