summaryrefslogtreecommitdiffhomepage
path: root/Hash.hs
blob: eca00e45224f7ac9f7493b25015756f25f298baf (plain)
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
{-# 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