<div dir="ltr">Similar as you envisaged. I would receive a bytestring data and a config point out what cipher to use. Then I deserialize the data to a data type with some fields. The serialize process is something like:<div><br></div><div>msum $ map (encrypt . encode) [field1, field2, field3]</div><div><br></div><div>I could parse the bytestring outside Get/Put monads. But I think that looks ugly. I really want to embed the decrypt process into Get/Put monads.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 21, 2015 at 10:08 PM, Ivan Lazar Miljenovic <span dir="ltr"><<a href="mailto:ivan.miljenovic@gmail.com" target="_blank">ivan.miljenovic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 21 April 2015 at 23:58, Magicloud Magiclouds<br>
<span class=""><<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
> Thank you. But how if the cipher was specified outside the binary data? I<br>
> mean I need to pass the decrypt/encrypt function to get/put while they do<br>
> not accept parameters. Should I use Reader here?<br>
<br>
</span>Maybe you could explain what you're doing better.<br>
<br>
I would envisage that you would get a Bytestring/Text value, then<br>
encrypt/decrypt and then put it back (though if you're dealing with<br>
Bytestrings, unless you're wanting to compose them with others there's<br>
no real need to use Get and Put as you'll have the resulting<br>
Bytestring already...).<br>
<br>
Or are you wanting to implement your own encryption/decryption scheme?<br>
In which case, you might want to either:<br>
<br>
a) write custom functions in the Get and Put monads OR<br>
<br>
b) write custom parsers (e.g. attoparsec) and builders (using the<br>
Builder module in bytestring); this is probably going to suit you<br>
better.<br>
<div><div class="h5"><br>
><br>
> On Tue, Apr 21, 2015 at 6:43 PM, Yitzchak Gale <<a href="mailto:gale@sefer.org">gale@sefer.org</a>> wrote:<br>
>><br>
>> Magicloud Magiclouds wrote:<br>
>> > I am trying to work with some binary data that encrypted by field<br>
>> > instead of<br>
>> > the result of serialization. I'd like to use Data.Serialize to wrap the<br>
>> > data<br>
>> > structure. But I could not figure out how to apply an runtime specified<br>
>> > cipher method to the bytestring.<br>
>><br>
>> Are you using the set of crypto libraries written by<br>
>> Victor Hanquez, such as cryptocipher-types,<br>
>> crypto-pubkey-types, and cryptohash?<br>
>><br>
>> Or the set of libraries written by Thomas DuBuisson,<br>
>> such as crypto-api, cipher-aes128, etc.?<br>
>><br>
>> Here is an example of decoding for Victor's libraries.<br>
>> Encoding would be similar using Put instead of Get.<br>
>> Thomas' libraries would be similar using the other<br>
>> API.<br>
>><br>
>> Let's say you have a type like this:<br>
>><br>
>> data MyCipher = MyAES | MyBlowfish | ...<br>
>><br>
>> Then in your cereal code you would have a Get monad<br>
>> expression something like this (assuming you have<br>
>> written all of the functions called parseSomething):<br>
>><br>
>> getStuff = do<br>
>> cipher <- parseCipher :: Get MyCipher<br>
>> clearText <- case cipher of<br>
>> MyAES -> do<br>
>> keyBS <- parseAESKey :: Get ByteString<br>
>> let key = either (error "bad AES key") id $ makeKey keyBS<br>
>> cipher = cipherInit key<br>
>> cipherText <- parseAESCipherText :: Get ByteString<br>
>> return $ ecbDecrypt cipher cipherText<br>
>> MyBlowfish -> do ...<br>
>><br>
>> etc.<br>
>><br>
>> Hope this helps,<br>
>> Yitz<br>
><br>
><br>
><br>
><br>
> --<br>
> 竹密岂妨流水过<br>
> 山高哪阻野云飞<br>
><br>
> And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.<br>
><br>
</div></div>> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
><br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Ivan Lazar Miljenovic<br>
<a href="mailto:Ivan.Miljenovic@gmail.com">Ivan.Miljenovic@gmail.com</a><br>
<a href="http://IvanMiljenovic.wordpress.com" target="_blank">http://IvanMiljenovic.wordpress.com</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">竹密岂妨流水过<br>山高哪阻野云飞<br><br>And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.</div>
</div>