Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Identifying Hashes Efficiently?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Identifying Hashes Efficiently?

    Hello,

    Before continuing a task that someone suggested for a mini-project, do you believe there is a more accurate way to identify hashes? Currently, I am using regex + length of hash to give the user possible hashes.

    Identifier class:
    package votek;
    /*Author: Votek/Karil Votek
     * Purpose: Class is used to run checks on possible hashes that the user has entered. 
     */
    public class Identifier {
     
    	//Hash entered by user is stored here
    	private String hash;
    	//String that will contain all possible hashes to share with user
    	private String results = "Possible Hashes: ";
    	//Method that runs the other hash methods checks and prints out the results.
    	public void runHashCheck(String hash) {
    		//setting Identifier's private instance 'hash' to the String given by user
    		this.hash = hash;
    		//Methods for each hash
    		checkMD2();
    		checkMD5();
    		checkSHA1();
    		//If the results variable did not change, tells user nothing was found. Otherwise, shares possible hashes.
    		if(results == "Possible Hashes: ") {
    			System.out.println("No possible hashes were found.");
    		} else {
    			System.out.println(results);
    			results = "Possible Hashes: ";
    		}
    	}
    	//Method to check for MD2 hash
    	private void checkMD2() { 
    		boolean isMD2 = hash.matches("[0-fA-F0-9]{32}");
    		if(isMD2) {
    			results += "MD2 ";
    		}
    	}
    	//Method to check for MD5 hash
    	private void checkMD5() {
    		boolean isMD5 = hash.matches("[0-fA-F0-9]{32}");
    		if(isMD5) {
    			results += "MD5 ";
    		}
    	}
    	//Method to check for SHA1 hash
    	private void checkSHA1() {
    		boolean isSHA1 = hash.matches("[0-fA-F0-9]{40}");
    		if(isSHA1) {
    			results+= "SHA1 ";
    		}
    	}
     
    	//More hash check methods will follow
     
    }// end of Identifier class

    HashIdentifier Class (main):
    /*Author: Votek/Karil Votek
     * Program: User inserts a hash and methods are ran to determine possible hashes the hash may be.
     */
    package votek;
     
    import java.util.Scanner;
     
    public class HashIdentifier {
     
    	public static void main(String[] args) {
    		//Creating & initializing an object for the Identifier class & Scanner class
    		Identifier identifier = new Identifier();
    		Scanner user = new Scanner(System.in);
    		//Used to stop the loop
    		char quit = 'n';
     
    		//Continue loop of user inputting hashes/checking hashes
    		while(Character.toLowerCase(quit) != 'q') {
    			System.out.print("Please enter the hash you would like to check: ");
    			identifier.runHashCheck(user.nextLine());
     
    			System.out.println("Enter 'q' if you would like to quit the program. Otherwise, enter any other character: ");
    			quit = user.nextLine().charAt(0);
    		}
     
    		user.close();
     
    	}
     
    } //end of HashIdentifier class


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Identifying Hashes Efficiently?

    Quote Originally Posted by Votek
    do you believe there is a more accurate way to identify hashes
    Nope. Hashes are one way finger prints. You can check the length and say it is possibly an MD5 hash but that's about it.

    As for your actual code I would refactor those methods to accept the hash as a parameter and return a boolean.

    public static boolean isPossiblyAnMD5(String hash) {
        if (hash.matches("[0-fA-F0-9]{32}")) {
            return true;
        }
        return false;
    }

    This will allow you to write unit tests and confirm it works for known MD5's.

    Also, while I am here:
    if(results == "Possible Hashes: ")

    You need to use .equals() for string equality.

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Votek (June 20th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Identifying Hashes Efficiently?

    Quote Originally Posted by ChristopherLowe View Post
    Nope. Hashes are one way finger prints. You can check the length and say it is possibly an MD5 hash but that's about it.

    As for your actual code I would refactor those methods to accept the hash as a parameter and return a boolean.

    public static boolean isPossiblyAnMD5(String hash) {
        if (hash.matches("[0-fA-F0-9]{32}")) {
            return true;
        }
        return false;
    }

    This will allow you to write unit tests and confirm it works for known MD5's.

    Also, while I am here:
    if(results == "Possible Hashes: ")

    You need to use .equals() for string equality.
    Thank you for the advice. After testing, is it okay to revert to the original code?

    (Completed project but went back and did suggestion/it worked with MD5's)

Similar Threads

  1. Help in recoding more efficiently
    By Bob M in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 25th, 2013, 02:49 PM
  2. Identifying something on screen.
    By cgskook in forum Java Theory & Questions
    Replies: 1
    Last Post: May 24th, 2013, 07:02 PM
  3. Identifying OO elements in this scenario
    By vinayjava in forum Object Oriented Programming
    Replies: 6
    Last Post: December 26th, 2012, 04:00 AM
  4. Efficiently processing data ....
    By rdegrijs in forum Java Servlet
    Replies: 0
    Last Post: July 10th, 2012, 06:25 PM
  5. Using arrays more efficiently in Java?
    By mjballa in forum Java Theory & Questions
    Replies: 1
    Last Post: February 4th, 2012, 08:53 PM