I am currently developing a password generator and just finished my first version. I know there is a lot to do but before I proceed with the next version I would like to have feedback on my current source code.
The embedded source code does not contain comments to reduce characters.
Main.cpp
void main()
{
Controller::PwGenerator* oController = new Controller::PwGenerator();
oController->Run();
delete oController;
}
PwGenerator.h
#pragma once
namespace View
{
class Terminal;
}
namespace Utility
{
class Security;
}
namespace Controller
{
class PwGenerator
{
private:
View::Terminal* oTerminal;
Utility::Security* oSecurity;
public:
PwGenerator();
void Run();
~PwGenerator();
};
}
PwGenerator.cpp
#include "PwGenerator.h"
#include "Terminal.h"
#include "Security.h"
#include <fstream>
#include <ctime>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
Controller::PwGenerator::PwGenerator()
{
this->oTerminal = new View::Terminal();
this->oSecurity = new Utility::Security();
}
void Controller::PwGenerator::Run()
{
bool bFinish = false;
int iMenuItem = 1;
std::ofstream oFstream;
std::time_t oTime = time(0);
std::tm* timePtr = std::localtime(&oTime);
this->oTerminal->Programminfo();
#ifdef WIN32
Sleep(3000);
#else
usleep(3000 * 1000);
#endif
do
{
this->oTerminal->Menu();
iMenuItem = this->oTerminal->SelectMenuOption();
switch (iMenuItem)
{
case 0:
bFinish = true;
break;
case 1:
this->oTerminal->Programminfo();
break;
case 2:
this->oSecurity->GeneratePw( this->oTerminal->MenuePwGen() );
if (this->oTerminal->MenueSavePw() == true)
{
this->oTerminal->Print("The password has been saved in a file.");
oFstream.open("PasswordGenerator.txt", std::fstream::app);
oFstream << "\r\n[" << (timePtr->tm_year + 1900) << "." << (timePtr->tm_mon+1) << "." << timePtr->tm_mday << " " << timePtr->tm_hour << ":" << timePtr->tm_min << ":" << timePtr->tm_sec << "] " << this->oSecurity->GetPw();
oFstream.close();
}
else
{
this->oTerminal->Print("Generated password:");
this->oTerminal->Print( this->oSecurity->GetPw() );
}
break;
default:
break;
}
} while (bFinish == false);
}
Controller::PwGenerator::~PwGenerator()
{
delete this->oTerminal;
}
Security.h
#pragma once
#include <string>
namespace Utility
{
class Security
{
private:
std::string sPassword;
std::string sAllowedCharacter;
public:
Security();
void GeneratePw(int iLength);
std::string GetPw();
~Security();
};
}
Security.cpp
Utility::Security::Security()
{
this->sPassword = std::string("");
this->sAllowedCharacter = std::string("abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY0123456789!$%&()[]{}=?");
}
void Utility::Security::GeneratePw(int iLength)
{
this->sPassword = this->sAllowedCharacter;
std::random_shuffle(this->sPassword.begin(), this->sPassword.end());
this->sPassword = this->sPassword.substr(0, iLength);
std::random_shuffle(this->sPassword.begin(), this->sPassword.end());
}
std::string Utility::Security::GetPw()
{
return this->sPassword;
}
Utility::Security::~Security()
{
}
oFstream
->oOfstream
? \$\endgroup\$Controller::PwGenerator
andUtility::Security
without them a code review is pointless. It would also be good to see amain()
to show a usage example. \$\endgroup\$