Jan 21, 2012 (last update: Jan 29, 2012)
Password Creator
Score: 3.5/5 (114 votes)
Original idea here
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
|
#ifndef _PARSER_HPP
#define _PARSER_HPP
//Parser.hpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
class Parser
{
std::ostream* Out;
int Argc;
char** Argv;
public:
Parser (int , char**);
~Parser ();
int getPos ();
void print ();
void setOut ();
static char randomChar ();
bool isDigit (char*);
};
#endif
|
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
|
//Parser.cpp
#include "Parser.hpp"
Parser::Parser (int Argc , char** Argv)
{
//Check if passed any arguments
if (Argc < 2)
{
//Print usage and then exit
std::cout << "Usage : " << Argv [0] << " --Out (Optional) (Password length)" << std::endl;
std::cout << "Examples : \n" << Argv [0] << " --Passwords.txt 1 43 2 6" << std::endl;
std::cout << Argv [0] << " 1 4" << std::endl;
//Exit with a failure
exit (1);
}
//Set argument counter and vector
this->Argc = Argc;
this->Argv = Argv;
//Set out
this->Out = 0;
}
Parser::~Parser ()
{
//Check if we need to delete anything
if (this->Out != &std::cout)
{
delete this->Out;
}
}
char Parser::randomChar ()
{
//String containing all possible characters
std::string Gen =
"abcedefghijkmnlopqrstuvwxyz"
"ABCDEFGHIJKMNLOPQRSTUVWXYZ"
"0123456789";
//Return a random character
return Gen [rand () % Gen.length ()];
}
bool Parser::isDigit (char* String)
{
//Temp variable(s)
int i = 0;
int iSize = strlen (String);
//Iterate through string to check for any non digit characters
for (i = 0 ; i < iSize ; i++)
{
//We found a digit stop the loop and return false
if (!isdigit (String [i]))
{
return false;
}
}
//All characters were digits so it is a number return true;
return true;
}
int Parser::getPos ()
{
//Temp variable(s)
int i = 0;
std::string Temp = "";
//Search vector
for (i = 0 ; i < this->Argc ; i++)
{
Temp = this->Argv [i];
if (Temp.find ("--") != std::string::npos)
{
return i;
}
}
//Return no position
return -1;
}
void Parser::setOut ()
{
//Set position
int iPos = this->getPos ();
//Check if valid position
if (iPos < 0)
{
this->Out = &std::cout;
return;
}
//Temp variable(s)
std::string Temp = this->Argv [iPos];
//Replace dashes with empty spaces
Temp.replace (Temp.find ("--") , 2 , "");
//Set new ofstream to out
this->Out = new std::ofstream (Temp.c_str () , std::ios::app);
}
void Parser::print ()
{
//Temp variable(s)
int i = 0;
int iPasswordSize = 0;
//Check if current string is a digit
for (i = 0 ; i < this->Argc ; i++)
{
//Set stream to convert numbers
std::stringstream Stream;
//Check if current argument is a number
if (this->isDigit (this->Argv [i]))
{
//It is a number convert it to an integer
Stream << this->Argv [i];
Stream >> iPasswordSize;
//Generate password
//Temp string containing password
std::string Temp;
//Resize it
Temp.resize (iPasswordSize);
//Generate random letters
std::generate (Temp.begin () , Temp.end () , Parser::randomChar);
//Shuffle the letters around
std::random_shuffle (Temp.begin () , Temp.end ());
//Print the passwords either to a file or to the console
*(this->Out) << Temp << std::endl;
}
}
}
|
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
|
//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Parser.hpp"
int main(int argc , char* argv[])
{
//Seed generator
srand ((unsigned) time (NULL));
//Create a parser
Parser Generator (argc , argv);
//Set the place to out put text
Generator.setOut ();
//Print passwords
Generator.print ();
return 0;
}
|