简体   繁体   English

检查JSON文件完整性

[英]Checking JSON file integrity

  • I'm working on a server based game and I'm sending my game data via a JSON from the server to the clients. 我正在开发基于服务器的游戏,并且正在通过JSON将游戏数据从服务器发送到客户端。
  • 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. 为了最大程度地减少带宽使用,我将游戏数据JSON缓存在客户端计算机上,这样他们就不必每次都从服务器下载JSON。
  • 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). 我需要知道客户端上缓存的JSON是否与服务器上的JSON相同(当我们在服务器上进行更改或用户访问并调整了客户端上的缓存时,它可能会变得不同)。

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. 因此,基本上,我需要某种哈希技术来获取JSON字符串唯一的密钥,我可以检查该密钥以查看客户端上缓存的JSON是否与服务器上存储的JSON相同。 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. 服务器将首先将该哈希发送给客户端,并且仅当该哈希与从客户端缓存生成的哈希不同时,客户端才会再次从服务器请求整个JSON。

  • 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. 我不关心由于JSON中的键顺序不同而导致的哈希差异,因为我确保从服务器获取JSON时它们始终处于正确的顺序。
  • The hash shouldn't be environment specific. 哈希值不应特定于环境。 The same algorithm should generate the same hash regardless of environment, 32 or 64 bit and endianness. 无论环境,32位或64位和字节序如何,相同的算法都应生成相同的哈希。

If your looking for a simple hash why not just use an MD5 hash? 如果您要查找简单的哈希,为什么不只使用MD5哈希呢? 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. https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.md5%28v=vs.110%29.aspx易于实现和使用的基本哈希。

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 :) 如果您希望使用JS在客户端上进行MD5哈希处理,则可能需要查看https://github.com/blueimp/JavaScript-MD5 (未经我测试,但我听过好消息:)

  1. As Brad mentioned, any inbuilt Hashing technique in .NET should do the job for you. 正如Brad所述,.NET中的任何内置哈希技术都可以为您完成这项工作。
  2. The other option is to use a UTC based timestamp. 另一个选择是使用基于UTC的时间戳。 (LastModified) The server could send down this value, and if the client has the same value, then it is good to use. (LastModified)服务器可以发送该值,并且如果客户端具有相同的值,则很好用。 And whoever updates the file, can update the timestamp. 谁更新文件,谁都可以更新时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM