Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 encode/decode safe for url and cookie #37

Closed
benbro opened this issue Jan 24, 2011 · 4 comments
Closed

base64 encode/decode safe for url and cookie #37

benbro opened this issue Jan 24, 2011 · 4 comments

Comments

@benbro
Copy link

benbro commented Jan 24, 2011

When using base64 strings in urls or http cookies we need to:

  1. Remove "=" padding
  2. Replace "+" with "-"
  3. Replace "/" with "_"
    http://en.wikipedia.org/wiki/Base64#URL_applications

I've implemented encode_base64_url/1 and decode_base64_url/1 which return binary.
Will you consider adding it?

encode_base64_url(Url) ->
    Url1 = base64:encode(Url),
    Url2 = re:replace(Url1, <<"=+$">>, <<"">>, [{return, binary}]),
    Url3 = binary:replace(Url2, <<"/">>, <<"_">>, [global]),
    binary:replace(Url3, <<"+">>, <<"-">>, [global]).

decode_base64_url(Url64) when is_list(Url64) ->
    decode_base64_url(list_to_binary(Url64));

decode_base64_url(Url64) ->
    Url1 = binary:replace(Url64, <<"-">>, <<"+">>, [global]),
    Url2 = binary:replace(Url1, <<"_">>, <<"/">>, [global]),
    Padding = case byte_size(Url2) rem 4 of
        2 ->
            <<"==">>;
        3 ->
            <<"=">>;
        _ ->
            <<"">>
    end,
    base64:decode(<<Url2/binary, Padding/binary>>).
@dylanz
Copy link

dylanz commented Dec 27, 2011

+1 ... I'd like to see something like this added as well (I'm having to deal w/ base64 cookie values).

@KlausTrainer
Copy link

+1

@qihuaheng
Copy link

+1 it fix my problem, tks.

@etrepum
Copy link
Member

etrepum commented Jul 10, 2014

Did you even look? This has been available since mochiweb 2.5.0 in the mochiweb_base64url module.

@etrepum etrepum closed this as completed Jul 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants