Search code examples
phpstringhashchecksumadler32

Cumulative Hashes


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?


Solution

  • 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?