|
| 1 | +package password; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +public class PasswordStrength { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + // TODO Auto-generated method stub |
| 11 | + Scanner sc = new Scanner(System.in); |
| 12 | + System.out.println("Enter the password:"); |
| 13 | + String password = sc.next(); |
| 14 | + check(password); |
| 15 | + } |
| 16 | + |
| 17 | + public static void check(String password) { |
| 18 | + // TODO Auto-generated method stub |
| 19 | + // ^ represents the starting of the string. |
| 20 | + // (?=.*[a-z]) represent at least one lowercase character. |
| 21 | + // (?=.*[A-Z]) represents at least one uppercase character. |
| 22 | + // (?=.*\\d) represents at least one numeric value. |
| 23 | + // (?=.*[-+_!@#$%^&*., ?]) represents at least one special character. |
| 24 | + // . represents any character except line break. |
| 25 | + // + represents one or more times. |
| 26 | + String regex = "^(?=.*[a-z])(?=."+ "*[A-Z])(?=.*\\d)"+ "(?=.*[-+_!@#$%^&*., ?]).+$"; |
| 27 | + |
| 28 | + // Compile the ReGex |
| 29 | + Pattern pattern = Pattern.compile(regex); |
| 30 | + |
| 31 | + // if the string entered is null then the output will be password invalid |
| 32 | + if (password == null) { |
| 33 | + System.out.println("Password Invalid."); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Find match between given string & regular expression |
| 38 | + Matcher matcher = pattern.matcher(password); |
| 39 | + |
| 40 | + if (matcher.matches()) |
| 41 | + System.out.println("Password Valid and Strong."); |
| 42 | + else |
| 43 | + System.out.println("Weak Password."); |
| 44 | + } |
| 45 | + |
| 46 | + } |
0 commit comments