]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude/Crypto/Hash.hs
[CLEAN] SocialLists
[gargantext.git] / src / Gargantext / Prelude / Crypto / Hash.hs
1 {-|
2 Module : Gargantext.Prelude.Crypto.Hash
3 Description : Useful Tools near Prelude of the project
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 module Gargantext.Prelude.Crypto.Hash
15 where
16
17 import Prelude (String)
18 import Data.Set (Set)
19 import Data.Text (Text)
20 import Gargantext.Prelude
21 import qualified Data.ByteString.Lazy.Char8 as Char
22 import qualified Data.Digest.Pure.SHA as SHA (sha256, showDigest)
23 import qualified Data.Set as Set
24 import qualified Data.Text as Text
25
26 --------------------------------------------------------------------------
27 -- | Use this datatype to keep traceability of hashes
28 -- TODO use newtype
29 type Hash = Text
30
31 -- | Class to make hashes
32 class IsHashable a where
33 hash :: a -> Hash
34
35 -- | Main API to hash text
36 -- using sha256 for now
37 instance IsHashable Char.ByteString where
38 hash = Text.pack
39 . SHA.showDigest
40 . SHA.sha256
41
42 instance {-# OVERLAPPING #-} IsHashable String where
43 hash = hash . Char.pack
44
45 instance IsHashable Text where
46 hash = hash . Text.unpack
47
48 instance IsHashable (Set Hash) where
49 hash = hash . foldl (<>) "" . Set.toList
50
51 instance {-# OVERLAPPABLE #-} IsHashable a => IsHashable [a] where
52 hash = hash . Set.fromList . map hash
53