Skip to content

Commit

Permalink
adding works
Browse files Browse the repository at this point in the history
  • Loading branch information
MiKaz003 committed Aug 23, 2024
1 parent 299b2fc commit e415253
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
57 changes: 56 additions & 1 deletion homework/password-check/validation.cpp
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";
}
}
18 changes: 17 additions & 1 deletion homework/password-check/validation.hpp
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);

0 comments on commit e415253

Please sign in to comment.