-
Notifications
You must be signed in to change notification settings - Fork 0
/
pw.c
30 lines (27 loc) · 954 Bytes
/
pw.c
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
#include <stdio.h>
#include <stdint.h>
#include <sys/random.h>
#include "words.h"
#define RANDOM_LEN 128
#define MIN_ENTROPY 64.0
int main(int argc, char **argv) {
uint16_t random_bytes[RANDOM_LEN] = {0};
// getrandom always succeeds for <=256B requests
getrandom(random_bytes, RANDOM_LEN*2, 0);
double pw_entropy = 0.0;
size_t rand_mask = (2 << (size_t)WORD_ENTROPY) - 1;
for (size_t random_index = 0; pw_entropy < MIN_ENTROPY; random_index++) {
// this should never happen
if (random_index >= RANDOM_LEN) return -1;
size_t rand = random_bytes[random_index] & rand_mask;
if (rand < WORD_COUNT) {
const char *word = WORDS[rand];
// skip space at the beginning for the first word
if (pw_entropy == 0.0) word++;
if (fputs(word, stdout) < 0) return -1;
pw_entropy += WORD_ENTROPY;
}
}
fputc('\n', stdout);
return 0;
}