-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.cpp
62 lines (47 loc) · 1.24 KB
/
generator.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
#include "generator.hpp"
#include "rng_factory.hpp"
#include <algorithm>
#include <sstream>
namespace pwgen
{
Generator::Generator(const std::string& wordFile)
: wordFile(wordFile)
{
rng = getRNG();
lineCount = CountLines();
if (!this->wordFile.good())
throw std::logic_error("No 'words.dic' file provided.");
}
std::string Generator::GeneratePassword(uint32 wordCount)
{
std::stringstream ss;
for (uint32 iWord = 0; iWord < wordCount; iWord++)
{
ss << GetRandomWord();
if (iWord < wordCount - 1)
ss << " ";
}
return ss.str();
}
std::string Generator::GetRandomWord()
{
// Search from the beginning
wordFile.seekg(std::ios::beg);
// Take random line/word
uint32 randomLine = rng->GenerateNumber(lineCount);
// Skip new line chars until we are at the correct line.
for (uint32_t i = 0; i < randomLine; i++)
wordFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string ret;
std::getline(wordFile, ret);
return ret;
}
uint32 Generator::CountLines()
{
wordFile.seekg(std::ios::beg);
return std::count(
std::istreambuf_iterator<char>(wordFile),
std::istreambuf_iterator<char>(), '\n'
);
}
}