aboutsummaryrefslogtreecommitdiff
path: root/util/token.c
blob: cf6034a327b36b8af8b41e1b897c986b1e408f3c (plain)
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
#include "util/token.h"
#include "wlr/util/log.h"

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

bool generate_token(char out[static TOKEN_STRLEN]) {
	static FILE *urandom = NULL;
	uint64_t data[2];

	if (!urandom) {
		if (!(urandom = fopen("/dev/urandom", "r"))) {
			wlr_log_errno(WLR_ERROR, "Failed to open random device");
			return false;
		}
	}
	if (fread(data, sizeof(data), 1, urandom) != 1) {
		wlr_log_errno(WLR_ERROR, "Failed to read from random device");
		return false;
	}
	if (snprintf(out, TOKEN_STRLEN, "%016" PRIx64 "%016" PRIx64, data[0], data[1]) != TOKEN_STRLEN - 1) {
		wlr_log_errno(WLR_ERROR, "Failed to format hex string token");
		return false;
	}
	return true;
}