module Crypto.Multihash.Internal.Types where
import Crypto.Hash.Algorithms
import Data.ByteString (ByteString)
import Data.String (IsString(..))
import Data.String.Conversions
data Base = Base2
| Base16
| Base32
| Base58
| Base64
deriving (Eq, Show)
class Codable a where
toCode :: a -> Int
instance Codable SHA1 where
toCode SHA1 = 0x11
instance Codable SHA256 where
toCode SHA256 = 0x12
instance Codable SHA512 where
toCode SHA512 = 0x13
instance Codable SHA3_512 where
toCode SHA3_512 = 0x14
instance Codable SHA3_384 where
toCode SHA3_384 = 0x15
instance Codable SHA3_256 where
toCode SHA3_256 = 0x16
instance Codable SHA3_224 where
toCode SHA3_224 = 0x17
instance Codable Blake2b_512 where
toCode Blake2b_512 = 0x40
instance Codable Blake2s_256 where
toCode Blake2s_256 = 0x41
class Encodable a where
encode :: IsString s => Base -> a -> Either String s
encode' :: IsString s => Base -> a -> s
encode' base digest = fromString <$> eitherToErr $ encode base digest
check :: (IsString s, ConvertibleStrings s ByteString) => s -> a -> Either String Bool
check' :: (IsString s, ConvertibleStrings s ByteString) => s -> a -> Bool
check' encoded digest = eitherToErr $ check encoded digest
class Checkable b where
checkPayload :: (IsString s, ConvertibleStrings s ByteString) => s -> b -> Either String Bool
checkPayload' :: (IsString s, ConvertibleStrings s ByteString) => s -> b -> Bool
checkPayload' encoded payload = eitherToErr $ checkPayload encoded payload
eitherToErr :: Either String b -> b
eitherToErr v = case v of
Right val -> val
Left err -> error err