Tuesday, December 26, 2017

Password Validator in Java

import java.util.regex.Pattern;

public class PasswordValidator {
    public static void main(String[] args) {
        String password1 = "deepak123";
        String password2 = "Depak123!@#";
        String password3 = "Deepak123";
        String password4 = "Depak@123";
        String password5 = "ddepak@123";
        String password6 = "dePak@123";
        String password7 = "monday123";
        String password8 = "ad9164";
        String password9 = "janduary123E$";
        String password10 = "Dfdfdf@123";
        String password11= "deepak1234";
        String password12 = "depak123455";

        System.out.println(validatePasswordCheck(password1));
        System.out.println(validatePasswordCheck(password2));
        System.out.println(validatePasswordCheck(password3));
        System.out.println(validatePasswordCheck(password4));
        System.out.println(validatePasswordCheck(password5));
        System.out.println(validatePasswordCheck(password6));
        System.out.println(validatePasswordCheck(password7));
        System.out.println(validatePasswordCheck(password8));
        System.out.println(validatePasswordCheck(password9));
        System.out.println(validatePasswordCheck(password10));
        System.out.println(validatePasswordCheck(password11));
        System.out.println(validatePasswordCheck(password12));
    }

    public static boolean validatePasswordCheck(String pwd){
        Pattern[] checks = {
                Pattern.compile("[!@#\\$%^&*()~`\\-=_+\\[\\]{}|:\\\";',\\./<>?]"),  //Checks Special Char ! @ # $ % ^ & * ( ) ~ ` - = _ + [ ] { } | : " ; ' , . / < > ?
                Pattern.compile("\\d+"),    //Checks Digits
                Pattern.compile("[A-Z]+"),  //Check Capital Letters
                Pattern.compile("[a-z]+"),  //Check Small Letters
                Pattern.compile("^.{8,20}$") };  //Check Length from 8 to 20.


        boolean ok = true;
        for(Pattern c : checks) {
            ok = ok && c.matcher(pwd).find();
        }    
        if(ok==true){
            if(!checkIfMonthNamesPresent(pwd) &&  !checkIfDayNamesPresent(pwd) && !IsNotSequentialChars(pwd)){
                return true;
            }
            else return false;
        }
        else return false;        
    }

    public static boolean checkIfMonthNamesPresent(String pwd) {
        String[] sArr = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
        String temp = pwd.toLowerCase();
        for(String x: sArr){
            if(temp.contains(x)){
                return true;
            }
        }
        return false;
    }
    public static boolean checkIfDayNamesPresent(String pwd) {
        String[] sArr = {"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
        String temp = pwd.toLowerCase();
        for(String x: sArr){
            if(temp.contains(x)){
                return true;
            }
        }
        return false;
    }

    public static boolean IsNotSequentialChars(String pwd) {  
        char c[] = pwd.toCharArray();
        int asciiCode = 0;
        boolean isConSeq = false;
        int previousAsciiCode = 0;
        int numSeqcount = 0;

        for (int i = 0; i < c.length; i++) {
            asciiCode = c[i];
            if(previousAsciiCode == asciiCode) {
                //if((previousAsciiCode + 1) == asciiCode) {  //For next consecutive incremental value
                numSeqcount++;
                if(numSeqcount >= 1) {
                    isConSeq = true;
                    break;
                }
            } else {
                numSeqcount = 0;
            }
            previousAsciiCode = asciiCode;
        }
        return isConSeq;
    }
}

No comments:

Post a Comment