Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

password - check solving problem #1388

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,60 @@
#include "validation.hpp"
// TODO: Put implementations here
#include <algorithm>

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";
}
}
20 changes: 19 additions & 1 deletion homework/password-check/validation.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
// 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

#pragma once
#include <string>

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);
Loading