2023-11-08 20:29:53 +08:00
|
|
|
|
|
|
|
#include "CryptoContext.h"
|
|
|
|
#include "CryptoFns.h"
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
DWORD CCryptoContext::ms_dwRefCount = 0;
|
|
|
|
DWORD CCryptoContext::ms_dwProviderType = PROV_RSA_FULL;
|
|
|
|
LPTSTR CCryptoContext::ms_szProviderName = NULL;
|
|
|
|
LPTSTR CCryptoContext::ms_szContainerName = (LPTSTR)"SAMP";
|
2024-05-23 22:26:55 +08:00
|
|
|
HMODULE CCryptoContext::ms_hAdvApi32 = NULL;
|
2023-11-08 20:29:53 +08:00
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
CCryptoContext::CCryptoContext(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if (!ms_hAdvApi32)
|
|
|
|
{
|
|
|
|
ms_hAdvApi32 = LoadLibrary("advapi32.dll");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Open existing context, if not found, create one!
|
|
|
|
if ( !CRYPT(AcquireContext)(&m_hCryptProv, ms_szContainerName, ms_szProviderName, ms_dwProviderType, 0) ) {
|
|
|
|
if( !CRYPT(AcquireContext)(&m_hCryptProv, ms_szContainerName, ms_szProviderName, ms_dwProviderType, CRYPT_NEWKEYSET) ) {
|
|
|
|
throw(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ms_dwRefCount++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
CCryptoContext::~CCryptoContext(void)
|
|
|
|
{
|
|
|
|
// Release the context
|
|
|
|
CRYPT(ReleaseContext)(m_hCryptProv, 0);
|
|
|
|
ms_dwRefCount--;
|
|
|
|
|
|
|
|
if (ms_dwRefCount == 0) {
|
|
|
|
/*
|
|
|
|
// Free the library
|
|
|
|
if (ms_hAdvApi32)
|
|
|
|
FreeLibrary(ms_hAdvApi32);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Delete the context
|
|
|
|
CRYPT(AcquireContext)(&m_hCryptProv, ms_szContainerName, ms_szProviderName, ms_dwProviderType, CRYPT_DELETEKEYSET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
HCRYPTPROV CCryptoContext::GetProvider()
|
|
|
|
{
|
|
|
|
return m_hCryptProv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
2024-05-23 23:22:12 +08:00
|
|
|
|
|
|
|
#ifdef ARCTOOL
|
|
|
|
void CCryptoContext::GenerateRandom(DWORD dwLength, BYTE* pbBuffer)
|
|
|
|
{
|
|
|
|
CryptGenRandom(m_hCryptProv, dwLength, pbBuffer);
|
|
|
|
}
|
|
|
|
#endif
|