From 50804d45945b99c4584a7c20ca5b6eb5e94814f4 Mon Sep 17 00:00:00 2001 From: Macias89-SC Date: Sat, 7 Sep 2024 23:52:37 +0200 Subject: [PATCH] password - check solving problem --- homework/password-check/validation.cpp | 60 +++++++++++++++++++++++++- homework/password-check/validation.hpp | 20 ++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..5bb368a4 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,60 @@ #include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file +#include + +bool doPasswordsMatch(const std::string& pass1, const std::string& pass2){ + if (pass1 == pass2){ + return true; + } + else{ + return false; + } +} + +bool checkSpecialChar(const std::string& pass) { + static const std::string special_chars = "!@#$%^&*()_-+=`~:;<,>.|"; + return pass.find_first_of(special_chars) != std::string::npos; +} + +ErrorCode checkPasswordRules(const std::string& pass) { + if (std::none_of(pass.begin(), pass.end(), ::isupper)) { + return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; + } else if (std::none_of(pass.begin(), pass.end(), ::isdigit)) { + return ErrorCode::PasswordNeedsAtLeastOneNumber; + } else if (!checkSpecialChar(pass)) { + return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter; + } else if (pass.length() < 9) { + return ErrorCode::PasswordNeedsAtLeastNineCharacters; + } + else { + return ErrorCode::Ok; + } +} + + +ErrorCode checkPassword(const std::string& pass, const std::string& repPass){ + if (doPasswordsMatch(pass, repPass)){ + return checkPasswordRules(pass); + } + else{ + return ErrorCode::PasswordsDoNotMatch; + } +} + +std::string getErrorMessage(const ErrorCode& errorNum){ + switch(errorNum){ + case ErrorCode::Ok: + return "Ok"; + case ErrorCode::PasswordNeedsAtLeastNineCharacters: + return "Password needs to have at least nine characters"; + case ErrorCode::PasswordNeedsAtLeastOneNumber: + return "Password needs to have at least one number"; + case ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter: + return "Password needs to have at least one special character"; + case ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter: + return "Password needs to have at least one uppercase letter"; + case ErrorCode::PasswordsDoNotMatch: + return "Passwords do not match"; + default: + 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..3b7e8d79 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,2 +1,20 @@ // 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 + +#pragma once +#include + +enum class ErrorCode{ + Ok, + PasswordNeedsAtLeastNineCharacters, + PasswordNeedsAtLeastOneNumber, + PasswordNeedsAtLeastOneSpecialCharacter, + PasswordNeedsAtLeastOneUppercaseLetter, + PasswordsDoNotMatch +}; + +std::string getErrorMessage(const ErrorCode& EnumCode); +bool doPasswordsMatch(const std::string& pass1, const std::string& pass2); +ErrorCode checkPasswordRules(const std::string& pass); +ErrorCode checkPassword(const std::string& pass, const std::string& repPass); +bool checkSpecialChar(const std::string& pass); \ No newline at end of file