当前位置: 首页>>代码示例>>C++>>正文


C++ CryptCreateHash函数代码示例

本文整理汇总了C++中CryptCreateHash函数的典型用法代码示例。如果您正苦于以下问题:C++ CryptCreateHash函数的具体用法?C++ CryptCreateHash怎么用?C++ CryptCreateHash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了CryptCreateHash函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: kull_m_crypto_pkcs5_pbkdf2_hmac

BOOL kull_m_crypto_pkcs5_pbkdf2_hmac(DWORD calgid, LPCVOID password, DWORD passwordLen, LPCVOID salt, DWORD saltLen, DWORD iterations, BYTE *key, DWORD keyLen, BOOL isDpapiInternal)
{
	BOOL status = FALSE;
	HCRYPTPROV hProv;
	HCRYPTHASH hHash;
	DWORD sizeHmac, count, i, j, r;
	PBYTE asalt, obuf, d1;

	if(CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
	{
		if(CryptCreateHash(hProv, calgid, 0, 0, &hHash))
		{
			if(CryptGetHashParam(hHash, HP_HASHVAL, NULL, &sizeHmac, 0))
			{
				if(asalt = (PBYTE) LocalAlloc(LPTR, saltLen + sizeof(DWORD)))
				{
					if(obuf = (PBYTE) LocalAlloc(LPTR, sizeHmac))
					{
						if(d1 = (PBYTE) LocalAlloc(LPTR, sizeHmac))
						{
							status = TRUE;
							RtlCopyMemory(asalt, salt, saltLen);
							for (count = 1; keyLen > 0; count++)
							{
								*(PDWORD) (asalt + saltLen) = _byteswap_ulong(count);
								kull_m_crypto_hmac(calgid, password, passwordLen, asalt, saltLen + 4, d1, sizeHmac);
								RtlCopyMemory(obuf, d1, sizeHmac);
								for (i = 1; i < iterations; i++)
								{
									kull_m_crypto_hmac(calgid, password, passwordLen, d1, sizeHmac, d1, sizeHmac);
									for (j = 0; j < sizeHmac; j++)
										obuf[j] ^= d1[j];
									if(isDpapiInternal) // thank you MS!
										RtlCopyMemory(d1, obuf, sizeHmac);
								}
								r = KIWI_MINIMUM(keyLen, sizeHmac);
								RtlCopyMemory(key, obuf, r);
								key += r;
								keyLen -= r;
							}
							LocalFree(d1);
						}
						LocalFree(obuf);
					}
					LocalFree(asalt);
				}
			}
			CryptDestroyHash(hHash);
		}
		CryptReleaseContext(hProv, 0);
	}
	return status;
}
开发者ID:Bluebear171,项目名称:mimikatz,代码行数:53,代码来源:kull_m_crypto.c

示例2: Java_org_company_security_csp_NativeCrypto_digestInit

/*
 * Class:     org_company_security_csp_NativeCrypto
 * Method:    digestInit
 * Signature: (Lorg/company/security/csp/CSPDigest;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_org_company_security_csp_NativeCrypto_digestInit(
		JNIEnv *env, jclass clazz, jobject jMessageDigest, jstring jHashAlgorithm) {

	HCRYPTPROV hCryptProv = (HCRYPTPROV) NULL;
	HCRYPTHASH hCryptHash = (HCRYPTHASH) NULL;
	DWORD dwBlockSize;
	BOOL result = FALSE;

	{
		ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
		DWORD dwProvId;
		jclass clazzCSPDigest;
		jmethodID mCSPPublicKeyInit;

		if(! FindProviderByAlg(env, NULL, algId, &dwProvId, &dwBlockSize)) {
			goto _m_leave;
		}

		if(! CryptAcquireContext(&hCryptProv, NULL, NULL, dwProvId, CRYPT_VERIFYCONTEXT)) {
			ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
			goto _m_leave;
		}

		// выделяем контекст хеш функции
		if(! CryptCreateHash(hCryptProv, algId, 0, 0, &hCryptHash)) {
			ThrowException(env, DIGEST_EXCEPTION, GetLastError());
			goto _m_leave;
		}

		// Get the method ID for the CSPPublicKey constructor
		clazzCSPDigest =
				(*env)->FindClass(env, "org/company/security/csp/CSPDigest");

		mCSPPublicKeyInit =
				(*env)->GetMethodID(env, clazzCSPDigest, "initDigest", "(JJI)V");

		// Create a new CSP public key
		(*env)->CallVoidMethod(env, jMessageDigest, mCSPPublicKeyInit,
				(jlong) hCryptProv, (jlong) hCryptHash, (jint) dwBlockSize);

		result = TRUE;
	}
	_m_leave:
	{
		if(! result) {
			if (hCryptHash)
				CryptDestroyHash((HCRYPTHASH) hCryptHash);

			if(hCryptProv)
				CryptReleaseContext((HCRYPTPROV) hCryptProv, 0);
		}
	}
}
开发者ID:alexey-su,项目名称:java-csp,代码行数:58,代码来源:cspDigest.c

示例3: open_hash

// generates SHA-1 hash of input
BOOL open_hash(void)
{
    BOOL bStatus = FALSE;

    // create hash object
    if (CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash)) {
        // hash input
        bStatus = CryptHashData(hHash, input, lstrlen(input), 0);
    }

    return bStatus;
}
开发者ID:ZhuHuiBeiShaDiao,项目名称:CryptoAPI-examples,代码行数:13,代码来源:dsagen.c

示例4: cryptoapi_hash_vector

int cryptoapi_hash_vector(ALG_ID alg, size_t hash_len, size_t num_elem,
						  const u8 *addr[], const size_t *len, u8 *mac)
{
	HCRYPTPROV prov;
	HCRYPTHASH hash;
	size_t i;
	DWORD hlen;
	int ret = 0;

	/*
	if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
		cryptoapi_report_error("CryptAcquireContext");
		return -1;
	}
	*/

	BOOL r = CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0);
	if (!r) {
		if (GetLastError() == NTE_BAD_KEYSET) {
			r = CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
		}
	}

	if (!r) {
		cryptoapi_report_error("CryptAcquireContext");
		return -1;
	}

	if (!CryptCreateHash(prov, alg, 0, 0, &hash)) {
		cryptoapi_report_error("CryptCreateHash");
		CryptReleaseContext(prov, 0);
		return -1;
	}

	for (i = 0; i < num_elem; i++) {
		if (!CryptHashData(hash, (BYTE *) addr[i], len[i], 0)) {
			cryptoapi_report_error("CryptHashData");
			CryptDestroyHash(hash);
			CryptReleaseContext(prov, 0);
		}
	}

	hlen = hash_len;
	if (!CryptGetHashParam(hash, HP_HASHVAL, mac, &hlen, 0)) {
		cryptoapi_report_error("CryptGetHashParam");
		ret = -1;
	}

	CryptDestroyHash(hash);
	CryptReleaseContext(prov, 0);

	return ret;
}
开发者ID:904498910,项目名称:ARO,代码行数:53,代码来源:crypto_alg.cpp

示例5: lw_sha1

void lw_sha1 (char * output, const char * input, size_t length)
{
   HCRYPTPROV hash_prov;
   DWORD hash_length = 20;

   crypt_init ();

   CryptCreateHash (crypt_prov, CALG_SHA1, 0, 0, &hash_prov);
   CryptHashData (hash_prov, (BYTE *) input, (DWORD) length, 0);
   CryptGetHashParam (hash_prov, HP_HASHVAL, (BYTE *) output, &hash_length, 0);
   CryptDestroyHash (hash_prov);
}
开发者ID:GeorgeBrown,项目名称:lacewing,代码行数:12,代码来源:global.c

示例6: _mesa_sha1_init

struct mesa_sha1 *
_mesa_sha1_init(void)
{
    HCRYPTHASH *ctx = malloc(sizeof(*ctx));

    if (!ctx)
        return NULL;

    CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
    CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx);
    return (struct mesa_sha1 *) ctx;
}
开发者ID:karolherbst,项目名称:mesa,代码行数:12,代码来源:mesa-sha1.c

示例7: getUUID

bool getUUID(std::string& uuid) {
	// Get the buffer length required for IP_ADAPTER_INFO.
	ULONG BufferLength = 0;
	BYTE* pBuffer = 0;
	if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo( 0, &BufferLength )) {
		// Now the BufferLength contain the required buffer length.
		// Allocate necessary buffer.
		pBuffer = new BYTE[ BufferLength ];
	} else {
		// Error occurred. handle it accordingly.
		return false;
	}

	// Get the Adapter Information.
	PIP_ADAPTER_INFO pAdapterInfo = reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);
	GetAdaptersInfo( pAdapterInfo, &BufferLength );
/*
	// Iterate the network adapters and print their MAC address.
	while( pAdapterInfo ) {
		// Assuming pAdapterInfo->AddressLength is 6.
		printf ("%02x:%02x:%02x:%02x:%02x:%02x \n",
			pAdapterInfo->Address[0],pAdapterInfo->Address[1],pAdapterInfo->Address[2],
			pAdapterInfo->Address[3],pAdapterInfo->Address[4],pAdapterInfo->Address[5]);
		// Get next adapter info.
		pAdapterInfo = pAdapterInfo->Next;
	}
*/
	// start encrypt
	HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
	CryptAcquireContext(&hProv, NULL,  NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
	CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
	CryptHashData(hHash, pAdapterInfo->Address, 6, 0);

	const int MD5LEN = 16;
	const CHAR rgbDigits[] = "0123456789abcdef";
	DWORD cbHash = MD5LEN;
	BYTE rgbHash[MD5LEN];
	uuid.resize(MD5LEN*2);
    if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)) {
        for (DWORD i = 0; i < cbHash; i++) {
			uuid[2*i] = rgbDigits[rgbHash[i] >> 4];
			uuid[2*i+1] = rgbDigits[rgbHash[i] & 0xf];
        }
    }
    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);

	// deallocate the buffer.
	delete[] pBuffer;
	return true;
}
开发者ID:Creativegame,项目名称:tinyswf,代码行数:52,代码来源:HelloWorldScene.cpp

示例8: Decrypt

std::string CStringUtils::Decrypt(const std::string& s, const std::string& password)
{
    std::string decryptstring;
    HCRYPTPROV hProv = NULL;
    // Get handle to user default provider.
    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
    {
        HCRYPTHASH hHash = NULL;
        // Create hash object.
        if (CryptCreateHash(hProv, CALG_SHA_512, 0, 0, &hHash))
        {
            // Hash password string.
            DWORD dwLength = DWORD(sizeof(WCHAR)*password.size());
            if (CryptHashData(hHash, (BYTE *)password.c_str(), dwLength, 0))
            {
                HCRYPTKEY hKey = NULL;
                // Create block cipher session key based on hash of the password.
                if (CryptDeriveKey(hProv, CALG_AES_256, hHash, CRYPT_EXPORTABLE, &hKey))
                {
                    dwLength = DWORD(s.size() + 1024); // 1024 bytes should be enough for padding
                    std::unique_ptr<BYTE[]> buffer(new BYTE[dwLength]);

                    std::unique_ptr<BYTE[]> strIn(new BYTE[s.size() + 1]);
                    if (buffer && strIn)
                    {
                        if (CStringUtils::FromHexString(s, strIn.get()))
                        {
                            // copy encrypted password to temporary buffer
                            memcpy(buffer.get(), strIn.get(), s.size());
                            dwLength = DWORD(s.size() / 2);
                            CryptDecrypt(hKey, 0, true, 0, (BYTE *)buffer.get(), &dwLength);
                            decryptstring = std::string((char*)buffer.get(), dwLength);
                            if (!decryptstring.empty() && (decryptstring[0] == '*'))
                            {
                                decryptstring = decryptstring.substr(1);
                            }
                            else
                                decryptstring.clear();
                        }
                    }
                    CryptDestroyKey(hKey);  // Release provider handle.
                }
            }
            CryptDestroyHash(hHash); // Destroy session key.
        }
        CryptReleaseContext(hProv, 0);
    }
    else
        DebugBreak();

    return decryptstring;
}
开发者ID:Kasper8660,项目名称:tortoisesvn,代码行数:52,代码来源:StringUtils.cpp

示例9: CryptoAPI_VerifyBegin

/**
  * Begins a signature verification hash context
  *
  * @param provider The crypt provider to use
  * @param hash     Out parameter for a handle to the hash context
  * @return CryptoX_Success on success, CryptoX_Error on error.
*/
CryptoX_Result
CryptoAPI_VerifyBegin(HCRYPTPROV provider, HCRYPTHASH* hash)
{
  BOOL result;
  if (!provider || !hash) {
    return CryptoX_Error;
  }

  *hash = (HCRYPTHASH)NULL;
  result = CryptCreateHash(provider, CALG_SHA1,
                           0, 0, hash);
  return result ? CryptoX_Success : CryptoX_Error;
}
开发者ID:CloCkWeRX,项目名称:core,代码行数:20,代码来源:cryptox.c

示例10: GIT_INLINE

GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
{
    if (ctx->ctx.cryptoapi.valid)
        CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle);

    if (!CryptCreateHash(ctx->prov->prov.cryptoapi.handle, CALG_SHA1, 0, 0, &ctx->ctx.cryptoapi.hash_handle)) {
        ctx->ctx.cryptoapi.valid = 0;
        return -1;
    }

    ctx->ctx.cryptoapi.valid = 1;
    return 0;
}
开发者ID:ZombiePork,项目名称:sonic-pi,代码行数:13,代码来源:hash_win32.c

示例11: CalculateMD5

/**
 * Calculates an MD5 hash for the given input binary data
 *
 * @param  data     Any sequence of bytes
 * @param  dataSize The number of bytes inside @data
 * @param  hash     Output buffer to store hash, must be freed by the caller
 * @param  hashSize The number of bytes in the output buffer
 * @return TRUE on success
*/
static BOOL
CalculateMD5(const char *data, DWORD dataSize, 
             BYTE **hash, DWORD &hashSize)
{
  HCRYPTPROV hProv = 0;
  HCRYPTHASH hHash = 0;

  if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL,
                           CRYPT_VERIFYCONTEXT)) {
    if (NTE_BAD_KEYSET != GetLastError()) {
      return FALSE;
    }
 
    // Maybe it doesn't exist, try to create it.
    if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, 
                             CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET)) {
      return FALSE;
    }
  }

  if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
    return FALSE;
  }

  if (!CryptHashData(hHash, reinterpret_cast<const BYTE*>(data), 
                    dataSize, 0)) {
    return FALSE;
  }

  DWORD dwCount = sizeof(DWORD);
  if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&hashSize, 
                        &dwCount, 0)) {
    return FALSE;
  }
  
  *hash = new BYTE[hashSize];
  ZeroMemory(*hash, hashSize);
  if (!CryptGetHashParam(hHash, HP_HASHVAL, *hash, &hashSize, 0)) {
    return FALSE;
  }

  if (hHash) {
    CryptDestroyHash(hHash);
  }

  if (hProv) {
    CryptReleaseContext(hProv,0);
  }

  return TRUE;
}
开发者ID:Andrel322,项目名称:gecko-dev,代码行数:60,代码来源:pathhash.cpp

示例12: CryptDestroyHash

CString &Cmd5Capi::Digest(CString & csBuffer)
{
    HCRYPTPROV hCryptProv; 
    HCRYPTHASH hHash; 
    BYTE bHash[0x7f]; 
    DWORD dwHashLen= 16; // The MD5 algorithm always returns 16 bytes. 
    DWORD cbContent= csBuffer.GetLength(); 
    BYTE* pbContent= (BYTE*)csBuffer.GetBuffer(cbContent); 


    if(CryptAcquireContext(&hCryptProv, 
		NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) 
	{

		if(CryptCreateHash(hCryptProv, 
			CALG_MD5,	// algorithm identifier definitions see: wincrypt.h
			0, 0, &hHash)) 
		{
			if(CryptHashData(hHash, pbContent, cbContent, 0))
			{

				if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0)) 
				{
					// Make a string version of the numeric digest value
					csDigest.Empty();
					CString tmp;
					  for (int i = 0; i<16; i++)
					  {
						tmp.Format("%02x", bHash[i]);
						csDigest+=tmp;
					  }

				}
				else csDigest=_T("Error getting hash param"); 

			}
			else csDigest=_T("Error hashing data"); 
		}
		else csDigest=_T("Error creating hash"); 

    }
    else csDigest=_T("Error acquiring context"); 


    CryptDestroyHash(hHash); 
    CryptReleaseContext(hCryptProv, 0); 
	csBuffer.ReleaseBuffer();
    return csDigest; 


}
开发者ID:fusefm,项目名称:fusic,代码行数:51,代码来源:md5Capi.cpp

示例13: strlen

bool StandardEncryption::GetMD5Hash (char *szPassword, char *szOutbuf)
{
	HCRYPTPROV hCryptProv; 
    HCRYPTHASH hHash; 
    BYTE bHash[0x7f]; 
    DWORD dwHashLen= 16; // The MD5 algorithm always returns 16 bytes. 
    DWORD cbContent= strlen (szPassword);
    BYTE* pbContent= (BYTE*) szPassword;

	char szFinal[SIZE_STRING];
	ZeroMemory (szFinal, SIZE_STRING);

	char szCurchar[SIZE_NAME];
	
    if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) {
		if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
			if(CryptHashData(hHash, pbContent, cbContent, 0)) {
				if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0)) {
					
					for (int i=0;i<16;i++) {
						ZeroMemory (szCurchar, SIZE_NAME);
						sprintf_s (szCurchar, SIZE_NAME, "%02x", bHash[i]);

						strcat_s (szFinal, SIZE_STRING, szCurchar);
					}

				} else {
					OutputText ("GetMD5Hash: Error getting hash param!");
					return false;
				}
			} else {
				OutputText ("GetMD5Hash: Error Hashing data!");
				return false;
			}
		} else {
			OutputText ("GetMD5Hash: Error Creating Hash!");
			return false;
		}
	} else {
		OutputText ("GetMD5Hash: Error Aquiring Context!");
		return false;
	}

	ZeroMemory (szOutbuf, SIZE_STRING);
	strcpy_s (szOutbuf, SIZE_STRING, szFinal);

	CryptDestroyHash(hHash); 
    CryptReleaseContext(hCryptProv, 0); 	
    return true; 
}
开发者ID:dannydraper,项目名称:CedeCryptPortable,代码行数:50,代码来源:StandardEncryption.cpp

示例14: TOE

CryptoHash::CryptoHash(HCRYPTPROV cryptProv, ALG_ID algId, const ByteVector& data)
         :handle(NULL)
{
    TOE(CryptCreateHash(cryptProv, algId, 0, 0, &handle), "CryptCreateHash");
    try
    {
        update(data);
    }
    catch (exception&)
    {
        CryptDestroyHash(handle);
        throw;
    }
}
开发者ID:adiantum,项目名称:NMPRK,代码行数:14,代码来源:CryptoProxyWin32.cpp

示例15: _ASSERTE

HRESULT CAssemblyStream::Init (LPCOLESTR pszPath, DWORD dwFormat)
{
    HRESULT                            hr = S_OK;
    DWORD                              cwPath;
    BOOL                               bRet;

    _ASSERTE(pszPath);

    _dwFormat = dwFormat;
    cwPath = lstrlenW(pszPath) + 1;

    _ASSERTE(cwPath < MAX_PATH);
    memcpy(_szPath, pszPath, sizeof(TCHAR) * cwPath);

    _hf = WszCreateFile(pszPath, GENERIC_WRITE, 0 /* no sharing */,
                     NULL, CREATE_NEW, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (_hf == INVALID_HANDLE_VALUE) {
        hr = HRESULT_FROM_WIN32(GetLastError());
        ReleaseParent(hr);
        goto Exit;
    }

    if (!g_hProv) {
        HCRYPTPROV hProv;
  
        bRet = CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
        if (!bRet) {
            hr = HRESULT_FROM_WIN32(GetLastError());
            ReleaseParent(hr);
            goto Exit;

        }

        if (InterlockedCompareExchangePointer((void **)&g_hProv, (void *)hProv, 0)) {
            // Lost the race. Release our provider.
            CryptReleaseContext(hProv, 0);
        }
    }

    bRet = CryptCreateHash(g_hProv, CALG_SHA1, 0, 0, &_hHash);
    if (!bRet) {
        hr = HRESULT_FROM_WIN32(GetLastError());
        ReleaseParent(hr);
        goto Exit;
    }

Exit:
    return hr;
}
开发者ID:Anupam-,项目名称:shared-source-cli-2.0,代码行数:49,代码来源:asmstrm.cpp


注:本文中的CryptCreateHash函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。