Created
January 21, 2020 11:10
-
-
Save zer0cat/c7bba60b0e35c9a60d0b3acded3e9293 to your computer and use it in GitHub Desktop.
Generate random string in pure C WinApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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