[erlang-questions] base64:decode fails on facebook base64 encoded strings

Christopher Vance cjsvance@REDACTED
Thu Dec 29 06:37:23 CET 2011


Trailing '=' if needed to bring the payload to a multiple of 4
characters are not optional. You could probably add extra '=' after
that if you like.

On 29 December 2011 16:29, Steve Davis <steven.charles.davis@REDACTED> wrote:
> These two refs are interesting in this context:
>
> http://en.wikipedia.org/wiki/Base64#Variants_summary_table
> http://tools.ietf.org/html/rfc4648#section-5
>
> /s
>
> On Dec 28, 10:30 pm, Max Bourinov <bouri...@REDACTED> wrote:
>> So Erlangers,
>>
>> I wrote about this little bug in Facebook and I got an answer!
>>
>> >>I believe the trailing = is an optional padding in base64 your code needs
>>
>> to accept base64 with and without trailing '=' characters. See last part ofhttp://email.about.com/cs/standards/a/base64_encoding.htm
>>
>> This is what they said. That is very interesting, I personally would not
>> omit data from protocol specification, but it appears that if it possible.
>> Anyway I didn't see anything about omitting herehttp://tools.ietf.org/html/rfc1421orhttp://tools.ietf.org/html/rfc2045
>>
>> So, if you ever face such a loose implementation of base64 here is a
>> snippet that might help you to deal with it:
>>
>> -spec fb_decode_base64(Base64 :: list()) -> binary() | error.
>> fb_decode_base64(Base64) when is_list(Base64) ->
>>     try base64:decode(Base64)
>>     catch
>>         error:_ -> % could be missing =
>>                 try base64:decode(Base64 ++ "=")
>>                 catch
>>                         error:_ -> % could be missing ==
>>                                 try base64:decode(Base64 ++ "==")
>>                                 catch
>>                                         error:_ -> % base64 is really wrong. we cannot fix it
>>                                                 error
>>                                 end
>>                 end
>>
>>     end.
>>
>> On Wed, Dec 21, 2011 at 7:54 PM, Heinz N. Gies <he...@REDACTED> wrote:
>>
>>
>>
>>
>>
>>
>>
>> > As an addition look closely at the decoding on the webside, it also fails
>> > the last two characters are missing unless you add the missing '=' to the
>> > base64 encoded data.
>>
>> > Regards,
>> > Heinz
>>
>> > --
>> > Heinz N. Gies
>> > he...@REDACTED
>> >http://licenser.net
>>
>> > On Dec 21, 2011, at 16:44, Max Bourinov wrote:
>>
>> >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am9RcG9aQ3ZWYW9YVUN6N21zU3Bjb0FuWG hKRDVRU0lRIiwidXNlciI6eyJjb3VudHJ5IjoicnUiLCJsb2NhbGUiOiJlbl9VUyIsImFnZSI6e yJtaW4iOjIxfX0sInVzZXJfaWQiOiIxMDI3NDE5NzkzIn0
>>
>> > You can easily decode it with online tool like this:
>> >http://www.motobit.com/util/base64-decoder-encoder.asp
>>
>> > But lovely Erlang says:
>>
>> > ** exception error: no function clause matching base64:decode("Jye",
>>
>> > [133,177,157,189,201,165,209,161,180,136,232,137,33,53,5,12,
>>
>> >  181,77,33,4,200,212,216,136,176,137,141|...])
>> >      in function  base64:decode/1
>>
>> > Seems something wrong with pattern matching or what could it be? And most
>> > interesting question - how to find a proper work around?
>>
>> > Best regards,
>> > Max
>>
>> >  _______________________________________________
>> > erlang-questions mailing list
>> > erlang-questi...@REDACTED
>> >http://erlang.org/mailman/listinfo/erlang-questions
>>
>> > _______________________________________________
>> > erlang-questions mailing list
>> > erlang-questi...@REDACTED
>> >http://erlang.org/mailman/listinfo/erlang-questions
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questi...@REDACTED://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



-- 
Christopher Vance



More information about the erlang-questions mailing list