{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}
module Haskoin.Crypto.Hash (
Hash512 (getHash512),
Hash256 (getHash256),
Hash160 (getHash160),
CheckSum32 (getCheckSum32),
sha512,
sha256,
ripemd160,
sha1,
doubleSHA256,
addressHash,
checkSum32,
hmac512,
hmac256,
split512,
join512,
initTaggedHash,
) where
import Control.DeepSeq
import Crypto.Hash (
Context,
RIPEMD160 (..),
SHA1 (..),
SHA256 (..),
SHA512 (..),
hashInit,
hashUpdates,
hashWith,
)
import Crypto.MAC.HMAC (HMAC, hmac)
import Data.Binary (Binary (..))
import Data.ByteArray (ByteArrayAccess)
import qualified Data.ByteArray as BA
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as BSS
import qualified Data.Bytes.Get as Get
import qualified Data.Bytes.Put as Put
import Data.Bytes.Serial (Serial (..))
import Data.Either (fromRight)
import Data.Hashable (Hashable)
import Data.Serialize (Serialize (..))
import Data.String (IsString, fromString)
import Data.String.Conversions (cs)
import Data.Word (Word32)
import GHC.Generics (Generic)
import Haskoin.Util
import Text.Read as R
newtype CheckSum32 = CheckSum32
{ CheckSum32 -> Word32
getCheckSum32 :: Word32
}
deriving (CheckSum32 -> CheckSum32 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CheckSum32 -> CheckSum32 -> Bool
$c/= :: CheckSum32 -> CheckSum32 -> Bool
== :: CheckSum32 -> CheckSum32 -> Bool
$c== :: CheckSum32 -> CheckSum32 -> Bool
Eq, Eq CheckSum32
CheckSum32 -> CheckSum32 -> Bool
CheckSum32 -> CheckSum32 -> Ordering
CheckSum32 -> CheckSum32 -> CheckSum32
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
min :: CheckSum32 -> CheckSum32 -> CheckSum32
$cmin :: CheckSum32 -> CheckSum32 -> CheckSum32
max :: CheckSum32 -> CheckSum32 -> CheckSum32
$cmax :: CheckSum32 -> CheckSum32 -> CheckSum32
>= :: CheckSum32 -> CheckSum32 -> Bool
$c>= :: CheckSum32 -> CheckSum32 -> Bool
> :: CheckSum32 -> CheckSum32 -> Bool
$c> :: CheckSum32 -> CheckSum32 -> Bool
<= :: CheckSum32 -> CheckSum32 -> Bool
$c<= :: CheckSum32 -> CheckSum32 -> Bool
< :: CheckSum32 -> CheckSum32 -> Bool
$c< :: CheckSum32 -> CheckSum32 -> Bool
compare :: CheckSum32 -> CheckSum32 -> Ordering
$ccompare :: CheckSum32 -> CheckSum32 -> Ordering
Ord, forall a.
(forall (m :: * -> *). MonadPut m => a -> m ())
-> (forall (m :: * -> *). MonadGet m => m a) -> Serial a
forall (m :: * -> *). MonadGet m => m CheckSum32
forall (m :: * -> *). MonadPut m => CheckSum32 -> m ()
deserialize :: forall (m :: * -> *). MonadGet m => m CheckSum32
$cdeserialize :: forall (m :: * -> *). MonadGet m => m CheckSum32
serialize :: forall (m :: * -> *). MonadPut m => CheckSum32 -> m ()
$cserialize :: forall (m :: * -> *). MonadPut m => CheckSum32 -> m ()
Serial, Int -> CheckSum32 -> ShowS
[CheckSum32] -> ShowS
CheckSum32 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CheckSum32] -> ShowS
$cshowList :: [CheckSum32] -> ShowS
show :: CheckSum32 -> String
$cshow :: CheckSum32 -> String
showsPrec :: Int -> CheckSum32 -> ShowS
$cshowsPrec :: Int -> CheckSum32 -> ShowS
Show, ReadPrec [CheckSum32]
ReadPrec CheckSum32
Int -> ReadS CheckSum32
ReadS [CheckSum32]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CheckSum32]
$creadListPrec :: ReadPrec [CheckSum32]
readPrec :: ReadPrec CheckSum32
$creadPrec :: ReadPrec CheckSum32
readList :: ReadS [CheckSum32]
$creadList :: ReadS [CheckSum32]
readsPrec :: Int -> ReadS CheckSum32
$creadsPrec :: Int -> ReadS CheckSum32
Read, Eq CheckSum32
Int -> CheckSum32 -> Int
CheckSum32 -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: CheckSum32 -> Int
$chash :: CheckSum32 -> Int
hashWithSalt :: Int -> CheckSum32 -> Int
$chashWithSalt :: Int -> CheckSum32 -> Int
Hashable, forall x. Rep CheckSum32 x -> CheckSum32
forall x. CheckSum32 -> Rep CheckSum32 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CheckSum32 x -> CheckSum32
$cfrom :: forall x. CheckSum32 -> Rep CheckSum32 x
Generic, CheckSum32 -> ()
forall a. (a -> ()) -> NFData a
rnf :: CheckSum32 -> ()
$crnf :: CheckSum32 -> ()
NFData)
instance Serialize CheckSum32 where
put :: Putter CheckSum32
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get CheckSum32
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance Binary CheckSum32 where
put :: CheckSum32 -> Put
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get CheckSum32
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
newtype Hash512 = Hash512 {Hash512 -> ShortByteString
getHash512 :: ShortByteString}
deriving (Hash512 -> Hash512 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Hash512 -> Hash512 -> Bool
$c/= :: Hash512 -> Hash512 -> Bool
== :: Hash512 -> Hash512 -> Bool
$c== :: Hash512 -> Hash512 -> Bool
Eq, Eq Hash512
Hash512 -> Hash512 -> Bool
Hash512 -> Hash512 -> Ordering
Hash512 -> Hash512 -> Hash512
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
min :: Hash512 -> Hash512 -> Hash512
$cmin :: Hash512 -> Hash512 -> Hash512
max :: Hash512 -> Hash512 -> Hash512
$cmax :: Hash512 -> Hash512 -> Hash512
>= :: Hash512 -> Hash512 -> Bool
$c>= :: Hash512 -> Hash512 -> Bool
> :: Hash512 -> Hash512 -> Bool
$c> :: Hash512 -> Hash512 -> Bool
<= :: Hash512 -> Hash512 -> Bool
$c<= :: Hash512 -> Hash512 -> Bool
< :: Hash512 -> Hash512 -> Bool
$c< :: Hash512 -> Hash512 -> Bool
compare :: Hash512 -> Hash512 -> Ordering
$ccompare :: Hash512 -> Hash512 -> Ordering
Ord, Eq Hash512
Int -> Hash512 -> Int
Hash512 -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Hash512 -> Int
$chash :: Hash512 -> Int
hashWithSalt :: Int -> Hash512 -> Int
$chashWithSalt :: Int -> Hash512 -> Int
Hashable, forall x. Rep Hash512 x -> Hash512
forall x. Hash512 -> Rep Hash512 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Hash512 x -> Hash512
$cfrom :: forall x. Hash512 -> Rep Hash512 x
Generic, Hash512 -> ()
forall a. (a -> ()) -> NFData a
rnf :: Hash512 -> ()
$crnf :: Hash512 -> ()
NFData)
newtype Hash256 = Hash256 {Hash256 -> ShortByteString
getHash256 :: ShortByteString}
deriving (Hash256 -> Hash256 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Hash256 -> Hash256 -> Bool
$c/= :: Hash256 -> Hash256 -> Bool
== :: Hash256 -> Hash256 -> Bool
$c== :: Hash256 -> Hash256 -> Bool
Eq, Eq Hash256
Hash256 -> Hash256 -> Bool
Hash256 -> Hash256 -> Ordering
Hash256 -> Hash256 -> Hash256
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
min :: Hash256 -> Hash256 -> Hash256
$cmin :: Hash256 -> Hash256 -> Hash256
max :: Hash256 -> Hash256 -> Hash256
$cmax :: Hash256 -> Hash256 -> Hash256
>= :: Hash256 -> Hash256 -> Bool
$c>= :: Hash256 -> Hash256 -> Bool
> :: Hash256 -> Hash256 -> Bool
$c> :: Hash256 -> Hash256 -> Bool
<= :: Hash256 -> Hash256 -> Bool
$c<= :: Hash256 -> Hash256 -> Bool
< :: Hash256 -> Hash256 -> Bool
$c< :: Hash256 -> Hash256 -> Bool
compare :: Hash256 -> Hash256 -> Ordering
$ccompare :: Hash256 -> Hash256 -> Ordering
Ord, Eq Hash256
Int -> Hash256 -> Int
Hash256 -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Hash256 -> Int
$chash :: Hash256 -> Int
hashWithSalt :: Int -> Hash256 -> Int
$chashWithSalt :: Int -> Hash256 -> Int
Hashable, forall x. Rep Hash256 x -> Hash256
forall x. Hash256 -> Rep Hash256 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Hash256 x -> Hash256
$cfrom :: forall x. Hash256 -> Rep Hash256 x
Generic, Hash256 -> ()
forall a. (a -> ()) -> NFData a
rnf :: Hash256 -> ()
$crnf :: Hash256 -> ()
NFData)
newtype Hash160 = Hash160 {Hash160 -> ShortByteString
getHash160 :: ShortByteString}
deriving (Hash160 -> Hash160 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Hash160 -> Hash160 -> Bool
$c/= :: Hash160 -> Hash160 -> Bool
== :: Hash160 -> Hash160 -> Bool
$c== :: Hash160 -> Hash160 -> Bool
Eq, Eq Hash160
Hash160 -> Hash160 -> Bool
Hash160 -> Hash160 -> Ordering
Hash160 -> Hash160 -> Hash160
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
min :: Hash160 -> Hash160 -> Hash160
$cmin :: Hash160 -> Hash160 -> Hash160
max :: Hash160 -> Hash160 -> Hash160
$cmax :: Hash160 -> Hash160 -> Hash160
>= :: Hash160 -> Hash160 -> Bool
$c>= :: Hash160 -> Hash160 -> Bool
> :: Hash160 -> Hash160 -> Bool
$c> :: Hash160 -> Hash160 -> Bool
<= :: Hash160 -> Hash160 -> Bool
$c<= :: Hash160 -> Hash160 -> Bool
< :: Hash160 -> Hash160 -> Bool
$c< :: Hash160 -> Hash160 -> Bool
compare :: Hash160 -> Hash160 -> Ordering
$ccompare :: Hash160 -> Hash160 -> Ordering
Ord, Eq Hash160
Int -> Hash160 -> Int
Hash160 -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Hash160 -> Int
$chash :: Hash160 -> Int
hashWithSalt :: Int -> Hash160 -> Int
$chashWithSalt :: Int -> Hash160 -> Int
Hashable, forall x. Rep Hash160 x -> Hash160
forall x. Hash160 -> Rep Hash160 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Hash160 x -> Hash160
$cfrom :: forall x. Hash160 -> Rep Hash160 x
Generic, Hash160 -> ()
forall a. (a -> ()) -> NFData a
rnf :: Hash160 -> ()
$crnf :: Hash160 -> ()
NFData)
instance Show Hash512 where
showsPrec :: Int -> Hash512 -> ShowS
showsPrec Int
_ = forall a. Show a => a -> ShowS
shows forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
encodeHex forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash512 -> ShortByteString
getHash512
instance Read Hash512 where
readPrec :: ReadPrec Hash512
readPrec = do
R.String String
str <- ReadPrec Lexeme
lexP
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. ReadPrec a
pfail forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ShortByteString -> Hash512
Hash512 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe ByteString
decodeHex (forall a b. ConvertibleStrings a b => a -> b
cs String
str)
instance Show Hash256 where
showsPrec :: Int -> Hash256 -> ShowS
showsPrec Int
_ = forall a. Show a => a -> ShowS
shows forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
encodeHex forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash256 -> ShortByteString
getHash256
instance Read Hash256 where
readPrec :: ReadPrec Hash256
readPrec = do
R.String String
str <- ReadPrec Lexeme
lexP
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. ReadPrec a
pfail forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ShortByteString -> Hash256
Hash256 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe ByteString
decodeHex (forall a b. ConvertibleStrings a b => a -> b
cs String
str)
instance Show Hash160 where
showsPrec :: Int -> Hash160 -> ShowS
showsPrec Int
_ = forall a. Show a => a -> ShowS
shows forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
encodeHex forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash160 -> ShortByteString
getHash160
instance Read Hash160 where
readPrec :: ReadPrec Hash160
readPrec = do
R.String String
str <- ReadPrec Lexeme
lexP
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. ReadPrec a
pfail forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ShortByteString -> Hash160
Hash160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe ByteString
decodeHex (forall a b. ConvertibleStrings a b => a -> b
cs String
str)
instance IsString Hash512 where
fromString :: String -> Hash512
fromString String
str =
case Text -> Maybe ByteString
decodeHex forall a b. (a -> b) -> a -> b
$ forall a b. ConvertibleStrings a b => a -> b
cs String
str of
Maybe ByteString
Nothing -> forall {a}. a
e
Just ByteString
bs ->
case ByteString -> Int
BS.length ByteString
bs of
Int
64 -> ShortByteString -> Hash512
Hash512 (ByteString -> ShortByteString
BSS.toShort ByteString
bs)
Int
_ -> forall {a}. a
e
where
e :: a
e = forall a. HasCallStack => String -> a
error String
"Could not decode hash from hex string"
instance Serial Hash512 where
deserialize :: forall (m :: * -> *). MonadGet m => m Hash512
deserialize = ShortByteString -> Hash512
Hash512 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGet m => Int -> m ByteString
Get.getByteString Int
64
serialize :: forall (m :: * -> *). MonadPut m => Hash512 -> m ()
serialize = forall (m :: * -> *). MonadPut m => ByteString -> m ()
Put.putByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash512 -> ShortByteString
getHash512
instance Serialize Hash512 where
put :: Putter Hash512
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash512
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance Binary Hash512 where
put :: Hash512 -> Put
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash512
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance IsString Hash256 where
fromString :: String -> Hash256
fromString String
str =
case Text -> Maybe ByteString
decodeHex forall a b. (a -> b) -> a -> b
$ forall a b. ConvertibleStrings a b => a -> b
cs String
str of
Maybe ByteString
Nothing -> forall {a}. a
e
Just ByteString
bs ->
case ByteString -> Int
BS.length ByteString
bs of
Int
32 -> ShortByteString -> Hash256
Hash256 (ByteString -> ShortByteString
BSS.toShort ByteString
bs)
Int
_ -> forall {a}. a
e
where
e :: a
e = forall a. HasCallStack => String -> a
error String
"Could not decode hash from hex string"
instance Serial Hash256 where
deserialize :: forall (m :: * -> *). MonadGet m => m Hash256
deserialize = ShortByteString -> Hash256
Hash256 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGet m => Int -> m ByteString
Get.getByteString Int
32
serialize :: forall (m :: * -> *). MonadPut m => Hash256 -> m ()
serialize = forall (m :: * -> *). MonadPut m => ByteString -> m ()
Put.putByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash256 -> ShortByteString
getHash256
instance Serialize Hash256 where
put :: Putter Hash256
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash256
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance Binary Hash256 where
put :: Hash256 -> Put
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash256
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance IsString Hash160 where
fromString :: String -> Hash160
fromString String
str =
case Text -> Maybe ByteString
decodeHex forall a b. (a -> b) -> a -> b
$ forall a b. ConvertibleStrings a b => a -> b
cs String
str of
Maybe ByteString
Nothing -> forall {a}. a
e
Just ByteString
bs ->
case ByteString -> Int
BS.length ByteString
bs of
Int
20 -> ShortByteString -> Hash160
Hash160 (ByteString -> ShortByteString
BSS.toShort ByteString
bs)
Int
_ -> forall {a}. a
e
where
e :: a
e = forall a. HasCallStack => String -> a
error String
"Could not decode hash from hex string"
instance Serial Hash160 where
deserialize :: forall (m :: * -> *). MonadGet m => m Hash160
deserialize = ShortByteString -> Hash160
Hash160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGet m => Int -> m ByteString
Get.getByteString Int
20
serialize :: forall (m :: * -> *). MonadPut m => Hash160 -> m ()
serialize = forall (m :: * -> *). MonadPut m => ByteString -> m ()
Put.putByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash160 -> ShortByteString
getHash160
instance Serialize Hash160 where
put :: Putter Hash160
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash160
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance Binary Hash160 where
put :: Hash160 -> Put
put = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
get :: Get Hash160
get = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
sha512 :: ByteArrayAccess b => b -> Hash512
sha512 :: forall b. ByteArrayAccess b => b -> Hash512
sha512 = ShortByteString -> Hash512
Hash512 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA512
SHA512
sha256 :: ByteArrayAccess b => b -> Hash256
sha256 :: forall b. ByteArrayAccess b => b -> Hash256
sha256 = ShortByteString -> Hash256
Hash256 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256
ripemd160 :: ByteArrayAccess b => b -> Hash160
ripemd160 :: forall b. ByteArrayAccess b => b -> Hash160
ripemd160 = ShortByteString -> Hash160
Hash160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith RIPEMD160
RIPEMD160
sha1 :: ByteArrayAccess b => b -> Hash160
sha1 :: forall b. ByteArrayAccess b => b -> Hash160
sha1 = ShortByteString -> Hash160
Hash160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA1
SHA1
doubleSHA256 :: ByteArrayAccess b => b -> Hash256
doubleSHA256 :: forall b. ByteArrayAccess b => b -> Hash256
doubleSHA256 =
ShortByteString -> Hash256
Hash256 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256
addressHash :: ByteArrayAccess b => b -> Hash160
addressHash :: forall b. ByteArrayAccess b => b -> Hash160
addressHash =
ShortByteString -> Hash160
Hash160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith RIPEMD160
RIPEMD160 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256
checkSum32 :: ByteArrayAccess b => b -> CheckSum32
checkSum32 :: forall b. ByteArrayAccess b => b -> CheckSum32
checkSum32 =
forall b a. b -> Either a b -> b
fromRight (forall a. HasCallStack => String -> a
error String
"Could not decode bytes as CheckSum32")
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Get a -> ByteString -> Either String a
Get.runGetS forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.take Int
4
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256
hmac512 :: ByteString -> ByteString -> Hash512
hmac512 :: ByteString -> ByteString -> Hash512
hmac512 ByteString
key ByteString
msg =
ShortByteString -> Hash512
Hash512 forall a b. (a -> b) -> a -> b
$ ByteString -> ShortByteString
BSS.toShort forall a b. (a -> b) -> a -> b
$ forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (forall key message a.
(ByteArrayAccess key, ByteArrayAccess message, HashAlgorithm a) =>
key -> message -> HMAC a
hmac ByteString
key ByteString
msg :: HMAC SHA512)
hmac256 :: (ByteArrayAccess k, ByteArrayAccess m) => k -> m -> Hash256
hmac256 :: forall k m.
(ByteArrayAccess k, ByteArrayAccess m) =>
k -> m -> Hash256
hmac256 k
key m
msg =
ShortByteString -> Hash256
Hash256 forall a b. (a -> b) -> a -> b
$ ByteString -> ShortByteString
BSS.toShort forall a b. (a -> b) -> a -> b
$ forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (forall key message a.
(ByteArrayAccess key, ByteArrayAccess message, HashAlgorithm a) =>
key -> message -> HMAC a
hmac k
key m
msg :: HMAC SHA256)
split512 :: Hash512 -> (Hash256, Hash256)
split512 :: Hash512 -> (Hash256, Hash256)
split512 Hash512
h =
(ShortByteString -> Hash256
Hash256 (ByteString -> ShortByteString
BSS.toShort ByteString
a), ShortByteString -> Hash256
Hash256 (ByteString -> ShortByteString
BSS.toShort ByteString
b))
where
(ByteString
a, ByteString
b) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort forall a b. (a -> b) -> a -> b
$ Hash512 -> ShortByteString
getHash512 Hash512
h
join512 :: (Hash256, Hash256) -> Hash512
join512 :: (Hash256, Hash256) -> Hash512
join512 (Hash256
a, Hash256
b) =
ShortByteString -> Hash512
Hash512
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
BSS.toShort
forall a b. (a -> b) -> a -> b
$ ShortByteString -> ByteString
BSS.fromShort (Hash256 -> ShortByteString
getHash256 Hash256
a) ByteString -> ByteString -> ByteString
`BS.append` ShortByteString -> ByteString
BSS.fromShort (Hash256 -> ShortByteString
getHash256 Hash256
b)
initTaggedHash ::
ByteString ->
Context SHA256
initTaggedHash :: ByteString -> Context SHA256
initTaggedHash ByteString
tag =
(forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
Context a -> [ba] -> Context a
`hashUpdates` [Digest SHA256
hashedTag, Digest SHA256
hashedTag]) forall a b. (a -> b) -> a -> b
$
forall a. HashAlgorithm a => Context a
hashInit @SHA256
where
hashedTag :: Digest SHA256
hashedTag = forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith SHA256
SHA256 ByteString
tag