<div dir="ltr">I see. Thank you. So I will find other solutions.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 22, 2015 at 12:01 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 22 April 2015 at 13:58, Magicloud Magiclouds<br>
<span class=""><<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
> Ah, sorry, the "encode" function is the one from cereal, not to "generate a<br>
> particular encoding for a value".<br>
<br>
</span>I meant "encode" as in "use a specific representation rather than<br>
directly converting it on a constructor-by-constructor basis as is the<br>
default for Get/Put".<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> On Wed, Apr 22, 2015 at 11:56 AM, Ivan Lazar Miljenovic<br>
> <<a href="mailto:ivan.miljenovic@gmail.com">ivan.miljenovic@gmail.com</a>> wrote:<br>
>><br>
>> On 22 April 2015 at 13:52, Magicloud Magiclouds<br>
>> <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
>> > Say the data structure is:<br>
>> ><br>
>> > data Person = Person { name :: String<br>
>> >                        , gender :: Gender<br>
>> >                        , age :: Int }<br>
>> ><br>
>> > Then the process to generate the binary is:<br>
>> ><br>
>> > msum $ map (encrypt . encode) [ length $ name person, name person,<br>
>> > gender<br>
>> > person, age person ]<br>
>> ><br>
>> > Above process is just persudo in Haskell, the actual is not coded in<br>
>> > Haskell.<br>
>><br>
>> Except that binary and cereal are for serializing Haskell values<br>
>> directly; you seem to be wanting to parse and generate a particular<br>
>> encoding for a value.  In which case, I don't think binary or cereal<br>
>> is really appropriate.<br>
>><br>
>> ><br>
>> > On Wed, Apr 22, 2015 at 11:44 AM, Andrey Sverdlichenko <<a href="mailto:blaze@ruddy.ru">blaze@ruddy.ru</a>><br>
>> > wrote:<br>
>> >><br>
>> >> Could you describe encrypted data format? I can't understand problem<br>
>> >> with<br>
>> >> decryption.<br>
>> >><br>
>> >><br>
>> >><br>
>> >> On Tue, Apr 21, 2015 at 8:41 PM, Magicloud Magiclouds<br>
>> >> <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
>> >>><br>
>> >>> That is the ugliness of the original binary data. The encryption is<br>
>> >>> not<br>
>> >>> by fixed block size. So decrypt cannot be run before the get* helpers.<br>
>> >>> So<br>
>> >>> decrypt-runGetPartial-decrypt-runGetPartial loop would not work.<br>
>> >>><br>
>> >>> I need a "post process" in Get. For example, "portNumber <- liftM<br>
>> >>> decrypt<br>
>> >>> getWord16be; return $ MyDataType portNumber". But currently I could<br>
>> >>> not pass<br>
>> >>> decrypt into get function.<br>
>> >>><br>
>> >>> On Wed, Apr 22, 2015 at 11:26 AM, Andrey Sverdlichenko<br>
>> >>> <<a href="mailto:blaze@ruddy.ru">blaze@ruddy.ru</a>><br>
>> >>> wrote:<br>
>> >>>><br>
>> >>>> You can't really modify source bytestring inside Get monad, and this<br>
>> >>>> is<br>
>> >>>> what decryption effectively do. The only option I know about is to<br>
>> >>>> run<br>
>> >>>> another parser inside Get monad. I'd rather write<br>
>> >>>> decrypt-runGetPartial-decrypt-runGetPartial loop and return Fail from<br>
>> >>>> it on<br>
>> >>>> decryption error.<br>
>> >>>><br>
>> >>>><br>
>> >>>><br>
>> >>>> On Tue, Apr 21, 2015 at 8:12 PM, Magicloud Magiclouds<br>
>> >>>> <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
>> >>>>><br>
>> >>>>> How about fail in Get monad if decrypt failed? So decrypt failure<br>
>> >>>>> would<br>
>> >>>>> lead to a result of "Left String" on decode.<br>
>> >>>>><br>
>> >>>>> On Wed, Apr 22, 2015 at 11:05 AM, Andrey Sverdlichenko<br>
>> >>>>> <<a href="mailto:blaze@ruddy.ru">blaze@ruddy.ru</a>><br>
>> >>>>> wrote:<br>
>> >>>>>><br>
>> >>>>>> You probably should not merge decrypt and decode operations, it is<br>
>> >>>>>> bad<br>
>> >>>>>> crypto habit. Until you decrypted and verified integrity of data,<br>
>> >>>>>> parsing is<br>
>> >>>>>> dangerous and opening your service to attacks. Correct way of<br>
>> >>>>>> implementing<br>
>> >>>>>> this would be to pass ciphertext to decryption function and run<br>
>> >>>>>> parser only<br>
>> >>>>>> if decryption is successful. If bytestring is too big to be<br>
>> >>>>>> decrypted in one<br>
>> >>>>>> piece, consider encrypting it in blocks and feeding decrypted parts<br>
>> >>>>>> to<br>
>> >>>>>> parser.<br>
>> >>>>>><br>
>> >>>>>><br>
>> >>>>>><br>
>> >>>>>> On Tue, Apr 21, 2015 at 7:49 PM, Magicloud Magiclouds<br>
>> >>>>>> <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br>
>> >>>>>>><br>
>> >>>>>>> Similar as you envisaged. I would receive a bytestring data and a<br>
>> >>>>>>> config point out what cipher to use. Then I deserialize the data<br>
>> >>>>>>> to a data<br>
>> >>>>>>> type with some fields. The serialize process is something like:<br>
>> >>>>>>><br>
>> >>>>>>> msum $ map (encrypt . encode) [field1, field2, field3]<br>
>> >>>>>>><br>
>> >>>>>>> I could parse the bytestring outside Get/Put monads. But I think<br>
>> >>>>>>> that<br>
>> >>>>>>> looks ugly. I really want to embed the decrypt process into<br>
>> >>>>>>> Get/Put monads.<br>
>> >>>>>>><br>
>> >>>>>>> On Tue, Apr 21, 2015 at 10:08 PM, Ivan Lazar Miljenovic<br>
>> >>>>>>> <<a href="mailto:ivan.miljenovic@gmail.com">ivan.miljenovic@gmail.com</a>> wrote:<br>
>> >>>>>>>><br>
>> >>>>>>>> On 21 April 2015 at 23:58, Magicloud Magiclouds<br>
>> >>>>>>>> <<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<br>
>> >>>>>>>> > binary<br>
>> >>>>>>>> > data? I<br>
>> >>>>>>>> > mean I need to pass the decrypt/encrypt function to get/put<br>
>> >>>>>>>> > while<br>
>> >>>>>>>> > they do<br>
>> >>>>>>>> > not accept parameters. Should I use Reader here?<br>
>> >>>>>>>><br>
>> >>>>>>>> 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<br>
>> >>>>>>>> with<br>
>> >>>>>>>> Bytestrings, unless you're wanting to compose them with others<br>
>> >>>>>>>> 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<br>
>> >>>>>>>> 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>
>> >>>>>>>><br>
>> >>>>>>>> ><br>
>> >>>>>>>> > On Tue, Apr 21, 2015 at 6:43 PM, Yitzchak Gale <<a href="mailto:gale@sefer.org">gale@sefer.org</a>><br>
>> >>>>>>>> > wrote:<br>
>> >>>>>>>> >><br>
>> >>>>>>>> >> Magicloud Magiclouds wrote:<br>
>> >>>>>>>> >> > I am trying to work with some binary data that encrypted by<br>
>> >>>>>>>> >> > field<br>
>> >>>>>>>> >> > instead of<br>
>> >>>>>>>> >> > the result of serialization. I'd like to use Data.Serialize<br>
>> >>>>>>>> >> > to<br>
>> >>>>>>>> >> > wrap the<br>
>> >>>>>>>> >> > data<br>
>> >>>>>>>> >> > structure. But I could not figure out how to apply an<br>
>> >>>>>>>> >> > runtime<br>
>> >>>>>>>> >> > 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<br>
>> >>>>>>>> >> 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>
>> >>>>>>>> > _______________________________________________<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>
>> >>>>>>>><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>
>> >>>>>>><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>
>> >>>>>><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>
>> >>>><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>
>> >><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>
>> > _______________________________________________<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>
>><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>
><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>
<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>
</div></div></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>