Skip to content

Commit

Permalink
conduit 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jun 20, 2012
1 parent 1ee4c2f commit 8f728fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crypto-conduit.cabal
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
Cabal-version: >= 1.8 Cabal-version: >= 1.8
Name: crypto-conduit Name: crypto-conduit
Version: 0.3.2 Version: 0.4.0
Synopsis: Conduit interface for cryptographic operations (from crypto-api). Synopsis: Conduit interface for cryptographic operations (from crypto-api).
Homepage: https://github.com/meteficha/crypto-conduit Homepage: https://github.com/meteficha/crypto-conduit
License: BSD3 License: BSD3
Expand Down Expand Up @@ -35,7 +35,7 @@ Library
base >= 3 && < 5, base >= 3 && < 5,
bytestring >= 0.9 && < 0.10, bytestring >= 0.9 && < 0.10,
cereal >= 0.3 && < 0.4, cereal >= 0.3 && < 0.4,
conduit >= 0.4 && < 0.5, conduit >= 0.5 && < 0.6,
transformers >= 0.2 && < 0.4 transformers >= 0.2 && < 0.4
if flag(old-crypto-api) if flag(old-crypto-api)
Build-depends: crypto-api >= 0.8 && < 0.9 Build-depends: crypto-api >= 0.8 && < 0.9
Expand Down
46 changes: 25 additions & 21 deletions src/Crypto/Conduit.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import qualified Crypto.Types as C


-- from conduit -- from conduit
import Data.Conduit import Data.Conduit
import qualified Data.Conduit.Internal as CI
import Data.Conduit.Binary (sourceFile) import Data.Conduit.Binary (sourceFile)


-- from transformers -- from transformers
Expand Down Expand Up @@ -93,17 +92,17 @@ sinkHash =


blockSize = (C.blockLength .::. getType self) `div` 8 blockSize = (C.blockLength .::. getType self) `div` 8


getBlock :: Monad m => BlockMode -> C.ByteLength -> Sink B.ByteString m Block getBlock :: Monad m => BlockMode -> C.ByteLength -> Pipe B.ByteString B.ByteString o u m Block
getBlock blockMode blockSize = getBlock blockMode blockSize =
go id go id
where where
go front = NeedInput (push front) (close front) go front = await >>= maybe (close front) (push front)


push front bs' = push front bs' =
case compare (B.length bs) blockSize of case compare (B.length bs) blockSize of
LT -> go $ B.append bs LT -> go $ B.append bs
EQ -> Done Nothing $ Full bs EQ -> return $ Full bs
GT -> Done (Just y) $ Full x GT -> leftover y >> return (Full x)
where where
bs = front bs' bs = front bs'
(x, y) = B.splitAt splitter bs (x, y) = B.splitAt splitter bs
Expand All @@ -113,7 +112,7 @@ getBlock blockMode blockSize =
StrictBlockSize -> blockSize StrictBlockSize -> blockSize
AnyMultiple -> B.length bs - (B.length bs `mod` blockSize) AnyMultiple -> B.length bs - (B.length bs `mod` blockSize)


close front = Done Nothing (LastOne $ front B.empty) close front = return $ LastOne $ front B.empty


-- | Hashes the whole contents of the given file in constant -- | Hashes the whole contents of the given file in constant
-- memory. This function is just a convenient wrapper around -- memory. This function is just a convenient wrapper around
Expand Down Expand Up @@ -340,12 +339,14 @@ sourceCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key. k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector. -> C.IV k -- ^ Initialization vector.
-> Source m B.ByteString -> Source m B.ByteString
sourceCtr k iv = sourceState iv pull sourceCtr k =
where loop
pull iv' = where
let !iv'' = C.incIV iv' loop iv =
block = C.encryptBlock k $ S.encode iv' yield block >> loop iv'
in return (StateOpen iv'' block) where
!iv' = C.incIV iv
block = C.encryptBlock k $ S.encode iv




---------------------------------------------------------------------- ----------------------------------------------------------------------
Expand Down Expand Up @@ -390,15 +391,17 @@ blocked :: Monad m =>
BlockMode BlockMode
-> C.ByteLength -- ^ Block size -> C.ByteLength -- ^ Block size
-> Conduit B.ByteString m Block -> Conduit B.ByteString m Block
blocked mode blockSize = conduitState B.empty push close blocked mode blockSize = go B.empty
where where
go x = awaitE >>= either (close x) (push x)

block = case mode of block = case mode of
StrictBlockSize -> blockStrict [] StrictBlockSize -> blockStrict id
AnyMultiple -> blockAny AnyMultiple -> blockAny
where where
blockStrict acc bs blockStrict front bs
| B.length bs < blockSize = (reverse acc, bs) | B.length bs < blockSize = (front [], bs)
| otherwise = blockStrict (Full this : acc) rest | otherwise = blockStrict (front . (Full this :)) rest
where (this, rest) = B.splitAt blockSize bs where (this, rest) = B.splitAt blockSize bs


blockAny bs blockAny bs
Expand All @@ -410,11 +413,12 @@ blocked mode blockSize = conduitState B.empty push close
| B.null bs1 = bs2 | B.null bs1 = bs2
| otherwise = B.append bs1 bs2 | otherwise = B.append bs1 bs2


push acc = return . mk . block . append acc push acc x = mapM_ yield blks >> go rest
where where
mk (blks, rest) = (StateProducing rest blks) (blks, rest) = block bs
bs = append acc x


close = return . (:[]) . LastOne close acc r = yield (LastOne acc) >> return r




-- | How 'Block's should be returned, either with strictly the -- | How 'Block's should be returned, either with strictly the
Expand Down Expand Up @@ -442,7 +446,7 @@ blockCipherConduit key mode initialState apply final =
blockSize = (C.blockSize .::. key) `div` 8 blockSize = (C.blockSize .::. key) `div` 8


go state = do go state = do
x <- CI.sinkToPipe $ getBlock mode blockSize x <- getBlock mode blockSize
case x of case x of
Full input -> do Full input -> do
let (!state', !output) = apply state input let (!state', !output) = apply state input
Expand Down

0 comments on commit 8f728fe

Please sign in to comment.