{-# LANGUAGE PackageImports #-}
-- |
-- Module      : Crypto.Hash.Types
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- Crypto hash types definitions
--
module Crypto.Hash.Types
    ( Context(..)
    , Digest(..)
    -- * deprecated
    , contextToByteString
    , digestToByteString
    )
    where

import Data.ByteString (ByteString)
import Data.Byteable
import qualified Data.ByteArray as B (convert)
import qualified "cryptonite" Crypto.Hash as H

-- | Represent a context for a given hash algorithm.
newtype Context a = Context (H.Context a)

instance Byteable (Context a) where
    toBytes :: Context a -> ByteString
toBytes (Context ctx :: Context a
ctx) = Context a -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert Context a
ctx

--- | return the binary bytestring. deprecated use toBytes.
contextToByteString :: Context a -> ByteString
contextToByteString :: Context a -> ByteString
contextToByteString = Context a -> ByteString
forall a. Byteable a => a -> ByteString
toBytes

-- | Represent a digest for a given hash algorithm.
newtype Digest a = Digest (H.Digest a)
    deriving (Digest a -> Digest a -> Bool
(Digest a -> Digest a -> Bool)
-> (Digest a -> Digest a -> Bool) -> Eq (Digest a)
forall a. Digest a -> Digest a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Digest a -> Digest a -> Bool
$c/= :: forall a. Digest a -> Digest a -> Bool
== :: Digest a -> Digest a -> Bool
$c== :: forall a. Digest a -> Digest a -> Bool
Eq,Eq (Digest a)
Eq (Digest a) =>
(Digest a -> Digest a -> Ordering)
-> (Digest a -> Digest a -> Bool)
-> (Digest a -> Digest a -> Bool)
-> (Digest a -> Digest a -> Bool)
-> (Digest a -> Digest a -> Bool)
-> (Digest a -> Digest a -> Digest a)
-> (Digest a -> Digest a -> Digest a)
-> Ord (Digest a)
Digest a -> Digest a -> Bool
Digest a -> Digest a -> Ordering
Digest a -> Digest a -> Digest a
forall a. Eq (Digest a)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Digest a -> Digest a -> Bool
forall a. Digest a -> Digest a -> Ordering
forall a. Digest a -> Digest a -> Digest a
min :: Digest a -> Digest a -> Digest a
$cmin :: forall a. Digest a -> Digest a -> Digest a
max :: Digest a -> Digest a -> Digest a
$cmax :: forall a. Digest a -> Digest a -> Digest a
>= :: Digest a -> Digest a -> Bool
$c>= :: forall a. Digest a -> Digest a -> Bool
> :: Digest a -> Digest a -> Bool
$c> :: forall a. Digest a -> Digest a -> Bool
<= :: Digest a -> Digest a -> Bool
$c<= :: forall a. Digest a -> Digest a -> Bool
< :: Digest a -> Digest a -> Bool
$c< :: forall a. Digest a -> Digest a -> Bool
compare :: Digest a -> Digest a -> Ordering
$ccompare :: forall a. Digest a -> Digest a -> Ordering
$cp1Ord :: forall a. Eq (Digest a)
Ord)

instance Byteable (Digest a) where
    toBytes :: Digest a -> ByteString
toBytes (Digest dig :: Digest a
dig) = Digest a -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert Digest a
dig

-- | return the binary bytestring. deprecated use toBytes.
{-# DEPRECATED digestToByteString "use toBytes from byteable:Data.Byteable" #-}
digestToByteString :: Digest a -> ByteString
digestToByteString :: Digest a -> ByteString
digestToByteString = Digest a -> ByteString
forall a. Byteable a => a -> ByteString
toBytes

instance Show (Digest a) where
    show :: Digest a -> String
show (Digest dig :: Digest a
dig) = Digest a -> String
forall a. Show a => a -> String
show Digest a
dig