TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Hashing Apples, Bananas and Cherries

33 点作者 muscaw超过 2 年前

3 条评论

kentonv超过 2 年前
TL;DR:<p>Hash functions operate on byte strings. But, sometimes you want to hash data structures. So you serialize the structure and hash the serialization.<p>You need to be very careful about how you serialize. It&#x27;s critical that the serialization actually be unique to the particular input. E.g. if you have two different types of data structures that you hash, it&#x27;s important that no instance of the first type has the same serialization as some instance of the second type. Another common problem is when people hash a structure containing multiple values by simply concatenating the values and hashing the concatenation. If you serialize both `[&quot;a&quot;, &quot;bc&quot;]` and `[&quot;ab&quot;, &quot;c&quot;]` as &quot;abc&quot;, then they will have the same hash. That&#x27;s bad!<p>One way to think about this is to design your serialization such that it can be unambiguously parsed back to the original structure. It doesn&#x27;t necessarily have to be convenient to parse, just possible. If you aren&#x27;t experienced with designing serialization schemes, though, it may be best to use a common scheme like JSON or Protobuf. But, don&#x27;t forget that if you have multiple <i>types</i> of structures, your serialization must specify its own type. For JSON, you could add a `&quot;type&quot;: &quot;MyType&quot;` property. For Protobuf, define a single top-level type which is a big &quot;oneof&quot; (union) of all possible types, and always serialize as that top-level type.
评论 #33946078 未加载
评论 #33945098 未加载
lmz超过 2 年前
Isn&#x27;t this something that is already solved by DER if you&#x27;re using ASN.1 data structures?
评论 #33943622 未加载
评论 #33950639 未加载
082349872349872超过 2 年前
key slogan: <i>Authenticate what is being meant, not what is being said.</i>
评论 #33950375 未加载