Code Snippets
Data/Hash/FNV1a.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
| {-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Data.Hash.FNV1a
( fnv1a
) where
--------------------------------------------------------------------------------
import Data.Bits
( Bits
, shiftL
, shiftR
, xor
, (.&.)
)
import Data.Foldable
( foldl'
)
import Data.Word
( Word8
)
--------------------------------------------------------------------------------
-- FNV Hash
--
-- http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
--
-- Example: ./fnv1a32 -s foo => 0xA9F37ED7 (2851307223)
fnv1a
:: Word8
-> String
-> Integer
fnv1a 032 x =
aux p o m x
where
p = 0x1000193
o = 0x811C9DC5
m = 0xFFFFFFFF
fnv1a 064 x =
aux p o m x
where
p = 0x100000001B3
o = 0xCBF29CE484222325
m = 0xFFFFFFFFFFFFFFFF
fnv1a 128 x =
aux p o m x
where
p = 0x1000000000000000000013B
o = 0x6C62272E07BB014262B821756295C58D
m = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
fnv1a 000 x = {- 256 bits -}
aux p o m x
where
p = 0x1000000000000000000000000000000000000000163
o = 0xDD268DBCAAC550362D98C384C4E576CCC8B1536847B6BBB31023B4C8CAEE0535
m = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
fnv1a bs x
| bs1 < 015 =
let
p = 0x1000193
o = 0x811C9DC5
m = 1 .<. bsi - 1
h = aux p o m x
in
((h .>. bsi) `xor` h) .&. m
| bs1 < 032 =
let
p = 0x1000193
o = 0x811C9DC5
m = 1 .<. bsi - 1
h = aux p o m x
in
(h .>. bsi) `xor` (h .&. m)
| bs1 < 064 =
let
p = 0x100000001B3
o = 0xCBF29CE484222325
m = 1 .<. bsi - 1
h = aux p o m x
in
(h .>. bsi) `xor` (h .&. m)
| bs1 < 128 =
let
p = 0x1000000000000000000013B
o = 0x6C62272E07BB014262B821756295C58D
m = 1 .<. bsi - 1
h = aux p o m x
in
(h .>. bsi) `xor` (h .&. m)
| otherwise =
let
p = 0x1000000000000000000000000000000000000000163
o = 0xDD268DBCAAC550362D98C384C4E576CCC8B1536847B6BBB31023B4C8CAEE0535
m = 1 .<. bsi - 1
h = aux p o m x
in
(h .>. bsi) `xor` (h .&. m)
where
bs1 = bs - 1
bsi = fromIntegral bs
--------------------------------------------------------------------------------
-- HELPERS
(.<.)
:: Bits a
=> a
-> Int
-> a
(.<.) = shiftL
(.>.)
:: Bits a
=> a
-> Int
-> a
(.>.) = shiftR
aux
:: Integer
-> Integer
-> Integer
-> String
-> Integer
aux prime offset bitmask =
foldl' (\ a x -> ((a `xor` x) * prime) .&. bitmask) offset .
map (fromIntegral . fromEnum)
|
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| #!/usr/bin/env stack
{- stack
--resolver lts-13.30
--install-ghc
script
--ghc-options -Werror
--ghc-options -Wall
--
-}
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Main (main) where
--------------------------------------------------------------------------------
import Data.Hash.FNV1a
( fnv1a
)
import Data.Word
( Word8
)
--------------------------------------------------------------------------------
hashes :: Word8 -> [String] -> [(String, Integer)]
hashes bs xs =
zip xs ys
where
ys = map (fnv1a bs) xs
pprint :: Word8 -> [String] -> IO ()
pprint bs xs =
putStrLn "" >>
putStrLn hd >>
mapM_ (putStrLn . (\(x,y) -> x ++ ": " ++ show y)) (hashes bs xs)
where
bs' = if 0 == bs then "256" else show bs
hd = "## " ++ bs' ++ " bit hashes:"
--------------------------------------------------------------------------------
main :: IO ()
main =
putStr "# FNV-1a hashes for: [" >>
mapM_ (putStr . (" " ++)) xs >>
putStrLn " ]" >>
mapM_ (flip pprint xs) ys
where
xs = map (:[]) ['α' .. 'ω'] -- ['a' .. 'z']
ys = [1 .. 255] ++ [0]
|
Code Output:
References:
- Landon Curt Noll homepage:
- Wikipedia: