[Haskell-cafe] How to use an crypto with hackage cereal?

Yitzchak Gale gale at sefer.org
Tue Apr 21 10:43:45 UTC 2015

Magicloud Magiclouds wrote:
> I am trying to work with some binary data that encrypted by field instead of
> the result of serialization. I'd like to use Data.Serialize to wrap the data
> structure. But I could not figure out how to apply an runtime specified
> cipher method to the bytestring.

Are you using the set of crypto libraries written by
Victor Hanquez, such as cryptocipher-types,
crypto-pubkey-types, and cryptohash?

Or the set of libraries written by Thomas DuBuisson,
such as crypto-api, cipher-aes128, etc.?

Here is an example of decoding for Victor's libraries.
Encoding would be similar using Put instead of Get.
Thomas' libraries would be similar using the other

Let's say you have a type like this:

data MyCipher = MyAES | MyBlowfish | ...

Then in your cereal code you would have a Get monad
expression something like this (assuming you have
written all of the functions called parseSomething):

getStuff = do
  cipher <- parseCipher :: Get MyCipher
  clearText <- case cipher of
    MyAES -> do
      keyBS <- parseAESKey :: Get ByteString
      let key = either (error "bad AES key") id $ makeKey keyBS
          cipher = cipherInit key
      cipherText <- parseAESCipherText :: Get ByteString
      return $ ecbDecrypt cipher cipherText
    MyBlowfish -> do ...


Hope this helps,

More information about the Haskell-Cafe mailing list