Monday, March 11, 2013

Java Utility Functions - Password Generation and Validation

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)



  1. package password;

  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Random;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;

  8. public class RandomPassword
  9. {
  10. private static String password;
  11. // Method for gnerating random password each time
  12. public static String generatePassword()
  13. {
  14. password="";
  15. String temp[] = new String[7];
  16. char []number = new char[]{'0','1','2','3','4','5','6','7','8','9'};
  17. 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'};
  18. 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'};
  19. char []symbol = new char[]{'!','@','#','$','%','*'};
  20. 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',
  21. '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',
  22. 'V','W','X','Y','Z','!','@','#','$','%','*'};
  23. Random random = new Random();
  24. int position = random.nextInt(10);
  25. temp[0] = ""+number[position];
  26. position = random.nextInt(26);
  27. temp[1] = ""+lowerCase[position];
  28. position = random.nextInt(26);
  29. temp[2] = ""+upperCase[position];
  30. position = random.nextInt(6);
  31. temp[3] = ""+symbol[position];
  32. for(int i=0;i3>
  33. {
  34. position = random.nextInt(68);
  35. temp[4+i] = "" +allCase[position];
  36. }
  37. // Suffle Your password
  38. Collections.shuffle(Arrays.asList(temp));
  39. // temp[] to String
  40. for(String pass : temp)
  41. password=password+pass;
  42. return password;
  43. }
  44. //Method for validate password
  45. public static boolean validatePassword(String password)
  46. {
  47. int flag=0;
  48. // Validate Length 
  49. if(password.length()20)7>
  50. return false;
  51. Pattern p = Pattern.compile("[a-z]");
  52. Matcher m = p.matcher(password);
  53. while(m.find())
  54. {
  55. flag++;
  56. break;
  57. }

  58. p = Pattern.compile("[A-Z]");
  59. m = p.matcher(password);
  60. while(m.find())
  61. {
  62. flag++;
  63. break;
  64. }
  65.  
  66. p = Pattern.compile("\\d");
  67. m = p.matcher(password);
  68. while(m.find())
  69. {
  70. flag++;
  71. break;
  72. }
  73.  
  74. p = Pattern.compile("['!','@','#','$','$',''%','*']");
  75. m = p.matcher(password);
  76. while(m.find())
  77. {
  78. flag++;
  79. break;
  80. }
  81.  
  82.  
  83. if(flag==0 || flag==1 || flag==2)
  84. return false;
  85.  
  86. if(flag==3 || flag==4)
  87. return true;
  88.  
  89. return true;
  90. }
  91. public static void main(String[] args) {
  92. System.out.println("passwors id "+RandomPassword.generatePassword());
  93. System.out.println(RandomPassword.validatePassword("aaaAaa)aa"));
  94. }
  95. }
Or

0 comments: