Permalink
Browse files

Merge pull request #4 from snoyberg/master

conduit 0.3
  • Loading branch information...
2 parents 4b2e6ee + 2669709 commit a2bd9a1f01a61c6e901cc41f308e7db77172b2fe @snoyberg snoyberg committed Mar 21, 2012
Showing with 25 additions and 25 deletions.
  1. +2 −2 crypto-conduit.cabal
  2. +19 −19 src/Crypto/Conduit.hs
  3. +4 −4 tests/runtests.hs
View
@@ -1,6 +1,6 @@
Cabal-version: >= 1.8
Name: crypto-conduit
-Version: 0.1.3.1
+Version: 0.2.0
Synopsis: Conduit interface for cryptographic operations (from crypto-api).
Homepage: https://github.com/meteficha/crypto-conduit
License: BSD3
@@ -35,7 +35,7 @@ Library
base >= 3 && < 5,
bytestring >= 0.9 && < 0.10,
cereal >= 0.3 && < 0.4,
- conduit >= 0.2 && < 0.3,
+ conduit >= 0.3 && < 0.4,
transformers == 0.2.*
if flag(old-crypto-api)
Build-depends: crypto-api >= 0.8 && < 0.9
View
@@ -39,7 +39,7 @@ module Crypto.Conduit
) where
-- from base
-import Control.Applicative ((<$>))
+import Control.Monad (liftM)
import Control.Arrow (first)
import Data.Bits (xor)
@@ -77,7 +77,7 @@ getType = undefined
-- | A 'Sink' that hashes a stream of 'B.ByteString'@s@ and
-- creates a digest @d@.
-sinkHash :: (Resource m, C.Hash ctx d) => Sink B.ByteString m d
+sinkHash :: (Monad m, C.Hash ctx d) => Sink B.ByteString m d
sinkHash = blocked AnyMultiple blockSize =$ sink
where
sink = sinkState C.initialCtx
@@ -110,7 +110,7 @@ hashFile fp = liftIO $ runResourceT (sourceFile fp $$ sinkHash)
-- | A 'Sink' that computes the HMAC of a stream of
-- 'B.ByteString'@s@ and creates a digest @d@.
-sinkHmac :: (Resource m, C.Hash ctx d) =>
+sinkHmac :: (Monad m, C.Hash ctx d) =>
#if OLD_CRYPTO_API
C.MacKey
#else
@@ -156,7 +156,7 @@ sinkHmac (C.MacKey key) = blocked AnyMultiple blockSize =$ sink
-- the block size of the cipher and fails otherwise. (Note that
-- ECB has many undesirable cryptographic properties, please
-- avoid it if you don't know what you're doing.)
-conduitEncryptEcb :: (Resource m, C.BlockCipher k) =>
+conduitEncryptEcb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> Conduit B.ByteString m B.ByteString
conduitEncryptEcb k =
@@ -170,7 +170,7 @@ conduitEncryptEcb k =
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using ECB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitDecryptEcb :: (Resource m, C.BlockCipher k) =>
+conduitDecryptEcb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> Conduit B.ByteString m B.ByteString
conduitDecryptEcb k =
@@ -187,7 +187,7 @@ conduitDecryptEcb k =
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CBC mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitEncryptCbc :: (Resource m, C.BlockCipher k) =>
+conduitEncryptCbc :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -203,7 +203,7 @@ conduitEncryptCbc k iv =
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using CBC mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitDecryptCbc :: (Resource m, C.BlockCipher k) =>
+conduitDecryptCbc :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -222,7 +222,7 @@ conduitDecryptCbc k iv =
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitEncryptCfb :: (Resource m, C.BlockCipher k) =>
+conduitEncryptCfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -238,7 +238,7 @@ conduitEncryptCfb k iv =
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using CFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitDecryptCfb :: (Resource m, C.BlockCipher k) =>
+conduitDecryptCfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -257,7 +257,7 @@ conduitDecryptCfb k iv =
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using OFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
-conduitEncryptOfb :: (Resource m, C.BlockCipher k) =>
+conduitEncryptOfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -272,7 +272,7 @@ conduitEncryptOfb k iv =
-- | Synonym for 'conduitEncryptOfb', since for OFB mode both
-- encryption and decryption are the same.
-conduitDecryptOfb :: (Resource m, C.BlockCipher k) =>
+conduitDecryptOfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Conduit B.ByteString m B.ByteString
@@ -285,7 +285,7 @@ conduitDecryptOfb = conduitEncryptOfb
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CTR mode. The input may have any length, even
-- non-multiples of the block size.
-conduitEncryptCtr :: (Resource m, C.BlockCipher k) =>
+conduitEncryptCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)
@@ -303,7 +303,7 @@ conduitEncryptCtr k iv incIV =
-- | Synonym for 'conduitEncryptCtr', since for CTR mode both
-- encryption and decryption are the same.
-conduitDecryptCtr :: (Resource m, C.BlockCipher k) =>
+conduitDecryptCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)
@@ -313,7 +313,7 @@ conduitDecryptCtr = conduitEncryptCtr
-- | An infinite stream of bytes generated by a block cipher on
-- CTR mode.
-sourceCtr :: (Resource m, C.BlockCipher k) =>
+sourceCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> Source m B.ByteString
@@ -334,7 +334,7 @@ sourceCtr k iv = sourceState iv pull
-- the input length to be a multiple of the block size of the
-- cipher and fails otherwise. (Note that CBC-MAC is not secure
-- for variable-length messages.)
-sinkCbcMac :: (Resource m, C.BlockCipher k) =>
+sinkCbcMac :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> Sink B.ByteString m B.ByteString
sinkCbcMac k = blocked StrictBlockSize blockSize =$ sink
@@ -363,7 +363,7 @@ sinkCbcMac k = blocked StrictBlockSize blockSize =$ sink
-- the 'BlockMode'). All 'Block'@s@ beside the last one will be
-- 'Full'. The last block will always be 'LastOne' with less
-- bytes than the block size, possibly zero.
-blocked :: Resource m =>
+blocked :: Monad m =>
BlockMode
-> C.ByteLength -- ^ Block size
-> Conduit B.ByteString m Block
@@ -406,12 +406,12 @@ data Block = Full B.ByteString | LastOne B.ByteString
-- | Constructs a 'Conduit' for a 'BlockCipher'.
-blockCipherConduit :: (Resource m, C.BlockCipher k) =>
+blockCipherConduit :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key (not used, just for getting block size).
-> BlockMode
-> s -- ^ Initial state.
-> (s -> B.ByteString -> (s, B.ByteString)) -- ^ Encrypt block.
- -> (s -> B.ByteString -> ResourceT m B.ByteString) -- ^ Final encryption.
+ -> (s -> B.ByteString -> m B.ByteString) -- ^ Final encryption.
-> Conduit B.ByteString m B.ByteString
blockCipherConduit key mode initialState apply final = blocked mode blockSize =$= conduit
where
@@ -424,7 +424,7 @@ blockCipherConduit key mode initialState apply final = blocked mode blockSize =$
in return (StateProducing state' [output])
push _ (LastOne input) | B.null input =
return (StateFinished Nothing [])
- push state (LastOne input) = mk <$> final state input
+ push state (LastOne input) = mk `liftM` final state input
where mk output = StateFinished Nothing [output]
close _ = fail "blockCipherConduit"
View
@@ -191,7 +191,7 @@ testBlockCipher undefinedKey = do
testBlockCipherConduit ::
Maybe C.ByteLength -- ^ Fix input length to be a multiple of the block size?
- -> (forall m. Resource m => Conduit B.ByteString m B.ByteString)
+ -> (forall m. Monad m => Conduit B.ByteString m B.ByteString)
-> (L.ByteString -> L.ByteString)
-> [Word8]
-> Bool
@@ -205,10 +205,10 @@ testBlockCipherConduit mblockSize conduit lazyfun input =
----------------------------------------------------------------------
-runPureResource :: (forall m. Resource m => ResourceT m a) -> a
-runPureResource r = runST (runResourceT r)
+runPureResource :: (forall m. Monad m => m a) -> a
+runPureResource r = runST r
-consumeAsLazy :: Resource m => Sink B.ByteString m L.ByteString
+consumeAsLazy :: Monad m => Sink B.ByteString m L.ByteString
consumeAsLazy = L.fromChunks <$> consume
fixBlockedSize :: C.ByteLength -> L.ByteString -> L.ByteString

0 comments on commit a2bd9a1

Please sign in to comment.