Skip to content

Instantly share code, notes, and snippets.

@zer0cat
Created January 21, 2020 11:10
Show Gist options
  • Select an option

  • Save zer0cat/c7bba60b0e35c9a60d0b3acded3e9293 to your computer and use it in GitHub Desktop.

Select an option

Save zer0cat/c7bba60b0e35c9a60d0b3acded3e9293 to your computer and use it in GitHub Desktop.
Generate random string in pure C WinApi
#include <windows.h>
#include <ntsecapi.h>
#include <stdio.h>
DWORD random_num(DWORD min, DWORD max)
{
if (min > max)
return 0;
DWORD val;
if (!RtlGenRandom(&val, sizeof val))
return 0;
DWORD t = max - min + 1;
if (0 == min && ~0UL == max)
--t;
return val % t + min;
}
PCHAR random_chars(PCHAR b, DWORD n)
{
for (DWORD i = 0, j; i < n; ++i) {
j = random_num(0, 2);
b[i] = "0aA"[j] + random_num(0, j ? 'z'-'a' : '9'-'0');
}
return b;
}
void main(void)
{
CHAR s[32 + 1] = { 0 };
for (int i = 0; i < 32; ++i)
printf("%s\n", random_chars(s, sizeof(s)-1));
ExitProcess(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment