summaryrefslogtreecommitdiffhomepage
path: root/Hash.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2017-04-11 13:12:37 -0400
committerJoey Hess <joeyh@joeyh.name>2017-04-11 15:31:26 -0400
commitbf71e5f4d875806e8f2623d95545c8b7a7c2d8f4 (patch)
treef7fa8439df3c89b592c89db0408324c8951c7f51 /Hash.hs
downloaddebug-me-bf71e5f4d875806e8f2623d95545c8b7a7c2d8f4.tar.gz
some basic data types and hashing to start debug-me
This commit was sponsored by Denis Dzyubenko on Patreon.
Diffstat (limited to 'Hash.hs')
-rw-r--r--Hash.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/Hash.hs b/Hash.hs
new file mode 100644
index 0000000..eca00e4
--- /dev/null
+++ b/Hash.hs
@@ -0,0 +1,36 @@
+{-# LANGUAGE FlexibleInstances #-}
+
+module Hash where
+
+import Types
+
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Char8 as C8
+import qualified Crypto.Hash as H
+
+-- | Encode a hash pointer using base16 format.
+encodeHashPointer :: HashPointer -> B.ByteString
+encodeHashPointer (HashPointer d) = C8.pack (show d)
+
+class Hashable a where
+ hash :: a -> HashPointer
+
+instance Hashable B.ByteString where
+ hash = HashPointer . H.hash
+
+-- | Hash the concacenation of the hashes, encoding them in base16 format.
+instance Hashable [HashPointer] where
+ hash = hash . B.concat . map encodeHashPointer
+
+instance Hashable a => Hashable (Activity a) where
+ hash (Activity a p s) = hash [hash a, p, hash s]
+ hash (StartActivity a s) = hash [hash a, hash s]
+
+instance Hashable Entered where
+ hash v = hash [hash (enteredData v), hash (echoData v)]
+
+instance Hashable Seen where
+ hash v = hash [hash (seenData v)]
+
+instance Hashable Signature where
+ hash (Signature s) = hash s