1

I have been told Object.hashCode() could return a different hash even if the contents is the same eg an Array [1,2] and that it can't be relied on to validate object consistency. My understanding is the contents of my Array is the same then I will get the same hash back, even on a different jvm?

The reason I ask is that I want to:

  • Hash an array of numbers returned by a service call and send that hash down to the web front end
  • Send that hash back on submit of a form
  • Make a call to the service again hash it and compare this to the hash sent from the front end.

I thought simply doing Array.hashCode() would do the job but I'm now filled with doubt!

Sutty1000
  • 767
  • 1
  • 14
  • 32
  • http://stackoverflow.com/questions/1516843/java-object-hashcode-result-constant-across-all-jvms-systems should help – Sudhansu Choudhary Jul 09 '15 at 06:57
  • I think you are confusing common hash functions with cryptographic hash functions. You seem to be wanting this to prevent the user from performing some sort of exploit in your application. In that case, you don't want to use java's hash method. You should use cryptographic hash functions like md5 or sha – xp500 Jul 09 '15 at 07:04

3 Answers3

3

Arrays do not override hashCode, which means you get the default "identity hash code". Two identical arrays will (probably) give you different hash codes, and changing the contents of an array will not change its hash code.

However, you can use the static method Arrays.hashCode(yourArray). The documentation for this says that the return value is equal to Arrays.asList(yourArray).hashCode(), and all lists are guaranteed to use the same hash algorithm. This means that Arrays.hashCode will return consistent hash codes on different JVMs, as long as the elements of the array have consistent hash codes on different JVMs.

user253751
  • 57,427
  • 7
  • 48
  • 90
2

Using one of the static methods Arrays.hashCode instead of the default implementation of Object's hashCode (which is the implementation used by array objects) will give you what you need, since it computes the hash code based on the elements of the array, so two arrays containing the same elements in the same order would produce the same hashCode.

Eran
  • 387,369
  • 54
  • 702
  • 768
2

No, java.lang.Object.hashCode() will not always return the same value across multiple JVMs; even worse, when you stop and start the JVM again then a call to Object.hashCode() might return a different value than in the previous run.

Note that Object.hashCode() does not automatically compute a hash code based on the content of the object. So for two arrays with the same content, the hash code is most likely different:

int[] one = new int[]{1,2};
int[] two = new int[]{1,2};

System.out.println(one.hashCode());
System.out.println(two.hashCode());

prints out (for example; when you run it you will most likely get different numbers):

1725154839
1670675563

Use Arrays.hashCode(array) as Eran and immibis suggest if you want a hash code based on the content of the array:

System.out.println(Arrays.hashCode(one));
System.out.println(Arrays.hashCode(two));

prints:

994
994

Note that Arrays.hashCode() is not the same as calling hashCode() on an array directly.

Jesper
  • 202,709
  • 46
  • 318
  • 350