-
Notifications
You must be signed in to change notification settings - Fork 0
/
diceware.cpp
85 lines (73 loc) · 2.25 KB
/
diceware.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <algorithm>
#include <iostream>
#include <iterator>
#include <cstdlib>
#include <fstream>
#include <string>
#include <vector>
#include <sodium.h>
#if defined(__linux__)
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#endif
int main(int argc, char* argv[]){
int numWords = 0;
std::vector<std::string> wordList;
std::ifstream listFile("eff_large_wordlist.txt");
int listSize;
// Exit if not exactly one argument
if (argc != 2){
std::cerr << "Usage: " << argv[0] << " NUM" << std::endl;
return 1;
}
numWords = std::atoi(argv[1]);
// Exit if argument isn't a positive integer
if(numWords < 0){
std::cerr << "Usage: " << argv[0] << " NUM" << std::endl;
return 1;
}
// Test to ensure enough entropy has been collected
// to seed the random number generator under Linux
#if defined(__linux__) && defined(RNDGETENTCNT)
int fd;
int c;
if ((fd = open("/dev/random", O_RDONLY)) != -1) {
if (ioctl(fd, RNDGETENTCNT, &c) == 0 && c < 160) {
fputs("This system doesn't provide enough entropy to "
"quickly generate high-quality random numbers.\n"
"Installing the rng-utils/rng-tools or haveged "
"packages may help.\nOn virtualized Linux"
"environments, also consider using virtio-rng.\n"
"The service will not start until enough entropy"
"has been collected.\n", stderr);
}
(void) close(fd);
}
#endif
// Exit if sodium fails to initialize
if (sodium_init() == -1) {
std::cerr << "Failed to initialize RNG." << std::endl;
return 1;
}
// Exit if we can't open the input file
if(!listFile){
std::cerr << "Error opening input file." << std::endl;
return -1;
}
// Copy word list into a vector. This provides no error checking,
// assumes a well formed file.
std::copy_if(std::istream_iterator<std::string>(listFile),
std::istream_iterator<std::string>(),
back_inserter(wordList),
[](__attribute__((__unused__)) std::string s){
static bool state = true; return state=!state;
});
// Prints the requested number of words at random form the vector.
listSize = wordList.size();
for(int i = 0; i < numWords; ++i)
std::cout << wordList[randombytes_uniform(listSize)] << " ";
std::cout << std::endl;
return 0;
}