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 5 of 5

Thread: Unhashing possible?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Unhashing possible?

    Alright guys, I want to unhash some names. But I need code to unhash it, I'm NOT begging you for giving me that code, I just want to know if it is possible to unhash this. I would have no idea how, so that's also why I'm asking you

    Would it be possible to reverse the name.charAt(j) code? Because that's the hardest part.

    	public int getHash(String name) {
    		int hash = 0;
    		name = name.toUpperCase();
    		for (int j = 0; j < name.length(); j++)
    			hash = (hash * 61 + name.charAt(j)) - 32;
    		return hash;
    	}

    Thanks, Endian.
    Last edited by Endian; January 24th, 2012 at 02:35 PM.

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Unhashing possible?

    Would it be possible to reverse the name.charAt(j) code?
    Given a string and a value of j, charAt(j) tells you the j-th character of the string. The "reverse" process (I am guessing, you don't say) is to set the j-th position to some given character. Strings are immutable, but you have the StringBuilder class which does allow you to set the character at any given position. So to reverse the hash you might try builing up a StringBuilder instance, setting the characters as you find them, then, at the end, forming a string from the string builder.

    Because that's the hardest part.
    But there are more strings than Java ints! I suspect that reversing charAt() is the easiest part of unhashing, much like using a funnel to pour the contents into a glued together eggshell is the easiest part of unscambling an egg.

    But, certainly it should be reasonably easy to come up with some string that hashes to a given value.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Unhashing possible?

    Hashing is not like encryption, where all the information of the original value is captured and can be recaptured. Your hash is built upon the values of a String, and given you don't know those values when unshashing you don't have an easy way to decipher the hash. If you want reversibility, use an encryption algorithm or store the hash and String in a Map.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Unhashing possible?

    Hashing is a technique to map long numbers into shorter numbers. By definition there can be collisions, which means one hashcode can have many numbers that it was computer from.

    By numbers I mean any string of bytes in a computer.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Unhashing possible?

    Thanks all! That really helped.