From 2a271b27c65a286882332b6268e8946851c52f2a Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 14 Apr 2017 09:38:04 -0400 Subject: 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. --- Hash.hs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'Hash.hs') 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] -- cgit v1.2.3