Skip to content
Permalink
Browse files
Add library/cmake task
  • Loading branch information
ac0745 committed Dec 3, 2019
1 parent c99ba70 commit 9719bd7706e5e490f97a504597b93bba715daabd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
@@ -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;
}
@@ -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
@@ -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;
}

0 comments on commit 9719bd7

Please sign in to comment.