Example #1
0
BOOL DeleteKeySet(const char *csp_name, const char *cont_name, DWORD flag)
{
// 公開鍵の削除
#ifndef MS_DEF_PROV
typedef unsigned long HCRYPTPROV;
#define MS_DEF_PROV				"Microsoft Base Cryptographic Provider v1.0"
#define MS_ENHANCED_PROV		"Microsoft Enhanced Cryptographic Provider v1.0"
#define CRYPT_DELETEKEYSET      0x00000010
#define CRYPT_MACHINE_KEYSET    0x00000020
#define PROV_RSA_FULL			1
#endif
	static HINSTANCE	advdll;
	static BOOL (WINAPI *pCryptAcquireContext)(HCRYPTPROV *, LPCSTR, LPCSTR, DWORD, DWORD);
	static BOOL (WINAPI *pCryptReleaseContext)(HCRYPTPROV, DWORD);

	if (!advdll) {
		advdll = ::LoadLibrary("advapi32.dll");
		pCryptAcquireContext = (BOOL (WINAPI *)(HCRYPTPROV *, LPCSTR, LPCSTR, DWORD, DWORD))
					 			::GetProcAddress(advdll, "CryptAcquireContextA");
		pCryptReleaseContext = (BOOL (WINAPI *)(HCRYPTPROV, DWORD))
					 			::GetProcAddress(advdll, "CryptReleaseContext");
	}
	if (!pCryptAcquireContext || !pCryptReleaseContext) return TRUE;

	HCRYPTPROV	hCsp = NULL;

	if (!pCryptAcquireContext(&hCsp, cont_name, csp_name, PROV_RSA_FULL, flag|CRYPT_DELETEKEYSET)) {
		if (pCryptAcquireContext(&hCsp, cont_name, csp_name, PROV_RSA_FULL, flag)) {
			pCryptReleaseContext(hCsp, 0);
			return	FALSE;
		}
	}
	return	TRUE;
}
Example #2
0
BOOL TDigest::Init(TDigest::Type _type)
{
	type = _type;

	if (hProv == NULL) {
		if (!pCryptAcquireContext(&hProv, NULL, MS_DEF_DSS_PROV, PROV_DSS, 0)) {
			pCryptAcquireContext(&hProv, NULL, MS_DEF_DSS_PROV, PROV_DSS, CRYPT_NEWKEYSET);
		}
	}
	if (hHash) {
		pCryptDestroyHash(hHash);
		hHash = NULL;
	}
	updateSize = 0;

	return	pCryptCreateHash(hProv, type == SHA1 ? CALG_SHA : CALG_MD5, 0, 0, &hHash);
}
Example #3
0
BOOL TGenRandom(void *buf, int len)
{
	static HCRYPTPROV hProv;

	if (hProv == NULL) {
		if (!pCryptAcquireContext(&hProv, NULL, MS_DEF_DSS_PROV, PROV_DSS, 0)) {
			pCryptAcquireContext(&hProv, NULL, MS_DEF_DSS_PROV, PROV_DSS, CRYPT_NEWKEYSET);
		}
	}

	if (hProv && pCryptGenRandom && pCryptGenRandom(hProv, (DWORD)len, (BYTE *)buf))
		return	TRUE;

	for (int i=0; i < len; i++) {
		*((BYTE *)buf + i) = (BYTE)(rand() >> 8);
	}

	return	 TRUE;
}