累积哈希值

发布于 2024-08-05 16:40:34 字数 348 浏览 4 评论 0原文

我之前读过这里(编辑: 增量校验和),有一些校验和支持以下属性的算法(我认为其中之一是 adler32):

adler32('abc'); // 123
adler32('def'); // 456
adler32('abcdef'); // 579 (123 + 456)

请注意,结果只是示例来演示我想要实现的目标。我已经尝试了一些使用 PHP 中的哈希扩展以及 adler 和 fletcher 模块的示例,但这些值似乎没有相加。有人可以给我展示一些实施示例吗?

I've read before here on SO (EDIT: Incremental Checksums) that there are some checksum algorithms (I think one of those is adler32) that support the following property:

adler32('abc'); // 123
adler32('def'); // 456
adler32('abcdef'); // 579 (123 + 456)

Please note that the results are only examples to demonstrate what I want to archieve. I've tried some examples with the hash extension in PHP with the adler and fletcher modules but the values don't seem to add up. Can someone show me some implementation examples?

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

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

发布评论

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

评论(5

猛虎独行 2024-08-12 16:40:34

如果您正在寻找的是字符串上的哈希函数,例如 hash(s1+s2) = hash(s1) + hash(s2),我非常确定具有此属性的所有函数哈希都是对于固定函数 hash_char,形式为 (字符串中所有字符 c 的 hash_char(c) 之和)

这并不能构成一个非常好的哈希函数。您没有记错您在 adler32 中看到的内容吗?

If what you are looking for is a hash function on strings such that hash(s1+s2) = hash(s1) + hash(s2), I am pretty sure that all functions hash with this property are of the form (sum of the hash_char(c) for all the chars c in the string), for a fixed function hash_char.

That does not make for a very good hash function. Do you not misremember what you saw about adler32?

美人如玉 2024-08-12 16:40:34

Adler32 不是哈希函数。
没有具有此属性的良好哈希函数。

然而,有一些加密函数具有类似的特性;称为同态
基本上:

E(text1)*E(text2)=cipher  
D(cipher) = text1 + text2  

其中E是加密函数,D是加密函数,文本是数字(或序列化为数字的数据)。请注意,使用 + & 以外的操作的方案* 确实存在。

两个示例方案:ElGamalPaillier。两者都,这是非对称加密方案的共同特征。这两个例子都使用 * & +。

Adler32 is not a hash function.
There are no good hash functions with this property.

There are, however, encryption functions with a similar property; known as homomorphism.
Basically:

E(text1)*E(text2)=cipher  
D(cipher) = text1 + text2  

Where E is the encryption function, D is the encryption function, and the texts are numbers (or data serialized as a number). Note that schemes using operations other than + & * do exist.

Two examples schemes: ElGamal, and Paillier. Both are slow, which is a common feature of asymmetric encryption schemes. Both of these examples use * & +.

无所谓啦 2024-08-12 16:40:34

除非我误解了,否则我不知道如何在没有不必要数量的碰撞的情况下实现这一点。

hash('cde') => 345 
hash('aaabcd') => 345

Unless I misunderstand I don't see how this is possible without an unnecessary number of collisions.

hash('cde') => 345 
hash('aaabcd') => 345
风渺 2024-08-12 16:40:34

Adler32 是一种校验和算法而不是哈希,并且不具有您描述的属性。

哈希具有这样的属性是很常见的:哈希的状态可以通过哈希的结果来描述,因此如果

H ( "aaa" ) = G ( H0, "aaa" )

其中 G 是哈希的函数和哈希值的串联,H0 是一个初始值,通常为零。那么

H ( "aaa" ) = G ( H("aa"), "a" )
= G ( G ( H("a"), "a" ), "a" )
= G ( G ( H0, "a" ), "a" ), "a" )

但是一个函数,它只是将输入中的字符的一些函数添加在一起(正如您的规则所暗示的那样,G ( x .concat. y ) = G(x) + G(y) ) 将对该输入的所有排列发生冲突。

一个这样的函数可能会从 ASCII 输入创建一个 128 位散列:

H(x) = sum ( b => 1 << b foreach byte b in x ) 

它具有以下属性:

H("abcdef") == H("abc") + H("def") 
            == H("a") + H("b") + H("c") + H("d") + H("e") + H("f") 
            == H("abcdfe")
            == H("abcfde")
            == H("abcfed")
 etc.

Adler32 is a checksum algorithm rather than a hash, and doesn't have the property you describe.

It's quite common for hashes to have the property that the state of the hash can be described by the result of the hash, so if

H ( "aaa" ) = G ( H0, "aaa" )

where G is the function of the hash and the concatenation to the hash, and H0 is an initial value, often zero. Then

H ( "aaa" ) = G ( H("aa"), "a" )
= G ( G ( H("a"), "a" ), "a" )
= G ( G ( G ( H0, "a" ), "a" ), "a" )

But a function which is simply adding some function of the characters in the input together ( as is implied by your rule that G ( x .concat. y ) = G(x) + G(y) ) will collide for all permutations of that input.

One such function might create a 128 bit hash from an ASCII input:

H(x) = sum ( b => 1 << b foreach byte b in x ) 

which would have the property that

H("abcdef") == H("abc") + H("def") 
            == H("a") + H("b") + H("c") + H("d") + H("e") + H("f") 
            == H("abcdfe")
            == H("abcfde")
            == H("abcfed")
 etc.
寄风 2024-08-12 16:40:34

鉴于 Alder32 算法描述,我不明白您描述的关于字符串连接的属性如何可能。

编辑:删除了有缺陷的演示,即哈希的这种属性是不可能的。事实上,这种哈希的一个例子就是简单地添加(也可能对某个大数取模,但这是可以理解的)输入中字符的 ASCII 值。 (谢谢 Pete Kirkham 指出我的这个错误)。

您可能指的是同态加密算法,而不是Alder32。您可以在此处找到此类加密方案的列表。 IBM 的研究员 Craig Gentry 也宣布成功生产出全同态加密,但我不知道此时是否发布了任何技术细节(顺便说一句,这将是一个大新闻)。

In view of Alder32 algorithm description, I don't see how the property you describe with regard to concatenation of string is possible.

Edit: removed flawed demo that such a property for a hash is impossible. Indeed an example of such a hash is the one that simply adds (well maybe modulo some big number, but that is understandable) the ASCII value of the characters in the input. (Thank you Pete Kirkham to point this error of mine).

Rather than Alder32 you may be referring to homomorphic encryption algorithms. You may find a list of such encryption schemes here. Craig Gentry, a research at IBM, also announced his success with producing fully homomorphic encryption, but I do not know if any technical details were published at this time (This would be big news, BTW).

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