From daf79506ba6ac9fa6b795ad2a19684288b367a92 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 17 Apr 2017 12:07:02 -0400 Subject: 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. --- Hash.hs | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'Hash.hs') 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) -- cgit v1.2.3