{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module Distribution.Client.HashValue (
HashValue,
hashValue,
truncateHash,
showHashValue,
readFileHashValue,
hashFromTUF,
) where
import Distribution.Client.Compat.Prelude
import Prelude ()
import qualified Hackage.Security.Client as Sec
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import System.IO (IOMode (..), withBinaryFile)
newtype HashValue = HashValue BS.ByteString
deriving (HashValue -> HashValue -> Bool
(HashValue -> HashValue -> Bool)
-> (HashValue -> HashValue -> Bool) -> Eq HashValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HashValue -> HashValue -> Bool
$c/= :: HashValue -> HashValue -> Bool
== :: HashValue -> HashValue -> Bool
$c== :: HashValue -> HashValue -> Bool
Eq, (forall x. HashValue -> Rep HashValue x)
-> (forall x. Rep HashValue x -> HashValue) -> Generic HashValue
forall x. Rep HashValue x -> HashValue
forall x. HashValue -> Rep HashValue x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep HashValue x -> HashValue
$cfrom :: forall x. HashValue -> Rep HashValue x
Generic, Int -> HashValue -> ShowS
[HashValue] -> ShowS
HashValue -> String
(Int -> HashValue -> ShowS)
-> (HashValue -> String)
-> ([HashValue] -> ShowS)
-> Show HashValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HashValue] -> ShowS
$cshowList :: [HashValue] -> ShowS
show :: HashValue -> String
$cshow :: HashValue -> String
showsPrec :: Int -> HashValue -> ShowS
$cshowsPrec :: Int -> HashValue -> ShowS
Show, Typeable)
instance Binary HashValue
instance Structured HashValue
hashValue :: LBS.ByteString -> HashValue
hashValue :: ByteString -> HashValue
hashValue = ByteString -> HashValue
HashValue (ByteString -> HashValue)
-> (ByteString -> ByteString) -> ByteString -> HashValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
SHA256.hashlazy
showHashValue :: HashValue -> String
showHashValue :: HashValue -> String
showHashValue (HashValue ByteString
digest) = ByteString -> String
BS.unpack (ByteString -> ByteString
Base16.encode ByteString
digest)
readFileHashValue :: FilePath -> IO HashValue
readFileHashValue :: String -> IO HashValue
readFileHashValue String
tarball =
String -> IOMode -> (Handle -> IO HashValue) -> IO HashValue
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile String
tarball IOMode
ReadMode ((Handle -> IO HashValue) -> IO HashValue)
-> (Handle -> IO HashValue) -> IO HashValue
forall a b. (a -> b) -> a -> b
$ \Handle
hnd ->
HashValue -> IO HashValue
forall a. a -> IO a
evaluate (HashValue -> IO HashValue)
-> (ByteString -> HashValue) -> ByteString -> IO HashValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> HashValue
hashValue (ByteString -> IO HashValue) -> IO ByteString -> IO HashValue
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Handle -> IO ByteString
LBS.hGetContents Handle
hnd
hashFromTUF :: Sec.Hash -> HashValue
hashFromTUF :: Hash -> HashValue
hashFromTUF (Sec.Hash String
hashstr) =
case ByteString -> Either String ByteString
Base16.decode (String -> ByteString
BS.pack String
hashstr) of
#if MIN_VERSION_base16_bytestring(1,0,0)
Right ByteString
hash -> ByteString -> HashValue
HashValue ByteString
hash
Left String
_ -> String -> HashValue
forall a. HasCallStack => String -> a
error String
"hashFromTUF: cannot decode base16"
#else
(hash, trailing) | not (BS.null hash) && BS.null trailing
-> HashValue hash
_ -> error "hashFromTUF: cannot decode base16 hash"
#endif
truncateHash :: Int -> HashValue -> HashValue
truncateHash :: Int -> HashValue -> HashValue
truncateHash Int
n (HashValue ByteString
h) = ByteString -> HashValue
HashValue (Int -> ByteString -> ByteString
BS.take Int
n ByteString
h)