Deprecate misc:encode_base64/1 and misc:decode_base64/1

This commit is contained in:
Evgeniy Khramtsov 2017-05-23 10:43:26 +03:00
parent 268065e5c4
commit e93762a720
10 changed files with 48 additions and 98 deletions

View File

@ -112,9 +112,9 @@ mech_step(#state{step = 2} = State, ClientIn) ->
true -> true ->
{StoredKey, ServerKey, Salt, IterationCount} = {StoredKey, ServerKey, Salt, IterationCount} =
if is_record(Pass, scram) -> if is_record(Pass, scram) ->
{misc:decode_base64(Pass#scram.storedkey), {base64:decode(Pass#scram.storedkey),
misc:decode_base64(Pass#scram.serverkey), base64:decode(Pass#scram.serverkey),
misc:decode_base64(Pass#scram.salt), base64:decode(Pass#scram.salt),
Pass#scram.iterationcount}; Pass#scram.iterationcount};
true -> true ->
TempSalt = TempSalt =
@ -132,14 +132,14 @@ mech_step(#state{step = 2} = State, ClientIn) ->
str:substr(ClientIn, str:substr(ClientIn,
str:str(ClientIn, <<"n=">>)), str:str(ClientIn, <<"n=">>)),
ServerNonce = ServerNonce =
misc:encode_base64(randoms:bytes(?NONCE_LENGTH)), base64:encode(randoms:bytes(?NONCE_LENGTH)),
ServerFirstMessage = ServerFirstMessage =
iolist_to_binary( iolist_to_binary(
["r=", ["r=",
ClientNonce, ClientNonce,
ServerNonce, ServerNonce,
",", "s=", ",", "s=",
misc:encode_base64(Salt), base64:encode(Salt),
",", "i=", ",", "i=",
integer_to_list(IterationCount)]), integer_to_list(IterationCount)]),
{continue, ServerFirstMessage, {continue, ServerFirstMessage,
@ -165,7 +165,9 @@ mech_step(#state{step = 4} = State, ClientIn) ->
ClientProofAttribute] -> ClientProofAttribute] ->
case parse_attribute(GS2ChannelBindingAttribute) of case parse_attribute(GS2ChannelBindingAttribute) of
{$c, CVal} -> {$c, CVal} ->
ChannelBindingSupport = binary:at(misc:decode_base64(CVal), 0), ChannelBindingSupport = try binary:first(base64:decode(CVal))
catch _:badarg -> 0
end,
if (ChannelBindingSupport == $n) if (ChannelBindingSupport == $n)
or (ChannelBindingSupport == $y) -> or (ChannelBindingSupport == $y) ->
Nonce = <<(State#state.client_nonce)/binary, Nonce = <<(State#state.client_nonce)/binary,
@ -174,7 +176,9 @@ mech_step(#state{step = 4} = State, ClientIn) ->
{$r, CompareNonce} when CompareNonce == Nonce -> {$r, CompareNonce} when CompareNonce == Nonce ->
case parse_attribute(ClientProofAttribute) of case parse_attribute(ClientProofAttribute) of
{$p, ClientProofB64} -> {$p, ClientProofB64} ->
ClientProof = misc:decode_base64(ClientProofB64), ClientProof = try base64:decode(ClientProofB64)
catch _:badarg -> <<>>
end,
AuthMessage = iolist_to_binary( AuthMessage = iolist_to_binary(
[State#state.auth_message, [State#state.auth_message,
",", ",",
@ -195,7 +199,7 @@ mech_step(#state{step = 4} = State, ClientIn) ->
{auth_module, State#state.auth_module}, {auth_module, State#state.auth_module},
{authzid, State#state.username}], {authzid, State#state.username}],
<<"v=", <<"v=",
(misc:encode_base64(ServerSignature))/binary>>}; (base64:encode(ServerSignature))/binary>>};
true -> {error, not_authorized, State#state.username} true -> {error, not_authorized, State#state.username}
end; end;
_ -> {error, bad_attribute} _ -> {error, bad_attribute}

View File

@ -645,10 +645,10 @@ is_password_scram_valid(Password, Scram) ->
false; false;
_ -> _ ->
IterationCount = Scram#scram.iterationcount, IterationCount = Scram#scram.iterationcount,
Salt = misc:decode_base64(Scram#scram.salt), Salt = base64:decode(Scram#scram.salt),
SaltedPassword = scram:salted_password(Password, Salt, IterationCount), SaltedPassword = scram:salted_password(Password, Salt, IterationCount),
StoredKey = scram:stored_key(scram:client_key(SaltedPassword)), StoredKey = scram:stored_key(scram:client_key(SaltedPassword)),
misc:decode_base64(Scram#scram.storedkey) == StoredKey base64:decode(Scram#scram.storedkey) == StoredKey
end. end.
password_to_scram(Password) -> password_to_scram(Password) ->
@ -661,9 +661,9 @@ password_to_scram(Password, IterationCount) ->
SaltedPassword = scram:salted_password(Password, Salt, IterationCount), SaltedPassword = scram:salted_password(Password, Salt, IterationCount),
StoredKey = scram:stored_key(scram:client_key(SaltedPassword)), StoredKey = scram:stored_key(scram:client_key(SaltedPassword)),
ServerKey = scram:server_key(SaltedPassword), ServerKey = scram:server_key(SaltedPassword),
#scram{storedkey = misc:encode_base64(StoredKey), #scram{storedkey = base64:encode(StoredKey),
serverkey = misc:encode_base64(ServerKey), serverkey = base64:encode(ServerKey),
salt = misc:encode_base64(Salt), salt = base64:encode(Salt),
iterationcount = IterationCount}. iterationcount = IterationCount}.
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
@ -744,7 +744,7 @@ auth_modules(Server) ->
match_passwords(Password, #scram{} = Scram, <<"">>, undefined) -> match_passwords(Password, #scram{} = Scram, <<"">>, undefined) ->
is_password_scram_valid(Password, Scram); is_password_scram_valid(Password, Scram);
match_passwords(Password, #scram{} = Scram, Digest, DigestFun) -> match_passwords(Password, #scram{} = Scram, Digest, DigestFun) ->
StoredKey = misc:decode_base64(Scram#scram.storedkey), StoredKey = base64:decode(Scram#scram.storedkey),
DigRes = if Digest /= <<"">> -> DigRes = if Digest /= <<"">> ->
Digest == DigestFun(StoredKey); Digest == DigestFun(StoredKey);
true -> false true -> false

View File

@ -770,7 +770,9 @@ code_to_phrase(505) -> <<"HTTP Version Not Supported">>.
-spec parse_auth(binary()) -> {binary(), binary()} | {oauth, binary(), []} | undefined. -spec parse_auth(binary()) -> {binary(), binary()} | {oauth, binary(), []} | undefined.
parse_auth(<<"Basic ", Auth64/binary>>) -> parse_auth(<<"Basic ", Auth64/binary>>) ->
Auth = misc:decode_base64(Auth64), Auth = try base64:decode(Auth64)
catch _:badarg -> <<>>
end,
%% Auth should be a string with the format: user@server:password %% Auth should be a string with the format: user@server:password
%% Note that password can contain additional characters '@' and ':' %% Note that password can contain additional characters '@' and ':'
case str:chr(Auth, $:) of case str:chr(Auth, $:) of

File diff suppressed because one or more lines are too long

View File

@ -152,7 +152,7 @@ handshake(#ws{headers = Headers} = State) ->
V -> V ->
[<<"Sec-Websocket-Protocol:">>, V, <<"\r\n">>] [<<"Sec-Websocket-Protocol:">>, V, <<"\r\n">>]
end, end,
Hash = misc:encode_base64( Hash = base64:encode(
crypto:hash(sha, <<Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11">>)), crypto:hash(sha, <<Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11">>)),
{State, [<<"HTTP/1.1 101 Switching Protocols\r\n">>, {State, [<<"HTTP/1.1 101 Switching Protocols\r\n">>,
<<"Upgrade: websocket\r\n">>, <<"Upgrade: websocket\r\n">>,

View File

@ -28,14 +28,18 @@
-module(misc). -module(misc).
%% API %% API
-export([tolower/1, term_to_base64/1, base64_to_term/1, -export([tolower/1, term_to_base64/1, base64_to_term/1, ip_to_list/1,
decode_base64/1, encode_base64/1, ip_to_list/1,
hex_to_bin/1, hex_to_base64/1, expand_keyword/3, hex_to_bin/1, hex_to_base64/1, expand_keyword/3,
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1, atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1, l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1,
encode_pid/1, decode_pid/2, compile_exprs/2, join_atoms/2, encode_pid/1, decode_pid/2, compile_exprs/2, join_atoms/2,
try_read_file/1]). try_read_file/1]).
%% Deprecated functions
-export([decode_base64/1, encode_base64/1]).
-deprecated([{decode_base64, 1},
{encode_base64, 1}]).
-include("logger.hrl"). -include("logger.hrl").
-include_lib("kernel/include/file.hrl"). -include_lib("kernel/include/file.hrl").
@ -58,83 +62,21 @@ term_to_base64(Term) ->
-spec base64_to_term(binary()) -> {term, term()} | error. -spec base64_to_term(binary()) -> {term, term()} | error.
base64_to_term(Base64) -> base64_to_term(Base64) ->
case catch binary_to_term(decode_base64(Base64), [safe]) of try binary_to_term(base64:decode(Base64), [safe]) of
{'EXIT', _} -> Term -> {term, Term}
error; catch _:badarg ->
Term -> error
{term, Term}
end. end.
-spec decode_base64(binary()) -> binary(). -spec decode_base64(binary()) -> binary().
decode_base64(S) -> decode_base64(S) ->
case catch binary:last(S) of try base64:mime_decode(S)
C when C == $\n; C == $\s -> catch _:badarg -> <<>>
decode_base64(binary:part(S, 0, byte_size(S) - 1));
_ ->
decode_base64_bin(S, <<>>)
end. end.
take_without_spaces(Bin, Count) ->
take_without_spaces(Bin, Count, <<>>).
take_without_spaces(Bin, 0, Acc) ->
{Acc, Bin};
take_without_spaces(<<>>, _, Acc) ->
{Acc, <<>>};
take_without_spaces(<<$\s, Tail/binary>>, Count, Acc) ->
take_without_spaces(Tail, Count, Acc);
take_without_spaces(<<$\t, Tail/binary>>, Count, Acc) ->
take_without_spaces(Tail, Count, Acc);
take_without_spaces(<<$\n, Tail/binary>>, Count, Acc) ->
take_without_spaces(Tail, Count, Acc);
take_without_spaces(<<$\r, Tail/binary>>, Count, Acc) ->
take_without_spaces(Tail, Count, Acc);
take_without_spaces(<<Char:8, Tail/binary>>, Count, Acc) ->
take_without_spaces(Tail, Count-1, <<Acc/binary, Char:8>>).
decode_base64_bin(<<>>, Acc) ->
Acc;
decode_base64_bin(Bin, Acc) ->
case take_without_spaces(Bin, 4) of
{<<A, B, $=, $=>>, _} ->
<<Acc/binary, (d(A)):6, (d(B) bsr 4):2>>;
{<<A, B, C, $=>>, _} ->
<<Acc/binary, (d(A)):6, (d(B)):6, (d(C) bsr 2):4>>;
{<<A, B, C, D>>, Tail} ->
Acc2 = <<Acc/binary, (d(A)):6, (d(B)):6, (d(C)):6, (d(D)):6>>,
decode_base64_bin(Tail, Acc2);
_ ->
<<"">>
end.
d(X) when X >= $A, X =< $Z -> X - 65;
d(X) when X >= $a, X =< $z -> X - 71;
d(X) when X >= $0, X =< $9 -> X + 4;
d($+) -> 62;
d($/) -> 63;
d(_) -> 63.
%% Convert Erlang inet IP to list
-spec encode_base64(binary()) -> binary(). -spec encode_base64(binary()) -> binary().
encode_base64(Data) -> encode_base64(Data) ->
encode_base64_bin(Data, <<>>). base64:encode(Data).
encode_base64_bin(<<A:6, B:6, C:6, D:6, Tail/binary>>, Acc) ->
encode_base64_bin(Tail, <<Acc/binary, (e(A)):8, (e(B)):8, (e(C)):8, (e(D)):8>>);
encode_base64_bin(<<A:6, B:6, C:4>>, Acc) ->
<<Acc/binary, (e(A)):8, (e(B)):8, (e(C bsl 2)):8, $=>>;
encode_base64_bin(<<A:6, B:2>>, Acc) ->
<<Acc/binary, (e(A)):8, (e(B bsl 4)):8, $=, $=>>;
encode_base64_bin(<<>>, Acc) ->
Acc.
e(X) when X >= 0, X < 26 -> X + 65;
e(X) when X > 25, X < 52 -> X + 71;
e(X) when X > 51, X < 62 -> X - 4;
e(62) -> $+;
e(63) -> $/;
e(X) -> exit({bad_encode_base64_token, X}).
-spec ip_to_list(inet:ip_address() | undefined | -spec ip_to_list(inet:ip_address() | undefined |
{inet:ip_address(), inet:port_number()}) -> binary(). {inet:ip_address(), inet:port_number()}) -> binary().
@ -160,7 +102,7 @@ hex_to_bin([H1, H2 | T], Acc) ->
-spec hex_to_base64(binary()) -> binary(). -spec hex_to_base64(binary()) -> binary().
hex_to_base64(Hex) -> hex_to_base64(Hex) ->
encode_base64(hex_to_bin(Hex)). base64:encode(hex_to_bin(Hex)).
-spec expand_keyword(binary(), binary(), binary()) -> binary(). -spec expand_keyword(binary(), binary(), binary()) -> binary().
expand_keyword(Keyword, Input, Replacement) -> expand_keyword(Keyword, Input, Replacement) ->

View File

@ -413,7 +413,7 @@ make_my_disco_hash(Host) ->
make_disco_hash(DiscoInfo, Algo) -> make_disco_hash(DiscoInfo, Algo) ->
Concat = list_to_binary([concat_identities(DiscoInfo), Concat = list_to_binary([concat_identities(DiscoInfo),
concat_features(DiscoInfo), concat_info(DiscoInfo)]), concat_features(DiscoInfo), concat_info(DiscoInfo)]),
misc:encode_base64(case Algo of base64:encode(case Algo of
md5 -> erlang:md5(Concat); md5 -> erlang:md5(Concat);
sha -> crypto:hash(sha, Concat); sha -> crypto:hash(sha, Concat);
sha224 -> crypto:hash(sha224, Concat); sha224 -> crypto:hash(sha224, Concat);

View File

@ -500,7 +500,7 @@ make_dir_rec(Dir) ->
%% {ok, F1}=file:open("valid-xhtml10.png", [read]). %% {ok, F1}=file:open("valid-xhtml10.png", [read]).
%% {ok, F1b}=file:read(F1, 1000000). %% {ok, F1b}=file:read(F1, 1000000).
%% c("../../ejabberd/src/jlib.erl"). %% c("../../ejabberd/src/jlib.erl").
%% misc:encode_base64(F1b). %% base64:encode(F1b).
image_base64(<<"powered-by-erlang.png">>) -> image_base64(<<"powered-by-erlang.png">>) ->
<<"iVBORw0KGgoAAAANSUhEUgAAAGUAAAAfCAYAAAD+xQNoA" <<"iVBORw0KGgoAAAANSUhEUgAAAGUAAAAfCAYAAAD+xQNoA"
@ -676,7 +676,7 @@ create_image_files(Images_dir) ->
lists:foreach(fun (Filename) -> lists:foreach(fun (Filename) ->
Filename_full = fjoin([Images_dir, Filename]), Filename_full = fjoin([Images_dir, Filename]),
{ok, F} = file:open(Filename_full, [write]), {ok, F} = file:open(Filename_full, [write]),
Image = misc:decode_base64(image_base64(Filename)), Image = base64:decode(image_base64(Filename)),
io:format(F, <<"~s">>, [Image]), io:format(F, <<"~s">>, [Image]),
file:close(F) file:close(F)
end, end,

View File

@ -1152,13 +1152,13 @@ handle_iq_vcard(ToJID, NewId, #iq{type = Type, sub_els = SubEls} = IQ) ->
-spec stanzaid_pack(binary(), binary()) -> binary(). -spec stanzaid_pack(binary(), binary()) -> binary().
stanzaid_pack(OriginalId, Resource) -> stanzaid_pack(OriginalId, Resource) ->
<<"berd", <<"berd",
(misc:encode_base64(<<"ejab\000", (base64:encode(<<"ejab\000",
OriginalId/binary, "\000", OriginalId/binary, "\000",
Resource/binary>>))/binary>>. Resource/binary>>))/binary>>.
-spec stanzaid_unpack(binary()) -> {binary(), binary()}. -spec stanzaid_unpack(binary()) -> {binary(), binary()}.
stanzaid_unpack(<<"berd", StanzaIdBase64/binary>>) -> stanzaid_unpack(<<"berd", StanzaIdBase64/binary>>) ->
StanzaId = misc:decode_base64(StanzaIdBase64), StanzaId = base64:decode(StanzaIdBase64),
[<<"ejab">>, OriginalId, Resource] = [<<"ejab">>, OriginalId, Resource] =
str:tokens(StanzaId, <<"\000">>), str:tokens(StanzaId, <<"\000">>),
{OriginalId, Resource}. {OriginalId, Resource}.

View File

@ -156,7 +156,9 @@ compute_hash(VCard) ->
<<>> -> <<>> ->
<<>>; <<>>;
BinVal -> BinVal ->
str:sha(misc:decode_base64(BinVal)) try str:sha(base64:decode(BinVal))
catch _:badarg -> <<>>
end
end. end.
%%==================================================================== %%====================================================================