Skip to content

Password check #1443

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

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
58 changes: 56 additions & 2 deletions 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
#include "validation.hpp"
#include <cstdlib>
#include <algorithm>
#include <cctype>

std::string getErrorMessage(ErrorCode test)
{
switch (test) {
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 "Nieznany blad";
}
};

bool doPasswordsMatch(std::string a, std::string b) {

return a == b;
}

ErrorCode checkPasswordRules(std::string a) {
if (a.size() >= 9) {
if(std::any_of(a.begin(), a.end(), [](char x) { return isdigit(x); } )){
if (std::any_of(a.begin(), a.end(), [](char x) { return ispunct(x); })) {
if (std::any_of(a.begin(), a.end(), [](char x) { return isupper(x); })) {
return ErrorCode::Ok;
}
else { return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; }
}
else {
return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
}
}
else { return ErrorCode::PasswordNeedsAtLeastOneNumber;}
}
else {
return ErrorCode::PasswordNeedsAtLeastNineCharacters;}
}

ErrorCode checkPassword(std::string a, std::string b) {
if (doPasswordsMatch(a, b)) {
return checkPasswordRules(a);
}
else return ErrorCode::PasswordsDoNotMatch;
}

21 changes: 19 additions & 2 deletions homework/password-check/validation.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
// TODO: I'm empty :) Put enum and function headers here.
// Don't forget the header guard - #pragma once
#ifndef VALIDATION_HPP
#define VALIDATION_HPP
#include <string>

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

std::string getErrorMessage(ErrorCode test);
bool doPasswordsMatch(std::string a, std::string b);
ErrorCode checkPasswordRules(std::string a);
ErrorCode checkPassword(std::string a, std::string b);

#endif
Loading