Skip to content

Instantly share code, notes, and snippets.

@DanielG
Created December 24, 2016 02:25
Show Gist options
  • Save DanielG/b1dfbf90b11575639546dbf9dc54b726 to your computer and use it in GitHub Desktop.
Save DanielG/b1dfbf90b11575639546dbf9dc54b726 to your computer and use it in GitHub Desktop.
import Data.Bits (xor)
import Data.Char (ord)
import Data.Word (Word8, Word32)
fnv1a :: [Word8] -> Word32
fnv1a = foldl (\h x -> (h `xor` fromIntegral x) * 0x01000193) 0x811C9DC5
-- Test vectors from https://tools.ietf.org/html/draft-eastlake-fnv-05#page-15
test = and
[ fnv1a [] == 0x811c9dc5
, fnv1a (map ord8 "a") == 0xe40c292c
, fnv1a (map ord8 "foobar") == 0xbf9cf968
, fnv1a (map ord8 "\0") == 0x050c5d1f
, fnv1a (map ord8 "a\0") == 0x2b24d044
, fnv1a (map ord8 "foobar\0") == 0x0c1c9eb8
]
ord8 = fromIntegral . ord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment