blob: 48bed23d4bc7d8cd9594bd9d13e9c5889ecb7e73 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
}
|