From f3e208572f8c3aa16d7a220f8b3671c6c4fa8e8c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 25 Feb 2020 12:45:22 +0100 Subject: [PATCH] Add SHA-256 support Signed-off-by: Simon Rozman --- include/wxex/crypto.h | 29 +++++++++++++++++++ src/crypto.cpp | 67 +++++++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/include/wxex/crypto.h b/include/wxex/crypto.h index 316d511..65e4a46 100644 --- a/include/wxex/crypto.h +++ b/include/wxex/crypto.h @@ -270,6 +270,35 @@ public: }; +#if (NTDDI_VERSION > NTDDI_WINXPSP2) + +/// +/// SHA-256 Cryptographics Hash +/// +class WXEXTEND_API wxCryptoHashSHA256 : public wxCryptoHash +{ +public: + /// + /// Creates a new cryptographics SHA-256 hash + /// + wxCryptoHashSHA256(wxCryptoSession &session); + + + /// + /// Finish hashing and return hash data. + /// + /// \param[out] hash Hash data + /// + /// \returns + /// - \c true if succeeded + /// - \c false otherwise + /// + virtual _Success_(return != 0) bool GetValue(_Out_ wxMemoryBuffer &hash); +}; + +#endif + + /// /// Cryptographics Key Base /// diff --git a/src/crypto.cpp b/src/crypto.cpp index 321de9d..d61148d 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -22,6 +22,23 @@ #pragma comment(lib, "Crypt32.lib") +static bool wxGetHashValue(HCRYPTHASH h, DWORD length, wxMemoryBuffer &hash) +{ + // Prepare buffer. + hash.SetBufSize(length); + + // Query hash value. + if (::CryptGetHashParam(h, HP_HASHVAL, (BYTE*)hash.GetData(), &length, 0)) { + hash.SetDataLen(length); + return true; + } else + wxLogLastError(wxT("CryptGetHashParam(HP_HASHVAL)")); + + hash.Clear(); + return false; +} + + ////////////////////////////////////////////////////////////////////////// // wxCryptoSession ////////////////////////////////////////////////////////////////////////// @@ -89,17 +106,7 @@ bool wxCryptoHash::GetValue(wxMemoryBuffer &hash) 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)")); + return wxGetHashValue(m_h, size, hash); } else wxLogLastError(wxT("CryptGetHashParam(HP_HASHSIZE)")); @@ -153,23 +160,33 @@ _Use_decl_annotations_ 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; + return wxGetHashValue(m_h, 20, hash); } +#if (NTDDI_VERSION > NTDDI_WINXPSP2) + +////////////////////////////////////////////////////////////////////////// +// wxCryptoHashSHA256 +////////////////////////////////////////////////////////////////////////// + +wxCryptoHashSHA256::wxCryptoHashSHA256(wxCryptoSession &session) +{ + if (!::CryptCreateHash(session, CALG_SHA_256, 0, 0, &m_h)) + wxLogLastError(wxT("CryptCreateHash(CALG_SHA_256)")); +} + + +_Use_decl_annotations_ +bool wxCryptoHashSHA256::GetValue(wxMemoryBuffer &hash) +{ + wxASSERT_MSG(m_h, wxT("object uninitialized")); + return wxGetHashValue(m_h, 32, hash); +} + +#endif + + ////////////////////////////////////////////////////////////////////////// // wxCryptoKey //////////////////////////////////////////////////////////////////////////