如何加密散列JSON对象? [英] How to cryptographically hash a JSON object?

查看:285
本文介绍了如何加密散列JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题比第一次看起来更复杂。

The following question is more complex than it may first seem.

假设我有一个任意的JSON对象,它可能包含任何数量的数据,包括其他嵌套的JSON对象。我想要的是JSON数据的加密哈希/摘要,而不考虑实际的JSON格式化本身(例如:忽略换行符和JSON令牌之间的间隔差异)。

Assume that I've got an arbitrary JSON object, one that may contain any amount of data including other nested JSON objects. What I want is a cryptographic hash/digest of the JSON data, without regard to the actual JSON formatting itself (eg: ignoring newlines and spacing differences between the JSON tokens).

最后一部分是一个要求,因为JSON将由多个(de)序列化器在多个不同平台上生成/读取。我知道至少一个用于Java的JSON库,在反序列化过程中读取数据时会完全删除格式化。

The last part is a requirement, as the JSON will be generated/read by a variety of (de)serializers on a number of different platforms. I know of at least one JSON library for Java that completely removes formatting when reading data during deserialization. As such it will break the hash.

上面的任意数据子句也使事情变得复杂,因为它阻止我以给定的顺序取得已知字段,并在它们之前连接它们(大致思考Java的非加密hashCode()方法如何工作)。

The arbitrary data clause above also complicates things, as it prevents me from taking known fields in a given order and concatenating them prior to hasing (think roughly how Java's non-cryptographic hashCode() method works).

最后,将整个JSON字符串作为字节块进行散列(在反序列化之前)因为在计算散列时应该忽略JSON中的字段。

Lastly, hashing the entire JSON String as a chunk of bytes (prior to deserialization) is not desirable either, since there are fields in the JSON that should be ignored when computing the hash.

我不确定这个问题是否有很好的解决方案,但我欢迎任何方法或想法=)

I'm not sure there is a good solution to this problem, but I welcome any approaches or thoughts =)

推荐答案

当计算任何允许灵活性的数据格式的哈希时,

The problem is a common one when computing hashes for any data format where flexibility is allowed. To solve this, you need to canonicalize the representation.

例如,Twitter和其他服务使用的OAuth1.0a协议用于认证,需要请求消息的安全散列。为了计算哈希值,OAuth1.0a说你需要首先对字段进行字母表排序,用换行符分隔它们,删除字段名(众所周知),并对空值使用空行。签名或散列是根据规范化的结果计算的。

For example, the OAuth1.0a protocol, which is used by Twitter and other services for authentication, requires a secure hash of the request message. To compute the hash, OAuth1.0a says you need to first alphabetize the fields, separate them by newlines, remove the field names (which are well known), and use blank lines for empty values. The signature or hash is computed on the result of that canonicalization.

XML DSIG的工作方式相同 - 您需要在签名之前对XML进行规范化。有一个涵盖此的W3标准,因为它是签署的基本要求。有些人称之为c14n。

XML DSIG works the same way - you need to canonicalize the XML before signing it. There is a proposed W3 standard covering this, because it's such a fundamental requirement for signing. Some people call it c14n.

我不知道json的规范化标准。值得研究。

I don't know of a canonicalization standard for json. It's worth researching.

如果没有,您当然可以为您的特定应用程序使用建立约定。合理的开始可能是:

If there isn't one, you can certainly establish a convention for your particular application usage. A reasonable start might be:


  • 按名称按字母顺序对属性排序


  • 所有字符串值都使用双引号

  • 名称和冒号之间没有空格或一个空格,

  • 所有其他白色空间折叠为单个空格或无空格 - 选择一个

  • 排除您不想签名的任何属性(例如,保存签名本身的属性)

  • 使用您选择的算法

  • lexicographically sort the properties by name
  • double quotes used on all names
  • double quotes used on all string values
  • no space, or one-space, between names and the colon, and between the colon and the value
  • no spaces between values and the following comma
  • all other white space collapsed to either a single space or nothing - choose one
  • exclude any properties you don't want to sign (one example is, the property that holds the signature itself)
  • sign the result, with your chosen algorithm

您还可以考虑如何在JSON对象中传递该签名 - 可能建立一个知名的属性名称, nichols-hmac或某物,获得base64编码版本的哈希。此属性必须由散列算法显式排除。然后,任何JSON的接收者都能够检查哈希。

You may also want to think about how to pass that signature in the JSON object - possibly establish a well-known property name, like "nichols-hmac" or something, that gets the base64 encoded version of the hash. This property would have to be explicitly excluded by the hashing algorithm. Then, any receiver of the JSON would be able to check the hash.

规范化表示不需要是在应用程序中传递的表示。它只需要很容易生成给定一个任意的JSON对象。

The canonicalized representation does not need to be the representation you pass around in the application. It only needs to be easily produced given an arbitrary JSON object.

这篇关于如何加密散列JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆