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…
Import the relevant packages as follows:
import Data.Digest.CityHash import Data.ByteString.Char8 (pack) import Data.Word (Word64) import Data.LargeWord (Word128)
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 $ cityHash64WithSeed (pack str) (1 :: Word64) print $ cityHash64WithSeed (pack str) (2 :: Word64) print $ cityHash128 (pack str) print $ cityHash128WithSeed print $ cityHash128WithSeed (pack str) (2 :: Word128)
Display the output as follows...