Last active
April 5, 2018 20:39
-
-
Save viperscape/83a70446cca67cc55a6b291b4335b887 to your computer and use it in GitHub Desktop.
random hex string from sqlite3's CSRNG
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
gcc -g -Wall -O1 sqlite3/sqlite3.c main.c -o randid |
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 "sqlite3\sqlite3.h" | |
#include <stdio.h> | |
void rand_hex(int n, unsigned char *randp, char *hex) { | |
sqlite3_randomness(n, randp); | |
char *ptr = &hex[0]; | |
for (int i = 0; i < n; ++i) | |
ptr += sprintf (ptr, "%02X", randp[i]); | |
} | |
int main (int argc, char *argv[]) { | |
int n = 16; | |
if (argc > 1) { | |
int r = sscanf(argv[1], "%i", &n); | |
if ((r < 1) || (n > 2048)) return 1; | |
} | |
unsigned char randp[2049] = {'\0'}; | |
char rand_id[2049] = {'\0'}; | |
rand_hex(n, randp, rand_id); | |
rand_id[n] = '\0'; | |
fprintf(stdout, "%s", rand_id); | |
return 0; | |
} |
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
./randid 64 | |
#expected output: E4ED9621EA47A138047C95FEB1F0CEE8FEA58BF58762B573DB8EE87998A8769 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment