Skip to content

Commit ec042cd

Browse files
committed
password strength algorithm in c++
1 parent 91cdf62 commit ec042cd

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

algorithms/PasswordStrength.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class PasswordStrength
6+
{
7+
private:
8+
string password;
9+
public:
10+
11+
friend istream& operator>>(istream& fin, PasswordStrength& pass)
12+
{
13+
fin >> pass.password;
14+
return fin;
15+
}
16+
17+
void strength()
18+
{
19+
bool hasLetter = false;
20+
bool hasDigit = false;
21+
bool hasUpperCase = false;
22+
bool hasLowerCase = false;
23+
bool hasMuchLetter = false;
24+
25+
26+
if(this->password.length()>=8)
27+
hasMuchLetter = true;
28+
29+
for(int i=0; i<this->password.length(); i++)
30+
{
31+
if (isupper(this->password[i]))
32+
{
33+
hasUpperCase = true;
34+
}
35+
if (islower(this->password[i]))
36+
{
37+
hasLowerCase = true;
38+
}
39+
if (isalpha(this->password[i]))
40+
{
41+
hasLetter = true;
42+
}
43+
if(isdigit(this->password[i]))
44+
{
45+
hasDigit = true;
46+
}
47+
}
48+
49+
if(hasLetter && hasDigit && hasUpperCase && hasLowerCase && hasMuchLetter)
50+
cout<<"very strong";
51+
else if(hasLetter && hasDigit && (hasUpperCase || hasLowerCase) && hasMuchLetter)
52+
cout<<"strong";
53+
else if(hasLetter && hasDigit && hasUpperCase==false && hasLowerCase==false && hasMuchLetter)
54+
cout<<"good";
55+
else if(hasLetter && hasDigit && password.length()==false && hasUpperCase==false && hasLowerCase==false)
56+
cout<<"weak";
57+
else if((hasLetter==false && hasDigit) || (hasLetter && hasDigit==false) && password.length()==false && hasUpperCase==false && hasLowerCase==false)
58+
cout<<"very weak";
59+
else
60+
cout<<"weak";
61+
62+
63+
}
64+
65+
};
66+
67+
int main()
68+
{
69+
PasswordStrength pass;
70+
cin>>pass;
71+
pass.strength();
72+
}

0 commit comments

Comments
 (0)