Add SHA-256 support

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-02-25 12:45:22 +01:00
parent 5fa66f625d
commit f3e208572f
2 changed files with 71 additions and 25 deletions

View File

@ -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
///

View File

@ -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
//////////////////////////////////////////////////////////////////////////