如何在对象树上创建 SHA1 摘要?

发布于 2024-09-03 08:30:42 字数 229 浏览 7 评论 0原文

假设我有一棵对象树,其中每个对象都有一个字符串表示形式。我想在整个树上创建 SHA1 摘要。

最简单的方法是递归地遍历树的每个节点。对于每个节点,我将连接(作为简单字符串)所有子节点的 SHA1 摘要,将给定节点的字符串表示形式添加到该连接的字符串中,并对其执行 SHA1。这将是给定节点的 SHA1 摘要。

问题是这个摘要是否会像我连接子节点的字符串表示形式而不是子节点的摘要一样“好”?

谢谢

Let's say that I have a tree of objects of which every one have a string representation. I want to create a SHA1 digest on the whole tree.

The easiest way would be to recursively go over each node of the tree. For each node I would concatenate (as simple strings) the SHA1 digests of all the children, add the string representation of the given nod to this concatenated string, and do a SHA1 on it. This would be the SHA1 digest of the given node.

The question is will this digest be just as "good" as if I would have concatenated the string representation of the child nodes, and not the digests of the child nodes?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生再见 2024-09-10 08:30:42

这比对子级的串联进行哈希处理要好。考虑下面的树:

  • A
    • AA
    • AB

连接后,变为“AAAB”。与下面的树对比:

  • A
    • AAA
    • B

不同,但串联的哈希值是相同的。

This would be better than hashing the concatenation of the children. Consider the following tree:

  • A
    • AA
    • AB

When concatenated, this becomes "AAAB". Contrast with the following tree:

  • A
    • AAA
    • B

Different, but the hash of the concatenation would be the same.

仅此而已 2024-09-10 08:30:42

假设您可以选择节点标签中不允许使用的 Unicode 字符。

然后,您可以使用流 API(如 Java MessageDigest)类按树顺序提供所有标签,并在其间插入保留的分隔符。

最后,您将获得一个相对紧凑的摘要,而无需支付额外级别的 SHA 计算费用。

编辑

上面的方案不太正确,但是,话又说回来,原始问题也不是,因为它没有对树的结构进行编码。代码需要为每个节点构造某种比遍历顺序更强的 ID,并将其包含在节点的哈希输入中,以便具有不同形状但相同标签的树不会进行相同的哈希处理。

如果树顺序很重要但精确结构不重要,则答案中的原始方案有效。

Assume that you can select a Unicode character that is never permitted in the label of a node.

Then, you can use a stream API (like the Java MessageDigest) classes to feed all the labels, in tree-order, inserting the reserved delimiter character in between.

At the end you have one relatively compact digest, without paying for an extra level of SHA calculations.

EDIT

The scheme above isn't quite right, but, then again, neither is the original question, insofar it doesn't encode the structure of the tree. The code needs to construct some sort of ID for each node that is stronger than just the traversal order and include that in the hash input for the node, so that trees with different shapes but the same labels don't hash identically.

The original scheme in the answer works if the tree order is important but the precise structure is not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文