Reading Time:- 3 min 34 sec
Reading Time:- 3 min 34 sec
#Password-Validator #Password-Validator-in-C #Password-Validator-in-C++ #Password-Validator-in-Java #Password-Validator-in-PythonBY TEJAS
A password is a string of characters that can be used to check a user's identity.
You know that passwords play a very important role in logging in or signing up on a website or app. You also know that a strong password uses many strings, numbers, and special characters. You may have seen on some websites that you can't easily put some terms in your password that will easily guess such as your name and your mobile number. This is because these such passwords can be effectively hacked. Your negligence is the main issue for password hack.
ADVERTISEMENT
Now, look at the demo password of [email protected]
. All the roles in it follow this password but keep in mind that this password can also be easily guessed or hacked. We have taken this password as a demo in this article.
As we have seen, your password should be secure, it should not contain your name or mobile number & your password should be at least 6-8 characters long. [email protected]
is the demo password we have used to check in the password validator.
ADVERTISEMENT
Also, the password validator can help you a lot to check whether your password is really strong or not. Password validator is a simple program that will let you know immediately whether the password you have entered is really strong or not.
The password validator is nothing more than a second or third, telling you whether your password is strong or weak. That helps to set your password Strong.
ADVERTISEMENT
So, now we come to our main point that we have created this password validator program using C, C++, Java, and Python.
In this, we have used a lot of if .... else
(if this happens to do this) statements. Using the if .... else
statement, we are telling our computer whether it is following this specific condition for the strong password or not. If so, then it sends it for the next condition. Also, we have used the boolean
(true or false or yes or no) operator.
ADVERTISEMENT
When you run this program, first you have to enter the string as a password in the program, after you entered the password the compilation and execution of the program will start. The password which you entered in it will be checked in a most maximum of the conditions. When the conditions are checked, it will give you your final output will tell you "Valid Password" or if the password you entered does not contain numbers or special characters then the program will tell you "The Password is weak".
ADVERTISEMENT
Password Validator program in C language:
#include<stdio.h>
#include<conio.h>
void main()
{
char pass[20],ch;
int i,number,special,capital,small;
number=special=capital=small=0;
printf("Enter Password>> ");
gets(pass);
for(i=0;pass[i]!='\0';i++)
{
if(pass[i]>='0' && pass[i]<='9')
number=1;
if(pass[i]>='a' && pass[i]<='z')
small=1;
if(pass[i]>='A' && pass[i]<='Z')
capital=1;
if(pass[i]=='!' || pass[i]=='@' || pass[i]=='#' || pass[i]=='$' || pass[i]=='%' || pass[i]=='*')
special=1;
}
if(number==0 || special==0 || capital==0 || small==0)
printf("\nInvalid Password");
else
printf("\nValid Password");
getch();
}
ADVERTISEMENT
Password Validator program in C++ language:
#include <iostream>
using namespace std;
void printStrongNess(string& input) {
int n = input.length();
// Checking lower alphabet in string
bool hasLower = false, hasUpper = false;
bool hasDigit = false, specialChar = false;
string normalChars = "abcdefghijklmnopqrstu" "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
for (int i = 0; i < n; i++) {
if (islower(input[i]))
hasLower = true;
if (isupper(input[i]))
hasUpper = true;
if (isdigit(input[i]))
hasDigit = true;
size_t special = input.find_first_not_of(normalChars);
if (special != string::npos)
specialChar = true;
}
// Strength of password
cout << "Strength of password is \"";
if (hasLower && hasUpper && hasDigit && specialChar && (n >= 8))
cout << "Strong\"" << endl;
else if ((hasLower || hasUpper) && specialChar && (n >= 6))
cout << "Moderate\"" << endl;
else
cout << "Weak\"" << endl;
}
int main() {
string input;
cout << "Please Enter your password: ";
std::cin >> input;
printStrongNess(input);
return 0;
}
ADVERTISEMENT
Password Validator program in Java language:
import java.util.regex.Pattern;
import java.util.Scanner;
public class Main
{
// 4-8 character password requiring numbers and alphabets of both cases
private static final String PASSWORD_REGEX =
"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$";
// 4-32 character password requiring at least 3 out of 4 (uppercase
// and lowercase letters, numbers & special characters) and at-most
// 2 equal consecutive chars.
private static final String COMPLEX_PASSWORD_REGEX =
"^(?:(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])|" +
"(?=.*\\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|" +
"(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|" +
"(?=.*\\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))(?!.*(.)\\1{2,})" +
"[A-Za-z0-9!~<>,;:_=?*+#.\"&§%°()\\|\\[\\]\\-\\$\\^\\@\\/]" +
"{8,32}$";
private static final Pattern PASSWORD_PATTERN =
Pattern.compile(COMPLEX_PASSWORD_REGEX);
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String password = sc.nextLine();
// Validate a password
if (PASSWORD_PATTERN.matcher(password).matches()) {
System.out.print("The Password " + password + " is valid");
}
else {
System.out.print("The Password " + password + " isn't valid");
}
}
}
ADVERTISEMENT
Password Validator program in Python language:
Here, you can clearly see how powerful Python is? because in other languages there are how many big lines of code but in python, we have to write very small code comparatively other programming languages.
l, u, p, d = 0, 0, 0, 0
s = input("Enter a Password: ")
if (len(s) >= 8):
for i in s:
# counting lowercase alphabets
if (i.islower()):
l+=1
# counting uppercase alphabets
if (i.isupper()):
u+=1
# counting digits
if (i.isdigit()):
d+=1
# counting the mentioned special characters
if(i=='@'or i=='$' or i=='_'):
p+=1
if (l>=1 and u>=1 and p>=1 and d>=1 and l+p+u+d==len(s)):
print("Valid Password")
else:
print("Invalid Password")
ADVERTISEMENT