diff --git a/cryptohash-sha256.cabal b/cryptohash-sha256.cabal index 44cafdc..b88c975 100644 --- a/cryptohash-sha256.cabal +++ b/cryptohash-sha256.cabal @@ -1,6 +1,6 @@ -cabal-version: 1.12 +cabal-version: 2.0 name: cryptohash-sha256 -version: 0.11.102.0 +version: 0.11.102.1 synopsis: Fast, pure and practical SHA-256 implementation description: { @@ -75,17 +75,19 @@ library default-language: Haskell2010 other-extensions: BangPatterns CApiFFI + CPP Trustworthy Unsafe - build-depends: base >= 4.5 && < 4.15 - , bytestring >= 0.9.2 && < 0.12 + build-depends: base >= 4.5 && < 4.15 + , bytestring ^>= 0.9.2 || ^>= 0.10.0 || ^>= 0.11.0 ghc-options: -Wall hs-source-dirs: src exposed-modules: Crypto.Hash.SHA256 other-modules: Crypto.Hash.SHA256.FFI + Compat include-dirs: cbits executable sha256sum @@ -99,7 +101,7 @@ executable sha256sum , base , bytestring - , base16-bytestring >= 0.1.1 && < 1.1 + , base16-bytestring ^>= 0.1.1 || ^>= 1.0.0 else buildable: False @@ -114,11 +116,11 @@ test-suite test-sha256 , base , bytestring - , base16-bytestring >= 0.1.1 && < 1.1 - , SHA >= 1.6.4 && < 1.7 - , tasty == 1.1.* - , tasty-quickcheck == 0.10.* - , tasty-hunit == 0.10.* + , base16-bytestring ^>= 0.1.1 || ^>= 1.0.0 + , SHA ^>= 1.6.4 + , tasty ^>= 1.1 + , tasty-quickcheck ^>= 0.10 + , tasty-hunit ^>= 0.10 benchmark bench-sha256 default-language: Haskell2010 @@ -129,4 +131,4 @@ benchmark bench-sha256 build-depends: cryptohash-sha256 , base , bytestring - , criterion == 1.5.* + , criterion ^>= 1.5 diff --git a/src-tests/test-sha256.hs b/src-tests/test-sha256.hs index 78719c0..b3b9d24 100644 --- a/src-tests/test-sha256.hs +++ b/src-tests/test-sha256.hs @@ -1,5 +1,5 @@ -{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE FlexibleInstances #-} module Main (main) where @@ -126,12 +126,14 @@ splitB l b where (b1, b2) = B.splitAt l b +-- Minor hack to paper over backward incompat changes introduced in base16-bytestring-1.0 +class B16DecRes a where b16DecRes :: a -> ByteString +instance B16DecRes (Either String ByteString) where b16DecRes = either error id +instance B16DecRes (ByteString, ByteString) where b16DecRes = fst + b16decode :: ByteString -> ByteString -#if MIN_VERSION_base16_bytestring(1,0,0) -b16decode = either error id . B16.decode -#else -b16decode = fst . B16.decode -#endif +b16decode = b16DecRes . B16.decode + rfc4231Vectors :: [(ByteString,ByteString,ByteString)] rfc4231Vectors = -- (secrect,msg,mac) diff --git a/src/Compat.hs b/src/Compat.hs new file mode 100644 index 0000000..a5d57b2 --- /dev/null +++ b/src/Compat.hs @@ -0,0 +1,24 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE Trustworthy #-} + +-- | +-- Module : Compat +-- License : BSD-3 +-- Maintainer : Herbert Valerio Riedel +-- Stability : stable +-- +-- Compat layer to reduce code exposure to CPP to a bare minimum +-- +module Compat (constructBS) where + +import Foreign.ForeignPtr (ForeignPtr) +import Data.Word (Word8) +import Data.ByteString.Internal (ByteString (..)) + +-- | Directly construct a 'ByteString', unsafely +constructBS :: ForeignPtr Word8 -> Int -> ByteString +#if MIN_VERSION_bytestring(0,11,0) +constructBS = BS +#else +constructBS = \fp -> PS fp 0 +#endif diff --git a/src/Crypto/Hash/SHA256.hs b/src/Crypto/Hash/SHA256.hs index d14de37..848d2a5 100644 --- a/src/Crypto/Hash/SHA256.hs +++ b/src/Crypto/Hash/SHA256.hs @@ -1,5 +1,4 @@ {-# LANGUAGE BangPatterns #-} -{-# LANGUAGE CPP #-} {-# LANGUAGE Trustworthy #-} -- | @@ -93,7 +92,7 @@ module Crypto.Hash.SHA256 import Data.Bits (xor) import Data.ByteString (ByteString) import qualified Data.ByteString as B -import Data.ByteString.Internal (ByteString (..), create, +import Data.ByteString.Internal (create, createAndTrim, mallocByteString, memcpy, toForeignPtr) import qualified Data.ByteString.Lazy as L @@ -106,6 +105,7 @@ import Foreign.Ptr import Prelude hiding (init) import System.IO.Unsafe (unsafeDupablePerformIO) +import Compat (constructBS) import Crypto.Hash.SHA256.FFI -- | perform IO for hashes that do allocation and ffi. @@ -137,11 +137,7 @@ create' :: Int -> (Ptr Word8 -> IO a) -> IO (ByteString,a) create' l f = do fp <- mallocByteString l x <- withForeignPtr fp $ \p -> f p -#if MIN_VERSION_bytestring(0,11,0) - let bs = BS fp l -#else - let bs = PS fp 0 l -#endif + let bs = constructBS fp l return $! x `seq` bs `seq` (bs,x) copyCtx :: Ptr Ctx -> Ptr Ctx -> IO ()