Created
August 23, 2013 09:32
-
-
Save kbjorklu/6317361 to your computer and use it in GitHub Desktop.
Sample code for the CryptGenRandom function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <windows.h> | |
#pragma comment(lib, "advapi32.lib") | |
int main() | |
{ | |
HCRYPTPROV hProvider = 0; | |
if (!::CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) | |
return 1; | |
const DWORD dwLength = 8; | |
BYTE pbBuffer[dwLength] = {}; | |
if (!::CryptGenRandom(hProvider, dwLength, pbBuffer)) | |
{ | |
::CryptReleaseContext(hProvider, 0); | |
return 1; | |
} | |
for (DWORD i = 0; i < dwLength; ++i) | |
std::cout << std::hex << static_cast<unsigned int>(pbBuffer[i]) << std::endl; | |
if (!::CryptReleaseContext(hProvider, 0)) | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to add
#include <Wincrypt.h>
as well