Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef LIBARARY_H | ||
#define LIBRARY_H | ||
|
||
#include <math.h> | ||
#include <string> | ||
|
||
/** | ||
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "passwords.h" | ||
|
||
#include <iostream> | ||
|
||
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; | ||
} |