Get the strength of a password as an integer between 0 and 5. - Android java.security

Android examples for java.security:Password

Description

Get the strength of a password as an integer between 0 and 5.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w  ww . j av  a 2s .c  om*/
     * Get the strength of a password as an integer between 0 and 5. Each of the following adds 1 to the strength -Password has more than 5
     * letters -Password has more than 8 letters -Password contains upper case letters -Password contains lower case letters -Password
     * contains numbers
     * 
     * @param password
     * @return
     */
    public static int getStrength(String password) {
        if (password == null)
            return 0;

        int passwordStrength = 0;
        if (password.length() > 5)
            passwordStrength++;

        if (password.toLowerCase() != password)
            passwordStrength++;

        if (password.toUpperCase() != password)
            passwordStrength++;

        if (password.length() > 8)
            passwordStrength++;

        if (getNumberDigits(password) > 0)
            passwordStrength++;

        return passwordStrength;
    }

    public static int getNumberDigits(String password) {
        if (password == null || password.length() == 0)
            return 0;

        int numDigits = 0;
        int length = password.length();
        for (int i = 0; i < length; i++)
            if (Character.isDigit(password.charAt(i)))
                numDigits++;

        return numDigits;
    }
}

Related Tutorials