]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude/Crypto/Hash.hs
[FIX] FLOW / TFICF bug
[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.List (foldl)
20 import Data.Text (Text)
21 import Gargantext.Prelude
22 import qualified Data.ByteString.Lazy.Char8 as Char
23 import qualified Data.Digest.Pure.SHA as SHA (sha256, showDigest)
24 import qualified Data.Set as Set
25 import qualified Data.Text as Text
26
27 --------------------------------------------------------------------------
28 -- | Use this datatype to keep traceability of hashes
29 -- TODO use newtype
30 type Hash = Text
31
32 -- | Class to make hashes
33 class IsHashable a where
34 hash :: a -> Hash
35
36 -- | Main API to hash text
37 -- using sha256 for now
38 instance IsHashable Char.ByteString where
39 hash = Text.pack
40 . SHA.showDigest
41 . SHA.sha256
42
43 instance {-# OVERLAPPING #-} IsHashable String where
44 hash = hash . Char.pack
45
46 instance IsHashable Text where
47 hash = hash . Text.unpack
48
49 instance IsHashable (Set Hash) where
50 hash = hash . foldl (<>) "" . Set.toList
51
52 instance {-# OVERLAPPABLE #-} IsHashable a => IsHashable [a] where
53 hash = hash . Set.fromList . map hash
54