Function 1: Generate a random password that's 7 characters. The random password MUST have 1 lowercase, 1 uppercase, 1 digit, and 1 symbol (valid symbols are ! @ # $ % *
Method signature should be
public static String generatePassword()
Function 2: Validate a passed in password. This password will be between 7 and 20 characters. It must comply with at least 3 of the following 4 rules: 1) at least 1 capital 2) at least one lowercase 3) at least 1 digit 4) at least 1 symbol (see above for valid symbols). If the password is too long or too short, return false. If it only meets 2 of the rules, return false. If it meets 3 or 4 of the rules, return true.
Method signature should be
public static boolean validatePassword(String password)
Method signature should be
public static String generatePassword()
Function 2: Validate a passed in password. This password will be between 7 and 20 characters. It must comply with at least 3 of the following 4 rules: 1) at least 1 capital 2) at least one lowercase 3) at least 1 digit 4) at least 1 symbol (see above for valid symbols). If the password is too long or too short, return false. If it only meets 2 of the rules, return false. If it meets 3 or 4 of the rules, return true.
Method signature should be
public static boolean validatePassword(String password)
- package password;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class RandomPassword
- {
- private static String password;
- // Method for gnerating random password each time
- public static String generatePassword()
- {
- password="";
- String temp[] = new String[7];
- char []number = new char[]{'0','1','2','3','4','5','6','7','8','9'};
- char []lowerCase = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
- char []upperCase = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- char []symbol = new char[]{'!','@','#','$','%','*'};
- char []allCase = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
- 'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',
- 'V','W','X','Y','Z','!','@','#','$','%','*'};
- Random random = new Random();
- int position = random.nextInt(10);
- temp[0] = ""+number[position];
- position = random.nextInt(26);
- temp[1] = ""+lowerCase[position];
- position = random.nextInt(26);
- temp[2] = ""+upperCase[position];
- position = random.nextInt(6);
- temp[3] = ""+symbol[position];
- for(int i=0;i<3 i="" span="">3>
- {
- position = random.nextInt(68);
- temp[4+i] = "" +allCase[position];
- }
- // Suffle Your password
- Collections.shuffle(Arrays.asList(temp));
- // temp[] to String
- for(String pass : temp)
- password=password+pass;
- return password;
- }
- //Method for validate password
- public static boolean validatePassword(String password)
- {
- int flag=0;
- // Validate Length
- if(password.length()<7 password.length="">20)7>
- return false;
- Pattern p = Pattern.compile("[a-z]");
- Matcher m = p.matcher(password);
- while(m.find())
- {
- flag++;
- break;
- }
- p = Pattern.compile("[A-Z]");
- m = p.matcher(password);
- while(m.find())
- {
- flag++;
- break;
- }
- p = Pattern.compile("\\d");
- m = p.matcher(password);
- while(m.find())
- {
- flag++;
- break;
- }
- p = Pattern.compile("['!','@','#','$','$',''%','*']");
- m = p.matcher(password);
- while(m.find())
- {
- flag++;
- break;
- }
- if(flag==0 || flag==1 || flag==2)
- return false;
- if(flag==3 || flag==4)
- return true;
- return true;
- }
- public static void main(String[] args) {
- System.out.println("passwors id "+RandomPassword.generatePassword());
- System.out.println(RandomPassword.validatePassword("aaaAaa)aa"));
- }
- }
Or

0 comments:
Post a Comment