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 #1367

Closed
56 changes: 55 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
#include "validation.hpp"
// TODO: Put implementations here

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";
case ErrorCode::PasswordsDoNotMatch:
return "Passwords do not match";
default:
return "Error";
}
}

bool doPasswordsMatch(std::string pwd1, std::string pwd2) {
if (pwd1 == pwd2) {
return true;
}
return false;
}

ErrorCode checkPasswordRules(std::string pwd) {
std::string charToChekc = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()";
if (pwd.size() >= 9) {
for (int it = 0; it <= pwd.size(); it++) {
if (!isupper(pwd[it])) {
return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter;
}
if (!ispunct(pwd[it])) {
return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
}
if (!isdigit(pwd[it])) {
return ErrorCode::PasswordNeedsAtLeastOneNumber;
}
}
return ErrorCode::Ok;

} else if (pwd.size() < 9) {
return ErrorCode::PasswordNeedsAtLeastNineCharacters;
}
return ErrorCode::Ok;
}

ErrorCode checkPassword(std::string password, std::string repPassword) {
if (!doPasswordsMatch(password, repPassword)) {
return ErrorCode::PasswordsDoNotMatch;
}
return checkPasswordRules(password);
}
19 changes: 17 additions & 2 deletions homework/password-check/validation.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
// TODO: I'm empty :) Put enum and function headers here.
// Don't forget the header guard - #pragma once
#pragma once
#include <iostream>
#include <string>

enum class ErrorCode {
Ok,
PasswordNeedsAtLeastNineCharacters,
PasswordNeedsAtLeastOneNumber,
PasswordNeedsAtLeastOneSpecialCharacter,
PasswordNeedsAtLeastOneUppercaseLetter,
PasswordsDoNotMatch
};

std::string getErrorMessage(ErrorCode code);
bool doPasswordsMatch(std::string pwd1, std::string pwd2);
ErrorCode checkPasswordRules(std::string pwd);
ErrorCode checkPassword(std::string password, std::string repPassword);
Loading