summaryrefslogtreecommitdiffhomepage
path: root/Hash.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2017-04-14 09:38:04 -0400
committerJoey Hess <joeyh@joeyh.name>2017-04-14 09:38:04 -0400
commit2a271b27c65a286882332b6268e8946851c52f2a (patch)
treeb46233a6285834d89947193a36df72341519b818 /Hash.hs
parentde7af3470e0b972ea9498f7ed07e6b38e8f15d03 (diff)
downloaddebug-me-2a271b27c65a286882332b6268e8946851c52f2a.tar.gz
add JSON serialization
Fairly straightforward, but did have to decide how to encode all the ByteStrings, since they are not necessarily utf-8. Used base64. This commit was sponsored by Henrik Riomar on Patreon.
Diffstat (limited to 'Hash.hs')
-rw-r--r--Hash.hs17
1 files changed, 10 insertions, 7 deletions
diff --git a/Hash.hs b/Hash.hs
index eca00e4..4174d5f 100644
--- a/Hash.hs
+++ b/Hash.hs
@@ -8,19 +8,22 @@ 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
+ -- Encodes the SHA256 using base16 format
+ hash = HashPointer SHA256 . Val . C8.pack . show . sha256
+
+instance Hashable Val where
+ hash (Val v) = hash v
+
+sha256 :: B.ByteString -> H.Digest H.SHA256
+sha256 = H.hash
--- | Hash the concacenation of the hashes, encoding them in base16 format.
+-- | Hash the concacenation of the hashes.
instance Hashable [HashPointer] where
- hash = hash . B.concat . map encodeHashPointer
+ hash = hash . B.concat . map (val . hashValue)
instance Hashable a => Hashable (Activity a) where
hash (Activity a p s) = hash [hash a, p, hash s]