Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

CBC for file #79

Open
nrolland opened this issue Apr 18, 2016 · 1 comment
Open

CBC for file #79

nrolland opened this issue Apr 18, 2016 · 1 comment
Labels

Comments

@nrolland
Copy link

hi,

is there a simple example somewhere to do some file level CBC encryption ?

thank you !

@tekul tekul added the question label Apr 18, 2016
@vincenthz
Copy link
Member

vincenthz commented Apr 27, 2016

There isn't sadly; it would be nice to start collecting some small example like this. @nrolland: care to write one maybe ? I'm happy to guide you to write the right thing.

It should roughly looks like in pseudo code:

import qualified Data.ByteString.Lazy as L
import Crypto.Error
import Crypto.Cipher

encrypt key in out = do
    let cipher = (throwCryptoError $ cipherInit key) :: AES128
    inData <- L.readFile in
    iv <- makeIV <$> getRandom 16
    let outData = loop cipher iv inData
    L.writeFile out (L.fromChunks outData)
  where loop cipher iv l =
                  let (a,b) = L.splitAt 1024 l
                   in if L.empty b
                            then cbcEncrypt cipher iv (applyPadding b)
                            else let out = cbcEncrypt cipher iv (L.toStrict a) 
                                       nextIv = makeIV $ takeLast 16 out
                                     in out : loop cipher nextIv b

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants