From 5097ee542ebd331dddbc41cfb6b2b4f798a04271 Mon Sep 17 00:00:00 2001 From: szymon Date: Tue, 22 Sep 2026 10:44:40 +0200 Subject: [PATCH] implemented password-check --- homework/password-check/validation.cpp | 73 +++++++++++++++++++++++++- homework/password-check/validation.hpp | 21 +++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..7497a249 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,73 @@ #include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file +#include +#include +#include + +std::vector SPECIAL_CHARACTERS = {33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, 63, 64}; + +std::string getErrorMessage(ErrorCode code) { + switch (code) { + 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"; + default: + return "Passwords do not match"; + } + + return ""; +} + +bool doPasswordsMatch(const std::string& password1, const std::string& password2) { + return password1 == password2; +} + +ErrorCode checkPasswordRules(const std::string& password) { + if (password.size() < 9) { + return ErrorCode::PasswordNeedsAtLeastNineCharacters; + } + + bool numbers = false; + bool special = false; + bool upper = false; + for (auto chr : password) { + if (isdigit(chr) && !numbers) { + numbers = true; + } + if (!special) { + if (std::find(SPECIAL_CHARACTERS.begin(), SPECIAL_CHARACTERS.end(), chr) != SPECIAL_CHARACTERS.end()) { + special = true; + } + } + if (isupper(chr) && !upper) { + upper = true; + } + } + if (!numbers) { + return ErrorCode::PasswordNeedsAtLeastOneNumber; + } + + if (!special) { + return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter; + } + + if (!upper) { + return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; + } + + return ErrorCode::Ok; +} + +ErrorCode checkPassword(const std::string& password1, const std::string& password2) { + if (!doPasswordsMatch(password1, password2)) { + return ErrorCode::PasswordsDoNotMatch; + } + + return ErrorCode::Ok; +} \ No newline at end of file diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 85160868..f0f192f7 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,2 +1,21 @@ // 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 ErrorCode { + Ok, + PasswordNeedsAtLeastNineCharacters, + PasswordNeedsAtLeastOneNumber, + PasswordNeedsAtLeastOneSpecialCharacter, + PasswordNeedsAtLeastOneUppercaseLetter, + PasswordsDoNotMatch, +}; + +std::string getErrorMessage(ErrorCode code); + +bool doPasswordsMatch(const std::string& password1, const std::string& password2); + +ErrorCode checkPasswordRules(const std::string& password); + +ErrorCode checkPassword(const std::string& password1, const std::string& password2); \ No newline at end of file