import Utility.CopyFile
import Utility.FileMode
import Utility.Touch
-import Types.Backend
+import Utility.Hash (IncrementalVerifier(..))
import Control.Concurrent
import qualified Data.ByteString as S
import qualified Types.Remote
import Types.Remote (VerifyConfigA(..))
import qualified Types.Backend
-import Types.Backend (IncrementalVerifier(..))
import qualified Backend
import Types.Remote (unVerified, Verification(..), RetrievalSecurityPolicy(..))
+import Utility.Hash (IncrementalVerifier(..))
import Annex.WorkerPool
import Types.WorkerPool
import Types.Key
-}
{-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE BangPatterns #-}
module Backend.Hash (
backends,
import qualified Data.ByteString.Lazy as L
import Control.DeepSeq
import Control.Exception (evaluate)
-import Data.IORef
data Hash
= MD5Hash
hasher (Blake2spHash hashsize) = blake2spHasher hashsize
mkHasher :: HashAlgorithm h => (L.ByteString -> Digest h) -> Context h -> Hasher
-mkHasher h c = (show . h, mkIncrementalVerifier c)
+mkHasher h c = (show . h, mkIncrementalVerifier c descChecksum . sameCheckSum)
sha2Hasher :: HashSize -> Hasher
sha2Hasher (HashSize hashsize)
md5Hasher :: Hasher
md5Hasher = mkHasher md5 md5_context
-mkIncrementalVerifier :: HashAlgorithm h => Context h -> Key -> IO IncrementalVerifier
-mkIncrementalVerifier ctx key = do
- v <- newIORef (Just (ctx, 0))
- return $ IncrementalVerifier
- { updateIncremental = \b ->
- modifyIORef' v $ \case
- (Just (ctx', n)) ->
- let !ctx'' = hashUpdate ctx' b
- !n' = n + fromIntegral (S.length b)
- in (Just (ctx'', n'))
- Nothing -> Nothing
- , finalizeIncremental =
- readIORef v >>= \case
- (Just (ctx', _)) -> do
- let digest = hashFinalize ctx'
- return $ sameCheckSum key (show digest)
- Nothing -> return False
- , failIncremental = writeIORef v Nothing
- , positionIncremental = readIORef v >>= \case
- Just (_, n) -> return (Just n)
- Nothing -> return Nothing
- , descVerify = descChecksum
- }
-
descChecksum :: String
descChecksum = "checksum"
import Types (Annex)
import Types.Key
import Types.UUID
-import Types.Remote (Verification(..))
-import Types.Backend (IncrementalVerifier(..))
import Types.Transfer
+import Types.Remote (Verification(..))
+import Utility.Hash (IncrementalVerifier(..))
import Utility.AuthToken
import Utility.Applicative
import Utility.PartialPrelude
import Annex.Common
import Types.StoreRetrieve
-import Types.Backend
import Remote.Helper.Special
import Utility.Metered
+import Utility.Hash (IncrementalVerifier(..))
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString as S
import Annex.Common
import Types.Remote
import Types.Export
-import Types.Backend
import qualified Git
import qualified Annex
import Config
import Creds
import Utility.Metered
import Utility.Url (URLString, matchStatusCodeException, matchHttpExceptionContent)
+import Utility.Hash (IncrementalVerifier(..))
import Annex.UUID
import Remote.WebDAV.DavLocation
import Types.ProposedAccepted
import Types.KeySource
import Utility.Metered
import Utility.FileSystemEncoding
-
-import Data.ByteString (ByteString)
+import Utility.Hash (IncrementalVerifier)
data BackendA a = Backend
{ backendVariety :: KeyVariety
instance Eq (BackendA a) where
a == b = backendVariety a == backendVariety b
-
-data IncrementalVerifier = IncrementalVerifier
- { updateIncremental :: ByteString -> IO ()
- -- ^ Called repeatedly on each peice of the content.
- , finalizeIncremental :: IO Bool
- -- ^ Called once the full content has been sent, returns true
- -- if the hash verified.
- , failIncremental :: IO ()
- -- ^ Call if the incremental verification needs to fail.
- , positionIncremental :: IO (Maybe Integer)
- -- ^ Returns the number of bytes that have been fed to this
- -- incremental verifier so far. (Nothing if failIncremental was
- -- called.)
- , descVerify :: String
- -- ^ A description of what is done to verify the content.
- }
import Types.Export
import Types.Import
import Types.RemoteConfig
-import Types.Backend (IncrementalVerifier)
+import Utility.Hash (IncrementalVerifier)
import Config.Cost
import Utility.Metered
import Git.Types (RemoteName)
import Annex.Common
import Utility.Metered
-import Types.Backend (IncrementalVerifier)
+import Utility.Hash (IncrementalVerifier)
import qualified Data.ByteString.Lazy as L
-{- Convenience wrapper around cryptonite's hashing. -}
+{- Convenience wrapper around cryptonite's hashing.
+ -
+ - Copyright 2013-2021 Joey Hess <id@joeyh.name>
+ -
+ - License: BSD-2-clause
+ -}
+
+{-# LANGUAGE BangPatterns #-}
module Utility.Hash (
sha1,
Mac(..),
calcMac,
props_macs_stable,
+ IncrementalVerifier(..),
+ mkIncrementalVerifier,
) where
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
+import Data.IORef
import "cryptonite" Crypto.MAC.HMAC hiding (Context)
import "cryptonite" Crypto.Hash
where
key = T.encodeUtf8 $ T.pack "foo"
msg = T.encodeUtf8 $ T.pack "bar"
+
+data IncrementalVerifier = IncrementalVerifier
+ { updateIncremental :: S.ByteString -> IO ()
+ -- ^ Called repeatedly on each peice of the content.
+ , finalizeIncremental :: IO Bool
+ -- ^ Called once the full content has been sent, returns true
+ -- if the hash verified.
+ , failIncremental :: IO ()
+ -- ^ Call if the incremental verification needs to fail.
+ , positionIncremental :: IO (Maybe Integer)
+ -- ^ Returns the number of bytes that have been fed to this
+ -- incremental verifier so far. (Nothing if failIncremental was
+ -- called.)
+ , descVerify :: String
+ -- ^ A description of what is done to verify the content.
+ }
+
+mkIncrementalVerifier :: HashAlgorithm h => Context h -> String -> (String -> Bool) -> IO IncrementalVerifier
+mkIncrementalVerifier ctx descverify samechecksum = do
+ v <- newIORef (Just (ctx, 0))
+ return $ IncrementalVerifier
+ { updateIncremental = \b ->
+ modifyIORef' v $ \case
+ (Just (ctx', n)) ->
+ let !ctx'' = hashUpdate ctx' b
+ !n' = n + fromIntegral (S.length b)
+ in (Just (ctx'', n'))
+ Nothing -> Nothing
+ , finalizeIncremental =
+ readIORef v >>= \case
+ (Just (ctx', _)) -> do
+ let digest = hashFinalize ctx'
+ return $ samechecksum (show digest)
+ Nothing -> return False
+ , failIncremental = writeIORef v Nothing
+ , positionIncremental = readIORef v >>= \case
+ Just (_, n) -> return (Just n)
+ Nothing -> return Nothing
+ , descVerify = descverify
+ }