Hash: use Block instead of UArray to reduce memory usage

This commit is contained in:
Vincent Hanquez 2018-02-04 23:06:02 +00:00
parent 2d87929a4e
commit b4add57955
3 changed files with 8 additions and 8 deletions

View File

@ -42,8 +42,8 @@ module Crypto.Hash
) where ) where
import Basement.Types.OffsetSize (CountOf (..)) import Basement.Types.OffsetSize (CountOf (..))
import Basement.UArray (UArray, new, unsafeFreeze) import Basement.Block (Block, unsafeFreeze)
import Basement.UArray.Mutable (copyFromPtr) import Basement.Block.Mutable (copyFromPtr, new)
import Control.Monad import Control.Monad
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Types import Crypto.Hash.Types
@ -115,10 +115,10 @@ digestFromByteString = from undefined
| B.length bs == (hashDigestSize alg) = Just $ Digest $ unsafeDoIO $ copyBytes bs | B.length bs == (hashDigestSize alg) = Just $ Digest $ unsafeDoIO $ copyBytes bs
| otherwise = Nothing | otherwise = Nothing
copyBytes :: ba -> IO (UArray Word8) copyBytes :: ba -> IO (Block Word8)
copyBytes ba = do copyBytes ba = do
muArray <- new count muArray <- new count
B.withByteArray ba $ \ptr -> copyFromPtr ptr count muArray B.withByteArray ba $ \ptr -> copyFromPtr ptr muArray 0 count
unsafeFreeze muArray unsafeFreeze muArray
where where
count = CountOf (B.length ba) count = CountOf (B.length ba)

View File

@ -20,7 +20,7 @@ import Crypto.Internal.Imports
import Crypto.Internal.ByteArray (ByteArrayAccess, Bytes) import Crypto.Internal.ByteArray (ByteArrayAccess, Bytes)
import qualified Crypto.Internal.ByteArray as B import qualified Crypto.Internal.ByteArray as B
import Foreign.Ptr (Ptr) import Foreign.Ptr (Ptr)
import Basement.UArray (UArray) import Basement.Block (Block)
import Basement.NormalForm (deepseq) import Basement.NormalForm (deepseq)
import GHC.TypeLits (Nat) import GHC.TypeLits (Nat)
@ -70,7 +70,7 @@ newtype Context a = Context Bytes
-- --
-- Creating a digest from a bytearray is also possible with function -- Creating a digest from a bytearray is also possible with function
-- 'Crypto.Hash.digestFromByteString'. -- 'Crypto.Hash.digestFromByteString'.
newtype Digest a = Digest (UArray Word8) newtype Digest a = Digest (Block Word8)
deriving (Eq,Ord,ByteArrayAccess) deriving (Eq,Ord,ByteArrayAccess)
instance NFData (Digest a) where instance NFData (Digest a) where

View File

@ -227,8 +227,8 @@ Library
Crypto.Internal.Nat Crypto.Internal.Nat
Build-depends: base >= 4.6 && < 5 Build-depends: base >= 4.6 && < 5
, bytestring , bytestring
, memory >= 0.14.10 , memory >= 0.14.14
, basement , basement >= 0.0.6
, ghc-prim , ghc-prim
ghc-options: -Wall -fwarn-tabs -optc-O3 -fno-warn-unused-imports ghc-options: -Wall -fwarn-tabs -optc-O3 -fno-warn-unused-imports
if os(linux) if os(linux)