0

I am developing a mobile application (implemented in Java), which synchronises data with a web service (implemented in C#). I would like to check whether object received from the server (and saved to the database) has the correct data compared to what is stored on the server.

The solution I have come up with is to create a hash code of an object that could be easily compared between both mobile application and web service.

I was thinking about using (either on server side or in mobile application) native hashCode() method and then try to reimplement the way it works in one of the languages in the second language. Would that be possible or do I have to override hashCode() methods in both languages so that it would give same hash codes? Is there any better method to verify data integrity?

The object structure is not very complicated, though each object has many attributes.

syntagma
  • 23,346
  • 16
  • 78
  • 134

2 Answers2

1

You need to override hash functions on both sides if you want to use built in hash code methods for objects.

Building you own helpers that don't rely on hash code may be cleaner solution as rate of collision on integer hash code is relatively high. Something like SHA256 will produce longer hash.

In .Net (including code written in C#) GetHashCode is explicitly defied as not guaranteed to stay the same across restarts, app domains and CLR versions:

. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value this method returns may differ between .NET Framework versions and platforms, such as 32-bit and 64-bit platforms.

Java hashCode have similar requirement (my highlight):

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

You could always use a library like DeepEqual at the server side to check the value of each property in the object for you.

I'm guessing you want to keep your mobile Java application light on the CPU. That would prevent you having to do any extra work on the client.

a-h
  • 4,244
  • 2
  • 23
  • 29