Crypto.purs 911 Bytes
Newer Older
1 2
module Gargantext.Utils.Crypto where

3
import Crypto.Simple as Crypto
4 5 6
import Data.Set (Set)
import Data.Set   as Set
import Data.Array as Array
7

8 9
import Gargantext.Prelude

10 11 12 13 14 15 16 17 18 19 20
-- | TODO use newtype to disambiguate Set String and Set Hash
-- Set String needs Set.map hash
-- Set Hash   does not need Set.map hash (just concat)
type Hash = String

hash' :: forall a. Crypto.Hashable a => a -> String
hash' = Crypto.toString <<< Crypto.hash Crypto.SHA256

class IsHashable a where
  hash :: a -> Hash

21
instance IsHashable String
22 23 24
  where
    hash = hash'

25
instance (Crypto.Hashable a, IsHashable a) => IsHashable (Array a)
26 27 28
  where
    hash = hash <<< Set.fromFoldable <<< map hash

29
instance IsHashable (Set String) where
30 31 32 33 34 35 36 37
  hash = hash <<< concat <<< toArray
    where
      toArray :: forall a. Set a -> Array a
      toArray = Set.toUnfoldable

      concat :: Array Hash -> String
      concat = Array.foldl (<>) ""

38