Password strength checker in Android [closed]
Want to improve thi开发者_如何学Pythons question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionHow can we implement a password strength checker in Android?
This seems to work:
//ASSERT password not null
private float getRating(String password) throws IllegalArgumentException {
if (password == null) {throw new IllegalArgumentException();}
int passwordStrength = 0;
if (password.length() > 5) {passwordStrength++;} // minimal pw length of 6
if (password.toLowerCase()!= password) {passwordStrength++;} // lower and upper case
if (password.length() > 8) {passwordStrength++;} // good pw length of 9+
int numDigits= Utilities.getNumberDigits(password);
if (numDigits > 0 && numDigits != password.length()) {passwordStrength++;} // contains digits and non-digits
return (float)passwordStrength;
}
Utilities class
public static int getNumberDigits(String inString){
if (isEmpty(inString)) {
return 0;
}
int numDigits= 0;
int length= inString.length();
for (int i = 0; i < length; i++) {
if (Character.isDigit(inString.charAt(i))) {
numDigits++;
}
}
return numDigits;
}
public static boolean isEmpty(String inString) {
return inString == null || inString.length() == 0;
}
精彩评论