base64url-1.0/0000755000232200023220000000000012560554034013533 5ustar debalancedebalancebase64url-1.0/LICENSE.txt0000644000232200023220000000207412560554034015361 0ustar debalancedebalanceCopyright (c) 2013 Vladimir Dronnikov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. base64url-1.0/README.md0000644000232200023220000000405612560554034015017 0ustar debalancedebalanceBase64Url ============== [![Hex.pm](https://img.shields.io/hexpm/v/base64url.svg)](https://hex.pm/packages/base64url) Standalone [URL safe](http://tools.ietf.org/html/rfc4648) base64-compatible codec. Usage -------------- URL-Safe base64 encoding: ```erlang base64url:encode(<<255,127,254,252>>). <<"_3_-_A">> base64url:decode(<<"_3_-_A">>). <<255,127,254,252>> ``` Vanilla base64 encoding: ```erlang base64:encode(<<255,127,254,252>>). <<"/3/+/A==">> ``` Some systems in the wild use base64 URL encoding, but keep the padding for MIME compatibility (base64 Content-Transfer-Encoding). To interact with such systems, use: ```erlang base64url:encode_mime(<<255,127,254,252>>). <<"_3_-_A==">> base64url:decode(<<"_3_-_A==">>). <<255,127,254,252>> ``` Thanks -------------- To authors of [this](https://github.com/basho/riak_control/blob/master/src/base64url.erl) and [this](https://github.com/mochi/mochiweb/blob/master/src/mochiweb_base64url.erl). [License](base64url/blob/master/LICENSE.txt) ------- Copyright (c) 2013 Vladimir Dronnikov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. base64url-1.0/Makefile0000644000232200023220000000057012560554034015175 0ustar debalancedebalanceall: deps compile check test deps: rebar get-deps compile: rebar compile run: compile sh start.sh clean: rebar clean rm -fr ebin .ct test/*.beam check: rebar eunit skip_deps=true test: deps compile check ##rebar ct #mkdir -p .ct #ct_run -dir test -logdir .ct -pa ebin dist: deps compile echo TODO .PHONY: all deps compile check test run clean dist .SILENT: base64url-1.0/src/0000755000232200023220000000000012560554034014322 5ustar debalancedebalancebase64url-1.0/src/base64url.erl0000644000232200023220000000572312560554034016644 0ustar debalancedebalance%% %% @doc URL safe base64-compatible codec. %% %% Based heavily on the code extracted from: %% https://github.com/basho/riak_control/blob/master/src/base64url.erl and %% https://github.com/mochi/mochiweb/blob/master/src/mochiweb_base64url.erl. %% -module(base64url). -author('Vladimir Dronnikov '). -export([ decode/1, encode/1, encode_mime/1 ]). -spec encode( binary() | iolist() ) -> binary(). encode(Bin) when is_binary(Bin) -> << << (urlencode_digit(D)) >> || <> <= base64:encode(Bin), D =/= $= >>; encode(L) when is_list(L) -> encode(iolist_to_binary(L)). -spec encode_mime( binary() | iolist() ) -> binary(). encode_mime(Bin) when is_binary(Bin) -> << << (urlencode_digit(D)) >> || <> <= base64:encode(Bin) >>; encode_mime(L) when is_list(L) -> encode_mime(iolist_to_binary(L)). -spec decode( binary() | iolist() ) -> binary(). decode(Bin) when is_binary(Bin) -> Bin2 = case byte_size(Bin) rem 4 of % 1 -> << Bin/binary, "===" >>; 2 -> << Bin/binary, "==" >>; 3 -> << Bin/binary, "=" >>; _ -> Bin end, base64:decode(<< << (urldecode_digit(D)) >> || <> <= Bin2 >>); decode(L) when is_list(L) -> decode(iolist_to_binary(L)). urlencode_digit($/) -> $_; urlencode_digit($+) -> $-; urlencode_digit(D) -> D. urldecode_digit($_) -> $/; urldecode_digit($-) -> $+; urldecode_digit(D) -> D. -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). aim_test() -> % vanilla base64 produce URL unsafe output ?assertNotEqual( binary:match(base64:encode([255,127,254,252]), [<<"=">>, <<"/">>, <<"+">>]), nomatch), % this codec produce URL safe output ?assertEqual( binary:match(encode([255,127,254,252]), [<<"=">>, <<"/">>, <<"+">>]), nomatch), % the mime codec produces URL unsafe output, but only because of padding ?assertEqual( binary:match(encode_mime([255,127,254,252]), [<<"/">>, <<"+">>]), nomatch), ?assertNotEqual( binary:match(encode_mime([255,127,254,252]), [<<"=">>]), nomatch). codec_test() -> % codec is lossless with or without padding ?assertEqual(decode(encode(<<"foo">>)), <<"foo">>), ?assertEqual(decode(encode(<<"foo1">>)), <<"foo1">>), ?assertEqual(decode(encode(<<"foo12">>)), <<"foo12">>), ?assertEqual(decode(encode(<<"foo123">>)), <<"foo123">>), ?assertEqual(decode(encode_mime(<<"foo">>)), <<"foo">>), ?assertEqual(decode(encode_mime(<<"foo1">>)), <<"foo1">>), ?assertEqual(decode(encode_mime(<<"foo12">>)), <<"foo12">>), ?assertEqual(decode(encode_mime(<<"foo123">>)), <<"foo123">>). iolist_test() -> % codec supports iolists ?assertEqual(decode(encode("foo")), <<"foo">>), ?assertEqual(decode(encode(["fo", "o1"])), <<"foo1">>), ?assertEqual(decode(encode([255,127,254,252])), <<255,127,254,252>>), ?assertEqual(decode(encode_mime("foo")), <<"foo">>), ?assertEqual(decode(encode_mime(["fo", "o1"])), <<"foo1">>), ?assertEqual(decode(encode_mime([255,127,254,252])), <<255,127,254,252>>). -endif. base64url-1.0/src/base64url.app.src0000644000232200023220000000050312560554034017417 0ustar debalancedebalance{application, base64url, [ {description, "URL safe base64-compatible codec"}, {vsn, "0.0.1"}, {id, "git"}, {registered, []}, {applications, [ kernel, stdlib ]}, {env, []}, {contributors, ["Vladimir Dronnikov"]}, {licenses, ["MIT"]}, {links, [{"Github", "https://github.com/dvv/base64url"}]} ]}. base64url-1.0/rebar.config0000644000232200023220000000023112560554034016011 0ustar debalancedebalance{lib_dirs, ["deps"]}. {erl_opts, [ debug_info, warn_format, warn_export_vars, warn_obsolete_guard, warn_bif_clash ]}. {cover_enabled, true}.