summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/src/id.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/util/src/id.c b/util/src/id.c
new file mode 100644
index 0000000..48bed23
--- /dev/null
+++ b/util/src/id.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <openssl/rand.h>
+
+#define IDLN 8
+#define CHARSET \
+ "abcdefghijklmnopqrstuvwxyz" \
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ "0123456789" \
+ "-_!~"
+#define CHARSETLN (sizeof(CHARSET) - 1)
+
+char* gen() {
+ char* id = malloc(IDLN + 1);
+ if (id == NULL) {
+ perror("malloc");
+ exit(1);
+ }
+
+ unsigned char buf[IDLN];
+ if (RAND_bytes(buf, IDLN) != 1) {
+ perror("RAND_bytes");
+ free(id);
+ exit(1);
+ }
+
+ for (
+ int i = 0;
+ i < IDLN || (id[IDLN] = '\0');
+ id[i++] = CHARSET[buf[i] % CHARSETLN]
+ );
+
+ return id;
+}
+
+int main(void) {
+ char* id = gen();
+ printf("%s", id);
+ free(id);
+}