summaryrefslogtreecommitdiffhomepage
path: root/Hash.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2017-04-17 12:07:02 -0400
committerJoey Hess <joeyh@joeyh.name>2017-04-17 12:10:16 -0400
commitdaf79506ba6ac9fa6b795ad2a19684288b367a92 (patch)
tree5cc347e0e8b08489452ccd1472c4d6f4b4c065cc /Hash.hs
parent8efda806c4dbb9d0acf069a34318a34e6f2bce86 (diff)
downloaddebug-me-daf79506ba6ac9fa6b795ad2a19684288b367a92.tar.gz
add Rejected and tag hashes by type
Need a way for the user to indicate when an Activity Entered is Rejected. Changed hashing to include type tags, so Acticity Entered and Activity Seen can never hash to the same hash. Got debug-me.hs to compile after these changes, but currently it's buggy after Activity Entered is Rejected. Started protocol.txt documentation. This commit was sponsored by Francois Marier on Patreon.
Diffstat (limited to 'Hash.hs')
-rw-r--r--Hash.hs42
1 files changed, 33 insertions, 9 deletions
diff --git a/Hash.hs b/Hash.hs
index 53be540..9b5fa80 100644
--- a/Hash.hs
+++ b/Hash.hs
@@ -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)