password program

//password.cpp implementation file
#include “password.h”

 

//copy constructor
passwordmanager::passwordmanager(const passwordmanager& otherObject)
{
string * commonPasswords = new string[passwordSize];

}

//constructor
passwordmanager::passwordmanager(string fileName)
{
cout<<“got here”;
int counter = 0, passwordSize = 0;
bool atLeastWeak = false;
bool atLeastMedium = false;
bool strongPassword = false;
bool longEnough = false;
bool containsAlphaChar = false;
bool containsNumber = false;
bool containsSymbols = false;
bool hasComPass = true;
bool isComPass = false;

ifstream infile;

infile.open(“C:\\Users\\brian\\Desktop\\passwordsUpdated.txt”);

if(!infile)
{
cout<<“Error opening file! “;
//return 1;
}
//read in contents of file to a variable inorder to get counter size
while(!infile.eof())
{
infile>>passwords;
passwordSize++;
}
infile.close();

//now I have the size of the pass word list stored in passwordSize
//made pointer called commonPasswords, dynamically allocated data for string array
commonPasswords = new string[passwordSize+10];

//Open file again
infile.open(“C:\\Users\\brian\\Desktop\\passwordsUpdated.txt”);

if(!infile)
{
cout<<“Error opening file! “;
//return 1; since im in the constructor it won’t let me return anything??
}

//read in contents of file to array
while(!infile.eof())
{
infile>>commonPasswords[counter];
counter++;
}

printRules();

sort(commonPasswords, passwordSize);

string potentialPassword;
cout<<“Please enter a password! “;
cin>>potentialPassword;

while(!atLeastWeak)
{
longEnough = checkLength(potentialPassword);
containsAlphaChar = checkForLetters(potentialPassword);
containsNumber = checkForNumbers(potentialPassword);
containsSymbols = checkForSymbols(potentialPassword);

if(longEnough && containsAlphaChar && containsNumber && containsSymbols)
atLeastWeak = true;
else
{
cout<<“Invalid password! \n”;
break;
}
if (atLeastWeak)
atLeastMedium = checkForUpperLower(potentialPassword);

if (atLeastMedium)
{
isComPass = isCommon(potentialPassword, commonPasswords, passwordSize);
hasComPass = hasCommon(potentialPassword, commonPasswords, passwordSize);

if (!isComPass && !hasComPass)
strongPassword = true;
}

printPasswordResults(atLeastWeak, atLeastMedium, strongPassword);
}
}

 

//Deconstructor
passwordmanager::~passwordmanager()
{
delete [] commonPasswords;
}

//Function checks if inputted password is at least 8 characters long
bool passwordmanager::checkLength(string potentialPassword)
{
if(potentialPassword.length()>=8)
return true;
else
{
cout<<“Password is not the required length. “;
return false;
}
}

//Function checks if there is any character from alphabet in the input password
bool passwordmanager::checkForLetters(string potentialPassword)
{
int index = potentialPassword.find_first_of(“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”,0);
if(index >= 0)
return true;
else
{
cout<<“Password does not contain a letter. \n”;
return false;
}
}

//Function checks for number in the inputted password
bool passwordmanager::checkForNumbers(string potentialPassword)
{
int index = potentialPassword.find_first_of(“0123456789”,0);
if(index>=0)
return true;
else
{
cout<<“Password does not contain a number. \n”;
return false;
}
}

//Function checks for any of the special characters (!$%?) in the inputted password
bool passwordmanager::checkForSymbols(string potentialPassword)
{
int index = potentialPassword.find_first_of(“!$%?”,0);
if(index>=0)
return true;
else
{
cout<<“Password does not contain a special character! (!%&?) \n”;
return false;
}
}

//Function checks for both an uppercase and lowercase letter in inputted password
//islower and isupper are built in functions in c++
bool passwordmanager::checkForUpperLower(string potentialPassword)
{
bool foundUppercase = false;
bool foundLowercase = false;

for(int i = 0; i<potentialPassword.length(); i++)
if(islower(potentialPassword[i]))
foundLowercase = true;
for(int i = 0; i<potentialPassword.length(); i++)
if(isupper(potentialPassword[i]))
foundUppercase = true;

if(foundUppercase && foundLowercase)
return true;
else
return false;
}

// Takes an array of passwords, its size, and the password to search
int passwordmanager::binarySearch(const string listOfPasswords[], int listLength, string searchPassword)
{
int first = 0;
int last = listLength – 1;
int mid;

bool found = false;

// check if the middle number equals searchPassword and react according
while(first <= last && !found)
{
mid = (first + last) / 2;

if (listOfPasswords[mid] == searchPassword)
found = true;
else if (listOfPasswords[mid] > searchPassword)
last = mid – 1;
else
first = mid + 1;
}

if (found)
return mid;
else
return -1;
}

//Function takes in the inputted password
bool passwordmanager::isCommon(string potentialPassword, const string commonPasswords[], int passwordSize)
{
int index;
for(int i = 0; i<potentialPassword.length(); i++)
potentialPassword[i] = tolower(potentialPassword[i]);

index = binarySearch(commonPasswords, passwordSize, potentialPassword);

//Checks if inputted password matches any on file’s list
if(index >= 0)
return true;
else if(index == -1)
return false;
else
return false;
}

//Function checks if inputted password has a password from the array that holds common passwords
bool passwordmanager::hasCommon(string potentialPassword, const string commonPasswords[], int PasswordSize)
{
string pass;
int index=0;

for(int i = 0; i<potentialPassword.length(); i++)
potentialPassword[i] = tolower(potentialPassword[i]);

for(int i = 0; i<PasswordSize; i++)
{
pass = commonPasswords[i];
if(pass.length()>2)
{
index = potentialPassword.find(pass, 0);
if(index >= 0)
return true;
}
}
return false;
}

//PrintRules function
//prints rules for getting valid passwords and rules for weak medium & strong password

void passwordmanager::printRules()
{
cout << “Password Rules\n————————————————————-\n”;
cout << “1-The password must be at least 8 characters long.\n”;
cout << “2-It must contain at least one alphabet character [A-Z][a-z].\n”;
cout << “3-It must contain at least one numeric character [0-9].\n”;
cout << “4-It must contain at least one special character from this set: ! $ % ?”;
cout << “\n\nWEAK – the minimum (passes above rules)\n”;
cout << “MEDIUM – at least WEAK and contains upper and lower case letters\n”;
cout << “STRONG – at least MEDIUM and does not contain string from the common list\n”;

}

//Function prints whether inputted password is weak medium or string
void passwordmanager::printPasswordResults(bool weak, bool medium, bool strong)
{
if (weak && !medium && !strong)
{
cout << “\nThis password is Weak.\n”;
cout << “This password is okay, you should try again! \n\n”;
}
else if (weak && medium && !strong)
{
cout << “\nThe password is a Medium strength password.\n”;
cout << “The password is acceptable I guess.\n”;
cout << “If you want a strong password you should be more creative! \n\n”;
}
else if (weak && medium && strong)
{
cout << “\nThe password is a strong password.\n”;
cout << “Awesome Password!!! \n\n”;
}
}

//sort
void passwordmanager::sort(string commonPasswords[], int passwordSize){
int indexOfNextSmallest;
for(int index = 0; index < passwordSize – 1; index++)
{
indexOfNextSmallest = indexOfSmallest(commonPasswords, index, passwordSize);
swapPasswords(commonPasswords[index], commonPasswords[indexOfNextSmallest]);
}
}

// Swaps the values of password1 and password2.
void passwordmanager::swapPasswords(string& password1, string& password2)
{
string temp;
temp = password1;
password1 = password2;
password2 = temp;
}
int passwordmanager::indexOfSmallest(const string commonPasswords[], int startIndex, int passwordSize)
{
string min = commonPasswords[startIndex];
int indexOfMin = startIndex;
for (int index = startIndex + 1; index < passwordSize; index++)
if (commonPasswords[index] < min)
{
min = commonPasswords[index];
indexOfMin = index;
//min is the smallest of a[startIndex] through a[index]
}

return indexOfMin;
}

}
//___________________________________________________________________________

 

//password.h file
#pragma once
#include<iostream>
#include<string>
#include <fstream>

using namespace std;

class passwordmanager
{
public:

bool search(string password);

bool checkLength(string potentialPassword);
bool checkForLetters(string potentialPassword);
bool checkForNumbers(string potentialPassword);
bool checkForSymbols(string potentialPassword);
bool checkForUpperLower(string potentialPassword);
bool checkForUpperLower();
bool isCommon(string potentialPassword, const string commonPasswords[], int PasswordSize);
bool hasCommon(string potentialPassword, const string commonPasswords[], int PasswordSize);
void printRules();
void printPasswordResults(bool weak, bool medium, bool strong);
void sort(string commonPasswords[], int passwordSize);
void swapPasswords(string& password1, string& password2);
int indexOfSmallest(const string commonPasswords[], int startIndex, int passSize);
int binarySearch(const string listOfPasswords[], int listLength, string searchPassword);

string potentialPassword;
passwordmanager(string fileName);
passwordmanager(const passwordmanager& otherObject);
~passwordmanager();

private:
int counter;
int passwordSize;
bool atLeastWeak;
bool atLeastMediume;
bool strongPassword;
bool longEnough;
bool containsAlphaChar;
bool containsNumber;
bool containsSymbols;
bool hasComPass;
bool isComPass;
string *commonPasswords;
string passwords;
string fileName;

};

 

//__________________________________________________________________________________
//main.cpp driver code
#include <iostream>
#include <fstream>
#include <string>
#include “password.h”
using namespace std;

int main()
{

string password = “C:\\Users\\brian\\Desktop\\passwordsUpdated.txt”;
passwordmanager object(password);

return 0;

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: