Skip to content
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
73 changes: 72 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,73 @@
#include "validation.hpp"
// TODO: Put implementations here
#include <algorithm>
#include <cctype>
#include <vector>

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

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