Skip to content

Instantly share code, notes, and snippets.

@roninhack
Forked from kbjorklu/CryptGenRandom.cpp
Created September 4, 2018 13:48
Show Gist options
  • Save roninhack/03090e0565db8bb34f0de0094bba8381 to your computer and use it in GitHub Desktop.
Save roninhack/03090e0565db8bb34f0de0094bba8381 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