[web-devel] [ANNOUNCE] First release of crypto-conduit

Grant thelff at hotmail.com
Sun Jan 8 01:04:34 CET 2012

Thanks for getting back to me so fast!  Actually, I need to include a string 
with the file length plus the file stream itself and THEN hash that whole thing 
together. The example below works, but the file is processed twice. 

Alternatively, I could create a specialised Source that gets the file size from 
the handle and then processes the file but that looks ugly. Any ideas?


{-# LANGUAGE NoMonomorphismRestriction #-}
{-# OPTIONS -Wall #-}
import Crypto.Conduit
import Crypto.Hash.SHA1 (SHA1)
import Data.Conduit
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C8
import Data.Monoid 

sinkPrefix::Resource m  => Sink B.ByteString m B.ByteString
sinkPrefix = sinkState 
  (\sz bs -> return (sz + B.length bs,Processing))
  (\sz -> return $ C8.pack ("blob " ++ show sz ++ "\0"))
digestFile::FilePath -> IO SHA1
digestFile fn=do
  a1 <- runResourceT $ CB.sourceFile fn $$ sinkPrefix 
  runResourceT $ (mconcat [CL.sourceList [a1], CB.sourceFile fn]) $$ sinkHash 

More information about the web-devel mailing list