Skip to content

Instantly share code, notes, and snippets.

@viperscape
Last active April 5, 2018 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viperscape/83a70446cca67cc55a6b291b4335b887 to your computer and use it in GitHub Desktop.
Save viperscape/83a70446cca67cc55a6b291b4335b887 to your computer and use it in GitHub Desktop.
random hex string from sqlite3's CSRNG
gcc -g -Wall -O1 sqlite3/sqlite3.c main.c -o randid
#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;
}
./randid 64
#expected output: E4ED9621EA47A138047C95FEB1F0CEE8FEA58BF58762B573DB8EE87998A8769
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment