Add SHA-256 support
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
5fa66f625d
commit
f3e208572f
@ -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
|
/// Cryptographics Key Base
|
||||||
///
|
///
|
||||||
|
@ -22,6 +22,23 @@
|
|||||||
#pragma comment(lib, "Crypt32.lib")
|
#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
|
// wxCryptoSession
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@ -89,17 +106,7 @@ bool wxCryptoHash::GetValue(wxMemoryBuffer &hash)
|
|||||||
DWORD size, length = sizeof(size);
|
DWORD size, length = sizeof(size);
|
||||||
if (::CryptGetHashParam(m_h, HP_HASHSIZE, (BYTE*)&size, &length, 0)) {
|
if (::CryptGetHashParam(m_h, HP_HASHSIZE, (BYTE*)&size, &length, 0)) {
|
||||||
wxASSERT(length == sizeof(size));
|
wxASSERT(length == sizeof(size));
|
||||||
|
return wxGetHashValue(m_h, size, hash);
|
||||||
// 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
|
} else
|
||||||
wxLogLastError(wxT("CryptGetHashParam(HP_HASHSIZE)"));
|
wxLogLastError(wxT("CryptGetHashParam(HP_HASHSIZE)"));
|
||||||
|
|
||||||
@ -153,23 +160,33 @@ _Use_decl_annotations_
|
|||||||
bool wxCryptoHashSHA1::GetValue(wxMemoryBuffer &hash)
|
bool wxCryptoHashSHA1::GetValue(wxMemoryBuffer &hash)
|
||||||
{
|
{
|
||||||
wxASSERT_MSG(m_h, wxT("object uninitialized"));
|
wxASSERT_MSG(m_h, wxT("object uninitialized"));
|
||||||
|
return wxGetHashValue(m_h, 20, hash);
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#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
|
// wxCryptoKey
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user