-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
#include "validation.hpp" | ||
// TODO: Put implementations here | ||
#include <algorithm> | ||
#include <cctype> | ||
#include <vector> | ||
// TODO: Put implementations here | ||
bool doPasswordsMatch(std::string& pass1, std::string& pass2) { | ||
if (pass1 == pass2) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool checkSpecialChar(std::string& pass) { | ||
static const std::string special_chars = "!@#$%^&*()_-+=`~:;<,>.|"; | ||
return pass.find_first_of(special_chars) != std::string::npos; | ||
} | ||
|
||
int checkPasswordRules(std::string& pass) { | ||
if (std::none_of(pass.begin(), pass.end(), ::isupper)) { | ||
return (static_cast<int>(ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter)); | ||
} else if (std::none_of(pass.begin(), pass.end(), ::isdigit)) { | ||
return (static_cast<int>(ErrorCode::PasswordNeedsAtLeastOneNumber)); | ||
} else if (!checkSpecialChar(pass)) { | ||
return (static_cast<int>(ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter)); | ||
} else if (pass.length() < 9) { | ||
return (static_cast<int>(ErrorCode::PasswordNeedsAtLeastNineCharacters)); | ||
} else { | ||
return (static_cast<int>(ErrorCode::Ok)); | ||
} | ||
} | ||
|
||
int checkPassword(std::string& pass, std::string& repPass){ | ||
if (doPasswordsMatch(pass, repPass)){ | ||
return checkPasswordRules(pass); | ||
} | ||
else{ | ||
return (static_cast<int> (ErrorCode::PasswordsDoNotMatch)); | ||
} | ||
} | ||
|
||
std::string getErrorMessage(int errorNum){ | ||
static const std::vector<std::string> errorMessages = { | ||
"Ok", | ||
"Password needs to have at least nine characters", | ||
"Password needs to have at least one number", | ||
"Password needs to have at least one special character", | ||
"Password needs to have at least one uppercase letter", | ||
"Passwords do not match", | ||
}; | ||
if (errorNum >= 0 && errorNum <= static_cast<int>(errorMessages.size())){ | ||
return errorMessages[errorNum]; | ||
} | ||
else{ | ||
return "Unknown error"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
#pragma once | ||
#include <string> | ||
// TODO: I'm empty :) Put enum and function headers here. | ||
// Don't forget the header guard - #pragma once | ||
// Don't forget the header guard - #pragma once | ||
|
||
enum class ErrorCode{ | ||
Ok, | ||
PasswordNeedsAtLeastNineCharacters, | ||
PasswordNeedsAtLeastOneNumber, | ||
PasswordNeedsAtLeastOneSpecialCharacter, | ||
PasswordNeedsAtLeastOneUppercaseLetter, | ||
PasswordsDoNotMatch | ||
}; | ||
std::string getErrorMessage(int errorNum); | ||
bool doPasswordsMatch(std::string& pass1, std::string& pass2); | ||
int checkPasswordRules(std::string& pass); | ||
int checkPassword(std::string& pass, std::string repPass); | ||
bool checkSpecialChar(std::string& pass); |