aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/fics_getsalt.cpp
blob: 95705b0d6cca9458766075692b0de29ffc03b3c9 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * Written by Markus Uhlin <maxxe@rpblc.net>,
 * 25 Dec 2023.
 */

#include <cstdio>
#include <cstring>
#include <random>

#include "fics_getsalt.h"

char *
fics_getsalt(void)
{
	static bool init_done = false;
	static const char legal_index[] =
	    "./0123456789"
	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	    "abcdefghijklmnopqrstuvwxyz";
	static char salt[FICS_SALT_SIZE];

	if (!init_done) {
		strcpy(salt, FICS_SALT_BEG);
		init_done = true;
	}

	std::random_device rd;
	std::mt19937 gen(rd());
	std::uniform_int_distribution<size_t> dist(0, strlen(legal_index) - 1);

	for (size_t i = 7; i < sizeof salt; i++)
		salt[i] = legal_index[dist(gen)];
	salt[sizeof salt - 1] = '\0';
	return (&salt[0]);
}

#if SELF_TEST
int
main(void)
{
	puts(fics_getsalt());
	puts(fics_getsalt());
	puts(fics_getsalt());
	return 0;
}
#endif