hash.pyx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from libc.stdint cimport uint32_t, uint64_t
  2. from libc.string cimport memcpy
  3. from lakesuperior.model.base cimport Buffer
  4. from lakesuperior.cy_include cimport spookyhash as sph
  5. memcpy(&term_hash_seed32, TERM_HASH_SEED, HLEN_32)
  6. memcpy(&term_hash_seed64_1, TERM_HASH_SEED, HLEN_64)
  7. memcpy(&term_hash_seed64_2, TERM_HASH_SEED + HLEN_64, HLEN_64)
  8. cdef inline int hash32(const Buffer *message, Hash32 *hash) except -1:
  9. """
  10. Get a 32-bit (unsigned int) hash value of a byte string.
  11. """
  12. cdef uint32_t seed = term_hash_seed64_1
  13. hash[0] = sph.spookyhash_32(message[0].addr, message[0].sz, seed)
  14. cdef inline int hash64(const Buffer *message, Hash64 *hash) except -1:
  15. """
  16. Get a 64-bit (unsigned long) hash value of a byte string.
  17. """
  18. cdef uint64_t seed = term_hash_seed32
  19. hash[0] = sph.spookyhash_64(message[0].addr, message[0].sz, seed)
  20. cdef inline int hash128(const Buffer *message, Hash128 *hash) except -1:
  21. """
  22. Get the hash value of a byte string with a defined size.
  23. The hashing algorithm is `SpookyHash
  24. <http://burtleburtle.net/bob/hash/spooky.html>`_ which produces 128-bit
  25. (16-byte) digests.
  26. Note that this returns a char array while the smaller functions return
  27. numeric types (uint, ulong).
  28. The initial seeds are determined in the application configuration.
  29. :rtype: Hash128
  30. """
  31. cdef:
  32. DoubleHash64 seed = [term_hash_seed64_1, term_hash_seed64_2]
  33. Hash128 digest
  34. sph.spookyhash_128(message[0].addr, message[0].sz, seed, seed + 1)
  35. # This casts the 2 contiguous uint64_t's into a char[16] pointer.
  36. hash[0] = <Hash128>seed