From a1dec468e420430f6e8b4a4b796e0e81d85a62ad Mon Sep 17 00:00:00 2001 From: Sapna2001 Date: Wed, 11 Nov 2020 14:07:27 +0530 Subject: [PATCH] Password Strength in Java --- Java/Password_Strength/PasswordStrength.java | 46 ++++++++++++++++++++ Java/Password_Strength/Readme.md | 18 ++++++++ 2 files changed, 64 insertions(+) create mode 100644 Java/Password_Strength/PasswordStrength.java create mode 100644 Java/Password_Strength/Readme.md diff --git a/Java/Password_Strength/PasswordStrength.java b/Java/Password_Strength/PasswordStrength.java new file mode 100644 index 00000000..fc81c216 --- /dev/null +++ b/Java/Password_Strength/PasswordStrength.java @@ -0,0 +1,46 @@ +package password; + +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class PasswordStrength { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + System.out.println("Enter the password:"); + String password = sc.next(); + check(password); + } + + public static void check(String password) { + // TODO Auto-generated method stub + // ^ represents the starting of the string. + // (?=.*[a-z]) represent at least one lowercase character. + // (?=.*[A-Z]) represents at least one uppercase character. + // (?=.*\\d) represents at least one numeric value. + // (?=.*[-+_!@#$%^&*., ?]) represents at least one special character. + // . represents any character except line break. + // + represents one or more times. + String regex = "^(?=.*[a-z])(?=."+ "*[A-Z])(?=.*\\d)"+ "(?=.*[-+_!@#$%^&*., ?]).+$"; + + // Compile the ReGex + Pattern pattern = Pattern.compile(regex); + + // if the string entered is null then the output will be password invalid + if (password == null) { + System.out.println("Password Invalid."); + return; + } + + // Find match between given string & regular expression + Matcher matcher = pattern.matcher(password); + + if (matcher.matches()) + System.out.println("Password Valid and Strong."); + else + System.out.println("Weak Password."); + } + + } diff --git a/Java/Password_Strength/Readme.md b/Java/Password_Strength/Readme.md new file mode 100644 index 00000000..2b996c98 --- /dev/null +++ b/Java/Password_Strength/Readme.md @@ -0,0 +1,18 @@ +# Password Strength + +A program to check the strength of the password . For a strong password, the password must contain a mixture of upper case letters, a digit (including 0-9), and special characters with lower case letters. + +## Sample Input +Sapna123@ + +## Sample Output +Password Valid and Strong. + +## Screenshot +![demo](https://user-images.githubusercontent.com/56690856/98788220-90b3ed00-2426-11eb-94e6-c9115c61eebf.png) + +## Time Complexity +O(N) + +## Space Complexity +O(1)