Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.passwordDiagnoserAPP;
- import java.util.Random;
- public class Password implements Comparable<Password> {
- private static Random Random = new Random();
- private String password, strengthIndex;
- private boolean digit, lowerCaseAlphabetical, upperCaseAlphabetical, specialChar, maybeIllegal, lessThan8, moreThan16;
- private int strengthCounter;
- /**
- * Constructors:
- * @param password
- */
- public Password(String password)
- {
- //super();
- this.password = password;
- autoDiagnose();
- }
- public Password(Object password)
- {
- //super();
- this.password = password.toString();
- autoDiagnose();
- }
- public Password(Password password)
- {
- //super();
- this.password = password.getPassword();
- autoDiagnose();
- }
- /**
- * Getters and password (new) setter:
- * @return
- */
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- autoDiagnose();
- }
- public String getStrengthIndex() {
- return strengthIndex;
- }
- public boolean isDigit() {
- return digit;
- }
- public boolean isLowerCaseAlphabetical() {
- return lowerCaseAlphabetical;
- }
- public boolean isUpperCaseAlphabetical() {
- return upperCaseAlphabetical;
- }
- public boolean isSpecialChar() {
- return specialChar;
- }
- public boolean isMaybeIllegal() {
- return maybeIllegal;
- }
- public boolean isLessThan8() {
- return lessThan8;
- }
- public boolean isMoreThan16() {
- return moreThan16;
- }
- public int getStrengthCounter() {
- return strengthCounter;
- }
- /**
- * toString:
- */
- public String toString() {
- return "Password [password=" + password + ", strengthIndex="
- + strengthIndex + ", digit=" + digit
- + ", lowerCaseAlphabetical=" + lowerCaseAlphabetical
- + ", upperCaseAlphabetical=" + upperCaseAlphabetical
- + ", specialChar=" + specialChar + ", maybeIllegal="
- + maybeIllegal + ", lessThan8=" + lessThan8 + ", moreThan16="
- + moreThan16 + ", strengthCounter=" + strengthCounter + "]";
- }
- /**
- * password auto diagnose:
- */
- public void autoDiagnose(){
- lessThan8 = false;
- moreThan16 = false;
- digit = false;
- lowerCaseAlphabetical = false;
- upperCaseAlphabetical = false;
- specialChar = false;
- maybeIllegal = false;
- strengthCounter = 0;
- char currentChar;
- for (int i = 0; i < password.length(); i++)
- {
- currentChar = password.charAt(i);
- if (Character.isDigit(currentChar))
- digit = true;
- if (Character.isAlphabetic(currentChar))
- if (Character.isLowerCase(currentChar))
- lowerCaseAlphabetical = true;
- if (Character.isAlphabetic(currentChar))
- if (Character.isUpperCase(currentChar))
- upperCaseAlphabetical = true;
- if (Character.getNumericValue(currentChar) == -1)
- specialChar = true;
- if (!Character.isDefined(currentChar))
- maybeIllegal = true;
- }
- //strength counter: 0-extremely weak, 1-very weak, 2-weak, 3-strong, 4-very strong, 5-extremely strong.
- if(password.length() < 8){
- lessThan8 = true;
- strengthCounter--;
- }
- if(password.length() > 16){
- moreThan16 = true;
- strengthCounter++;
- }
- if(lowerCaseAlphabetical)
- strengthCounter++;
- if(upperCaseAlphabetical)
- strengthCounter++;
- if(digit)
- strengthCounter++;
- if(specialChar)
- strengthCounter++;
- //very weak:
- if(!digit && lowerCaseAlphabetical && !specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0001";
- if(!digit && !lowerCaseAlphabetical && !specialChar && upperCaseAlphabetical)
- strengthIndex = "@0002";
- if (digit && !lowerCaseAlphabetical && !specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0003";
- if (!digit && !lowerCaseAlphabetical && specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0004";
- //weak:
- if (!digit && lowerCaseAlphabetical && !specialChar && upperCaseAlphabetical)
- strengthIndex = "@0012";
- if (digit && lowerCaseAlphabetical && !specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0013";
- if (!digit && lowerCaseAlphabetical && specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0014";
- if (digit && !lowerCaseAlphabetical && !specialChar && upperCaseAlphabetical)
- strengthIndex = "@0023";
- if (!digit && !lowerCaseAlphabetical && specialChar && upperCaseAlphabetical)
- strengthIndex = "@0024";
- if (digit && !lowerCaseAlphabetical && specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0034";
- //strong:
- if(digit && lowerCaseAlphabetical && !specialChar && upperCaseAlphabetical)
- strengthIndex = "@0123";
- if (!digit && lowerCaseAlphabetical && specialChar && upperCaseAlphabetical)
- strengthIndex = "@0124";
- if (digit && lowerCaseAlphabetical && specialChar && !upperCaseAlphabetical)
- strengthIndex = "@0134";
- if (digit && !lowerCaseAlphabetical && specialChar && upperCaseAlphabetical)
- strengthIndex = "@0234";
- //very strong:
- if (digit && lowerCaseAlphabetical && specialChar && upperCaseAlphabetical)
- strengthIndex = "@1234";
- }
- /**
- * equals:
- */
- public boolean equals(Object obj)
- {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- Password other = (Password) obj;
- if (digit != other.digit) {
- return false;
- }
- if (lessThan8 != other.lessThan8) {
- return false;
- }
- if (lowerCaseAlphabetical != other.lowerCaseAlphabetical) {
- return false;
- }
- if (maybeIllegal != other.maybeIllegal) {
- return false;
- }
- if (moreThan16 != other.moreThan16) {
- return false;
- }
- if (password == null) {
- if (other.password != null) {
- return false;
- }
- } else if (!password.equals(other.password)) {
- return false;
- }
- if (specialChar != other.specialChar) {
- return false;
- }
- if (strengthCounter != other.strengthCounter) {
- return false;
- }
- if (strengthIndex == null) {
- if (other.strengthIndex != null) {
- return false;
- }
- } else if (!strengthIndex.equals(other.strengthIndex)) {
- return false;
- }
- if (upperCaseAlphabetical != other.upperCaseAlphabetical) {
- return false;
- }
- return true;
- }
- /**
- * compare passwords or password to String:
- * @param arg0
- * @return
- * Because the main character of passwords is their strength, This method compares password strength.
- */
- public int compareTo(Password p)
- {
- return p.strengthCounter - this.strengthCounter;
- }
- public int compareTo(String s)
- {
- Password p = new Password(s);
- if(this.strengthCounter > p.strengthCounter)
- return this.strengthCounter - p.strengthCounter;
- if(this.strengthCounter < p.strengthCounter)
- return p.strengthCounter - this.strengthCounter;
- return 0;
- }
- /**
- * PasswordImprover:
- * @param wS
- */
- public void passwordImprover(int wS)
- {
- StringBuilder passwordSB = new StringBuilder(password);
- int randomNum1, randomNum2;
- char[] lowerCases = "abcdefghijklmnopqrstuvwxyz".toCharArray();
- char[] upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
- char[] digits = "0123456789".toCharArray();
- char[] specials = "`~!@#$%^&*()-_=+[]{}|:;'<>.,?/".toCharArray();
- if(wS == 2)
- {
- if("@0001".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- /* Here is the main problem!!!
- * there's a need to fix or change the Switch cases to if(){}
- *
- * edit: add break; statment in the end of each case~s.
- */
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- break;
- }
- }
- if("@0002".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- }
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- break;
- }
- }
- if("@0003".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- break;
- }
- }
- if("@0004".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(3);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- break;
- case 2:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- break;
- }
- }
- }
- if(wS == 3)
- {
- if("@0001".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- }
- if("@0002".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- }
- if("@0003".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- }
- if("@0004".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(3);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- break;
- case 2:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- break;
- }
- }
- if("@0012".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- }
- if("@0013".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- }
- if("@0014".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- break;
- }
- }
- if("@0023".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- }
- }
- if("@0024".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- }
- break;
- }
- }
- if("@0034".equals(strengthIndex))
- {
- randomNum1 = Random.nextInt(2);
- switch(randomNum1)
- {
- case 0:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- }
- break;
- case 1:
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- }
- break;
- }
- }
- }
- if(wS == 4)
- {
- if("@0001".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(4);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 3)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0002".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(4);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 3)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0003".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(4);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- if (randomNum2 == 3)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0004".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(4);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]); // char choosing need check!!
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 3)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- }
- }
- if("@0012".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0013".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0014".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- }
- if("@0023".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0024".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- }
- if("@0034".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(3);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- if (randomNum2 == 2)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- }
- if("@0123".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, specials[Random.nextInt(specials.length)]);
- }
- }
- if("@0124".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, digits[Random.nextInt(digits.length)]);
- }
- }
- if("@0134".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, upperCases[Random.nextInt(upperCases.length)]);
- }
- }
- if("@0234".equals(strengthIndex))
- {
- for(int i = 0; i < password.length(); i++)
- {
- randomNum2 = Random.nextInt(2);
- if (randomNum2 == 1)
- passwordSB.setCharAt(i, lowerCases[Random.nextInt(lowerCases.length)]);
- }
- }
- }
- password = passwordSB.toString();
- autoDiagnose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement