Search code examples
pythonmysqlhashfnv

FNV1A_64 don't match


I'm trying 3 different implementation of FNV1A_64 hash.

1) Maatkit

SELECT FNV1A_64('1')

Result: -5808609649712063748

2) pyhash

import pyhash
hasher = pyhash.fnv1a_64()
print hasher('1')

Result: 53876069782339L

3) fnv

./fnv1a64 -s 1

Result: 0xaf63ac4c86019afc (12638134423997487000 decimal)

Why all the results are different ?


Solution

  • MySQL and the command line program gave you the same result. One just printed a signed number and the other printed a hex representation of the same number.

    >>> import struct
    >>> struct.pack('q', -5808609649712063748)
    '\xfc\x9a\x01\x86L\xacc\xaf'
    >>> struct.unpack('Q', _)
    (12638134423997487868L,)
    >>> 0xaf63ac4c86019afc == _[0]
    True
    

    Python, however, requires FNV1A_64_INIT as it's not passed by default.

    >>> FNV1A_64_INIT = 0xcbf29ce484222325
    >>> import pyhash
    >>> hasher = pyhash.fnv1a_64()
    >>> hasher('1', seed=FNV1A_64_INIT)
    12638134423997487868L
    >>> _ == 0xaf63ac4c86019afc
    True