Crypto.purs 967 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
-- | 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

instance isHashableString :: IsHashable String
  where
    hash = hash'

instance isHashableArray :: (Crypto.Hashable a, IsHashable a) => IsHashable (Array a)
  where
    hash = hash <<< Set.fromFoldable <<< map hash

instance isHashableSet :: IsHashable (Set String) where
  hash = hash <<< concat <<< toArray
    where
      toArray :: forall a. Set a -> Array a
      toArray = Set.toUnfoldable

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

38