Skip to content

Instantly share code, notes, and snippets.

@viperscape viperscape/build.sh
Last active Apr 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.