I wrote this to generate a random password:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
string read(string value) { //read value
ifstream input;
int olength;
string line = "", output = "";
size_t pos;
bool a = true;
int i = 0;
input.open("pg_options.txt");
if (!input.is_open()) {
cout << "pg_options.txt missing.";
return "error";
}
while (getline(input, line)) {
pos = line.find(value);
if (pos != string::npos) {
while (a == true) {
if (line[i] == '=') {
i++;
break;
}
else {
i++;
}
}
olength = line.length() - value.length() - 1;
for (int i2 = 0; i2 < olength; i2++) {
output += line[i];
i++;
}
}
}
input.close();
return output;
}
char randupper() { //generate random upper case character
char uppercase[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
int i = rand() % 26;
return uppercase[i];
}
char randlower() { //generate random lower case character
char lowercase[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int i = rand() % 26;
return lowercase[i];
}
char randspecial() { //generate random special character
char special[7] = { '!', '#', '$', '%', '&', '*', '?' };
int i = rand() % 7;
return special[i];
}
char randnumbers() { //generate random number
char numbers[10] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
int i = rand() % 10;
return numbers[i];
}
void generate() { //generate the password
string output = "";
int i1=0, digits = 0, upper = 0, lower = 0, special = 0, numbers = 0;
digits = stoi(read("digits")); //get digits
if (read("include_upper_case") == "true") { //determine number of upper case characters
upper = rand() % (digits / 2) + 1;
digits -= upper;
}
if (read("include_lower_case") == "true") {
lower = rand() % (digits / 2) + 1;
digits -= lower;
}
if (read("include_special_characters") == "true") {
special = rand() % (digits / 2) + 1;
digits -= special;
}
if (read("include_numbers") == "true") {
numbers = digits;
}
else { //if numbers not included
if (read("include_upper_case") == "true") {
upper += digits;
}
else if (read("include_lower_case") == "true") {
lower += digits;
}
else if (read("include_special_characters") == "true") {
special += digits;
}
else {
cout << "error generating, please check your options.";
return;
}
}
for (int i = 0; i < stoi(read("digits")); i++) {
i1 = rand() % 4;
if (i1 == 0) { //if uppercase
if (upper > 0) {
output += randupper();
upper--;
}
else {
i--;
}
}
else if (i1 == 1) {
if (lower > 0) {
output += randlower();
lower--;
}
else {
i--;
}
}
else if (i1 == 2) {
if (special > 0) {
output += randspecial();
special--;
}
else {
i--;
}
}
else if (i1 == 3) {
if (numbers > 0) {
output += randnumbers();
numbers--;
}
else {
i--;
}
}
}
cout << output;
}
int main() {
generate();
return 0;
}
pg_options.txt:
include_special_characters=true
include_upper_case=true
include_lower_case=true
include_numbers=true
digits=10
However, it generates the same thing every time it runs, that is HM*nfx375g
, so it has same sequence of random numbers. (in generate(), upper is always 2, lower is always 4, special is always 1, numbers is always 3...) Is there a function that can generate different random numbers every time?
Aucun commentaire:
Enregistrer un commentaire