Skip to content

Instantly share code, notes, and snippets.

@kbjorklu
Created August 23, 2013 09:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kbjorklu/6317361 to your computer and use it in GitHub Desktop.
Save kbjorklu/6317361 to your computer and use it in GitHub Desktop.
Sample code for the CryptGenRandom function.
#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;
}
@haephrati
Copy link

You need to add
#include <Wincrypt.h>
as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment