diff options
author | c+1 | 2024-09-01 21:48:56 -0400 |
---|---|---|
committer | c+1 | 2024-09-01 21:48:56 -0400 |
commit | 94daf8ad98b19afbd4320078b9d1ee25c9fb82f1 (patch) | |
tree | 28adfead942eb55dafdf67505bc326bb3f6e2f26 /util |
Allows for the uploading of files.
Diffstat (limited to 'util')
-rw-r--r-- | util/src/id.c | 40 |
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); +} |