-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpassword.cpp
More file actions
173 lines (152 loc) · 5.26 KB
/
Copy pathpassword.cpp
File metadata and controls
173 lines (152 loc) · 5.26 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*!
* \file password.cpp
*
* \copyright Copyright 2013 (C) Morpheus Being
*
* \author Morpheus Being
*
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
// compile as
// g++ -o password password.cpp -lboost_random
#include <iostream> // provide access to cout, cin, cerr
#include <string> // provide access to string
#include <vector> // provide vector
#include <boost/random/random_device.hpp>
#include <boost/nondet_random.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/uniform_int_distribution.hpp>
/**
* @brief Create a pronouncable password
*
*
* @param number - the number of passwords required to create
*/
void password_pronounce(int number)
{
// We first define the characters that we're going
// to allow. This is pretty much just the characters
// on a standard keyboard.
std::string chars("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ");
std::string vowels("aeiouAEIOU");
std::string numbers("1234567890");
std::string symbols("!@#$%^&*()`~-_=+[{]}\\|;:'\",<.>/? ");
// We use __random_device as a source of entropy, since we want
// passwords that are not predictable.
boost::random::random_device rng;
// Now develop uniform distribution
boost::random::uniform_int_distribution<> chars_dist(0, chars.size() - 1);
boost::random::uniform_int_distribution<> vowels_dist(0, vowels.size() - 1);
boost::random::uniform_int_distribution<> symbols_dist(0, symbols.size() - 1);
boost::random::uniform_int_distribution<> numbers_dist(0, numbers.size() - 1);
for (int i = 0; i < number; ++i) {
// Display group of char/vowel/char to get pronouncable password
for (int j = 0; j < 3; ++j) {
std::cout << chars[chars_dist(rng)];
std::cout << vowels[vowels_dist(rng)];
std::cout << chars[chars_dist(rng)];
}
// now add a symbol
std::cout << symbols[symbols_dist(rng)];
// now add 3 numbers to end
for (int i = 0; i < 3; ++i) {
std::cout << numbers[numbers_dist(rng)];
}
std::cout << std::endl;
}
}
/**
* @brief Create long passwords for system use - not for remembering
*
* @param num - the number of passwords to create
* @param len - the number of characters in each password
*/
void password_long(int& num, int len = 20)
{
std::string chars(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()"
"`~-_=+[{]}\\|;:'\",<.>/?");
boost::random_device rng;
boost::variate_generator<boost::random_device&, boost::random::uniform_int_distribution<> >
gen(rng, boost::random::uniform_int_distribution<>(0, chars.size()));
// the number asked for
for (int i = 0; i < num; ++i) {
// of the length requested - if none, use standard length of 20
for (int j = 0; j < len; ++j) {
std::cout << chars[gen()];
}
std::cout << std::endl;
}
}
/**
* @brief Show how to use the program.
*
* This can be called or shown on error on command line.
*
* @param name - the name of the calling program
*/
static void show_usage(std::string name)
{
std::cerr << "usage: " << name << " [options] " << std::endl;
std::cerr << " l - create [n] long password of [n] (20 is default) characters for system useage." << std::endl;
std::cerr << " p - create [n] pronouncable passwords as cvccvccvcsnnn " << std::endl;
std::cerr << " where c = constanant " << std::endl;
std::cerr << " v = vowel" << std::endl;
std::cerr << " s = symbol" << std::endl;
std::cerr << " n = digit" << std::endl;
}
static void show_version(std::string name)
{
std::cerr << name << " version: " << "0.1" << std::endl << std::endl;
}
int main(int argc, char *argv[])
{
int length = 20;
int number = 1;
if (argc < 2) {
show_usage(argv[0]);
return 1;
}
std::vector <std::string> commandline;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-h" ) || (arg == "--help") || (arg == "-?")) {
show_usage(argv[0]);
}
else if ((arg == "-v") || (arg == "--version")) {
show_version(argv[0]);
}
else if ((arg == "-l") || (arg == "--long")) {
// we want a set number of long passwords
if (i + 1 < argc) {
std::string myString = argv[++i];
number = atoi(myString.c_str());
}
// if yet another argument, we want them to be of set length
if (i + 1 < argc) {
std::string myString = argv[++i];
length = atoi(myString.c_str());
password_long(number, length);
} else {
password_long(number);
}
} else if ((arg == "-p") || (arg == "--pronounce")) {
if (i + 1 < argc) {
std::string myString = argv[++i];
number = atoi(myString.c_str());
}
password_pronounce(number);
} else {
// we have a problem
std::cerr << "Houston, we have a problem." << std::endl;
std::cerr << "Why don't you try again with -h or --help option. " << std::endl;
}
}
return 0;
}
// EOF