Skip to content

Instantly share code, notes, and snippets.

@bmharper
Created July 25, 2013 09:56
Show Gist options
  • Save bmharper/6078376 to your computer and use it in GitHub Desktop.
Save bmharper/6078376 to your computer and use it in GitHub Desktop.
windows password generator
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "Advapi32")
const int CorpusSize = 26 * 2 + 10;
const char Corpus[CorpusSize + 1] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int main(int argc, char** argv)
{
const int nChars = 30;
HCRYPTPROV crypt;
byte buf[nChars];
char result[nChars + 1];
if ( !CryptAcquireContext( &crypt, NULL, NULL, PROV_RSA_AES, CRYPT_SILENT ) )
{
if ( !CryptAcquireContext( &crypt, NULL, NULL, PROV_RSA_AES, CRYPT_NEWKEYSET | CRYPT_SILENT ) )
{
printf( "CryptAcquireContext error (%d)\n", GetLastError() );
return 1;
}
}
memset( buf, 0, sizeof(buf) );
if ( CryptGenRandom( crypt, sizeof(buf), buf ) )
{
for ( int i = 0; i < nChars; i++ )
result[i] = Corpus[buf[i] % CorpusSize];
result[nChars] = 0;
printf( "%s\n", result );
}
else
{
printf( "CryptGenRandom error %d\n", GetLastError() );
}
CryptReleaseContext( crypt, 0 );
return result[0] != 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment