Skip to content

Instantly share code, notes, and snippets.

@bskim45
Forked from kbjorklu/CryptGenRandom.cpp
Created October 13, 2016 01:24
Show Gist options
  • Save bskim45/95479010efdcaf952202fe872c39f8ae to your computer and use it in GitHub Desktop.
Save bskim45/95479010efdcaf952202fe872c39f8ae 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment