diff options
Diffstat (limited to 'Hash.hs')
-rw-r--r-- | Hash.hs | 42 |
1 files changed, 33 insertions, 9 deletions
@@ -1,4 +1,4 @@ -{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE FlexibleInstances, OverloadedStrings #-} module Hash where @@ -18,22 +18,46 @@ instance Hashable B.ByteString where instance Hashable Val where hash (Val v) = hash v +instance Hashable Hash where + hash = id + sha256 :: B.ByteString -> H.Digest H.SHA256 sha256 = H.hash --- | Hash the concacenation of the hashes. -instance Hashable [Hash] where - hash = hash . B.concat . map (val . hashValue) +-- | A value tagged with a ByteString describing the type of value. +-- This is hashed by hashing the concacenation of the hash of the +-- bytestring and the hash of the value. This way, items of different types +-- but with the same internal content will hash differently. For example, +-- a Seen "foo" and a Entered "foo" should not hash the same. +data Tagged a = Tagged B.ByteString a + +instance Hashable a => Hashable (Tagged a) where + hash (Tagged b a) = hash [hash b, hash a] instance Hashable a => Hashable (Activity a) where - hash (Activity a (Just p) s) = hash [hash a, p, hash s] - hash (Activity a Nothing s) = hash [hash a, hash s] + hash (Activity a (Just p) s) = hash $ Tagged "Activity" + [hash a, hash p, hash s] + hash (Activity a Nothing s) = hash $ Tagged "Activity" + [hash a, hash (), hash s] + +instance Hashable a => Hashable (Proto a) where + hash (Proto a) = hash $ Tagged "Proto" a + hash (Rejected a) = hash $ Tagged "Rejected" (hash a) instance Hashable Entered where - hash v = hash [hash (enteredData v), hash (echoData v)] + hash v = hash $ Tagged "Entered" + [hash (enteredData v), hash (echoData v)] instance Hashable Seen where - hash v = hash [hash (seenData v)] + hash v = hash $ Tagged "Seen" [hash (seenData v)] instance Hashable Signature where - hash (Signature s) = hash s + hash (Signature s) = hash $ Tagged "Signature" s + +-- | Hash a list of hashes by hashing the concacenation of the hashes. +instance Hashable [Hash] where + hash = hash . B.concat . map (val . hashValue) + +-- | Hash empty string for () +instance Hashable () where + hash () = hash (mempty :: B.ByteString) |