0
  • I'm working on a server based game and I'm sending my game data via a JSON from the server to the clients.
  • To minimize bandwidth use, I cache the game data JSON on the client machine so that they do not need to download the JSON from the server everytime.
  • I need to know if the JSON cached on the client is the same as the one on the server (it can become different either when we make changes on the server or if the user accessed and tweaked the cache on the client side).

So basically I need some kind of hashing technique to get a key unique to a JSON string that I can check to see if the JSON cached on the client is the same as the one stored on the server. The server will send that hash first to the client and only if it differs from the one generated from the client cache, then the client will request the whole JSON from the server again.

  • I do not care about data loss as the hash will never be converted back to string again.
  • I do not care about Hash difference due to different key orders in the JSON as I make sure they are always in the right order when I get the JSON from the server.
  • The hash shouldn't be environment specific. The same algorithm should generate the same hash regardless of environment, 32 or 64 bit and endianness.
Steven
  • 166,672
  • 24
  • 332
  • 435
orukinawa
  • 99
  • 5

2 Answers2

1

If your looking for a simple hash why not just use an MD5 hash? https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx basic hash that is easy to implement and use.

If you are looking to do an MD5 hash on the client using JS you might want to look at https://github.com/blueimp/JavaScript-MD5 (Non-tested by me, but I have heard good things :)

Brad Hess
  • 21
  • 2
  • Yes MD5 is the best thing I found so far. The only issue I have with it is implementing it on the server side so that a new hash is generated everytime a change is made to the JSON on the server. The best solution for me is an algorithm simple enough (and that doesn't require any libraries) to implement on both server and clients – orukinawa Jun 03 '15 at 04:17
0
  1. As Brad mentioned, any inbuilt Hashing technique in .NET should do the job for you.
  2. The other option is to use a UTC based timestamp. (LastModified) The server could send down this value, and if the client has the same value, then it is good to use. And whoever updates the file, can update the timestamp.
Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
  • I thought about this but the problem is that user can still tweak the cached values without modifying the time stamp. Since the time stamp is still the same as on the server, the JSON won't be pulled again – orukinawa Jun 03 '15 at 04:14
  • can you please explain what do you mean by 'tweak the cached values'? i think if anybody modifies the file, the hash or timestamp should be regenerated/updated. – Raja Nadar Jun 03 '15 at 17:04