0

I'm trying to find a string that shares the same hash result as a given string.

For Example:

If I have the String 'den' and use Java's hashcode() method the hash result = 99,341

String s = "den";
System.out.println(s.hashCode());
Result = 99,341

Knowing that the hashcode() method computes

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + 2[n-1]

How would I go about taking this result (99,341) and finding a string (of the same length) as 'den' with the equivalent hash value?

4
  • Create a dictionary of String words and their associated hashcode. You can't reverse the hash formula because you don't know the word length. Commented Aug 29, 2021 at 23:13
  • The word length is the same as the given String, so 3 in this case Commented Aug 29, 2021 at 23:24
  • Why do you need to do this? Commented Aug 29, 2021 at 23:25
  • Homework exercise Commented Aug 29, 2021 at 23:25

1 Answer 1

2

You need to solve this equation:

(x * 31^(3-1)) + (y * 31^(3-2)) + (z * 31^(3-3)) = 99341

It's a plane, every point in that plane is good for you as long as x,y,z are integers and between 0 and 255 (if you are only talking about ASCII).

One possible solution is x=100, y=101, z=110. To simply find another one, you can just change the order of two of them and see what would be the third one, e.g.:

x=101
y=???
z=110

(101 * 31^(3-1)) + (y * 31^(3-2)) + (110 * 31^(3-3)) = 99341

where y=70 (F) so eFn should have the same hashCode as den using the function in your question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.