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

View File

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

View File

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