From 9719bd7706e5e490f97a504597b93bba715daabd Mon Sep 17 00:00:00 2001 From: David Croft Date: Tue, 3 Dec 2019 14:34:11 +0000 Subject: [PATCH] Add library/cmake task --- passwords.cpp | 34 ++++++++++++++++++++++++++++++++++ passwords.h | 25 +++++++++++++++++++++++++ validate.cpp | 20 ++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 passwords.cpp create mode 100644 passwords.h create mode 100644 validate.cpp diff --git a/passwords.cpp b/passwords.cpp new file mode 100644 index 0000000..864d395 --- /dev/null +++ b/passwords.cpp @@ -0,0 +1,34 @@ +#include "passwords.h" + +int possible_combinations( int characters, int length ) +{ + if( characters <= 0 || length <=0 ) + return 0; + + return pow( characters, length ); +} + +bool good_password( std::string password ) +{ + int upper = 0, lower = 0, special = 0, digit = 0; + const int goodThreshold = 1000000000; + + // count types of characters in password + for( char c : password ) + { + if ( c >= 'A' && c <= 'Z' ) ++upper; // is uppercase character + else if( c >= 'a' && c <= 'z' ) ++lower; // is lowercase character + else if( c >= '0' && c <= '9' ) ++digit; // is a number + else ++special; + } + + // how many characters need to be checked + int characters = 0; + if( upper ) characters += 26; + if( lower ) characters += 26; + if( digit ) characters += 10; + if( special ) characters += 10; + + // good password if possible combinations > threshold + return possible_combinations( characters, password.length() ) > goodThreshold; +} diff --git a/passwords.h b/passwords.h new file mode 100644 index 0000000..9f1a638 --- /dev/null +++ b/passwords.h @@ -0,0 +1,25 @@ +#ifndef LIBARARY_H +#define LIBRARY_H + +#include +#include + +/** + * Calculate the number of possible passwords for the + * given character set and password length + * + * @param characters number of different possible characters in the password + * @param length password length + * @return number of possible passwords + */ +int possible_combinations( int characters, int length ); + +/** + * Returns if a given password is a good one + * + * @param password the password to test + * @return was it a good password + */ +bool good_password( std::string password ); + +#endif \ No newline at end of file diff --git a/validate.cpp b/validate.cpp new file mode 100644 index 0000000..53f9ce5 --- /dev/null +++ b/validate.cpp @@ -0,0 +1,20 @@ +#include "passwords.h" + +#include + +int main() +{ + std::string userInput; + + std::cout << "Enter a password to test if it is a good one" << std::endl; + std::cout << " Obviously you shouldn't use a real one: "; + + std::cin >> userInput; + + if( good_password( userInput ) ) + std::cout << "Good password" << std::endl; + else + std::cout << "Bad password" << std::endl; + + return 0; +} \ No newline at end of file