Using Google's CityHash hash functions for strings

Google's CityHash hash functions are optimized for hashing strings, but are not meant to be cryptographically secure. CityHash is ideal for implementing a hash table dealing with strings. We will use it in this recipe to produce both 64-bit and 128-bit digests.

Getting ready

Install the cityhash package from Cabal as follows:

$ cabal install cityhash

How to do it…

  1. Import the relevant packages as follows:
    import Data.Digest.CityHash
    import Data.ByteString.Char8 (pack)
    import Data.Word (Word64)
    import Data.LargeWord (Word128)
  2. Test the various hashing function on an input string using the following code snippet:
    main = do (pack str) (1 :: Word128) let str = "cityhash" print $ cityHash64 (pack str) print ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.