diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..b11d6218 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,57 @@ #include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file +#include +#include +#include +// 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(ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter)); + } else if (std::none_of(pass.begin(), pass.end(), ::isdigit)) { + return (static_cast(ErrorCode::PasswordNeedsAtLeastOneNumber)); + } else if (!checkSpecialChar(pass)) { + return (static_cast(ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter)); + } else if (pass.length() < 9) { + return (static_cast(ErrorCode::PasswordNeedsAtLeastNineCharacters)); + } else { + return (static_cast(ErrorCode::Ok)); + } +} + +int checkPassword(std::string& pass, std::string& repPass){ + if (doPasswordsMatch(pass, repPass)){ + return checkPasswordRules(pass); + } + else{ + return (static_cast (ErrorCode::PasswordsDoNotMatch)); + } +} + +std::string getErrorMessage(int errorNum){ + static const std::vector 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(errorMessages.size())){ + return errorMessages[errorNum]; + } + else{ + return "Unknown error"; + } +} \ No newline at end of file diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 85160868..ead034fc 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,2 +1,18 @@ +#pragma once +#include // TODO: I'm empty :) Put enum and function headers here. -// Don't forget the header guard - #pragma once \ No newline at end of file +// 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); \ No newline at end of file