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;
    }
}

Tuesday, December 19, 2017

Learn New Stuffs

https://howtodoinjava.com/java-tutorials-list-howtodoinjava/

Fingerprint DeviceId

https://github.com/Valve/fingerprintjs2

Getting location using HTML5 in Browser

Getting location using HTML5 in Browser:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Finding Me</title>
<script>
function getLocation()
{
    // Check whether browser supports Geolocation API or not
    if (navigator.geolocation) { // Supported
        //navigator.geolocation.getCurrentPosition(getPosition);
        navigator.geolocation.getCurrentPosition(getPosition, catchError);
    } else { // Not supported
        alert("Oop! This browser does not support HTML5 Geolocation.");
    }
}
function getPosition(position)
{
    document.getElementById("location").innerHTML = 
        "Latitude: " + position.coords.latitude + "<br>" +
        "Longitude: " + position.coords.longitude + "<br>" +
        "Altitude: " + position.coords.altitude + "<br>" +
        "Timestamp: " + position.coords.timestamp;
}
function catchError(error) {
    switch(error.code)
    {
        case error.TIMEOUT:
            alert("The request to get user location has aborted as it has taken too long.");
            break;
    case error.POSITION_UNAVAILABLE:
            alert("Location information is not available.");
            break;
    case error.PERMISSION_DENIED:
            alert("Permission to share location information has been denied!");
            break;
    default:
            alert("An unknown error occurred.");
    }
}
</script>
</head>
<body>
    <h1>Finding Me</h1>
    <button onclick="getLocation()">Where am I?</button>
    <p id="location"></p>
</body>
</html>

Reference: https://www.codeproject.com/Articles/1184757/HTML-Geolocation