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

remove used function from sha.erl module #336

Merged
merged 7 commits into from
Dec 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 0 additions & 121 deletions apps/ejabberd/c_src/sha_drv.c

This file was deleted.

2 changes: 1 addition & 1 deletion apps/ejabberd/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

{cover_enabled, true}.
{cover_print_enabled, true}.
{cover_export_enabled, true}.
{cover_export_enabled, true}.
1 change: 0 additions & 1 deletion apps/ejabberd/src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ start(normal, _Args) ->
ejabberd_loglevel:set(4),
write_pid_file(),
db_init(),
sha:start(),
xml:start(),
application:start(p1_cache_tab),

Expand Down
5 changes: 2 additions & 3 deletions apps/ejabberd/src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,8 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
StateData#state.access, JID) == allow) of
true ->
DGen = fun(PW) ->
list_to_binary(sha:sha(
StateData#state.streamid
++ binary_to_list(PW))) end,
sha:sha1_hex(StateData#state.streamid
++ binary_to_list(PW)) end,
case ejabberd_auth:check_password_with_authmodule(
U, StateData#state.server, P, D, DGen) of
{true, AuthModule} ->
Expand Down
4 changes: 2 additions & 2 deletions apps/ejabberd/src/ejabberd_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ wait_for_handshake({xmlstreamelement, El}, StateData) ->
#xmlel{name = Name, children = Els} = El,
case {Name, xml:get_cdata(Els)} of
{<<"handshake">>, Digest} ->
case list_to_binary(sha:sha(StateData#state.streamid ++
StateData#state.password)) of
case sha:sha1_hex(StateData#state.streamid ++
StateData#state.password) of
Digest ->
send_text(StateData, <<"<handshake/>">>),
lists:foreach(
Expand Down
2 changes: 1 addition & 1 deletion apps/ejabberd/src/mod_bosh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ start_session(Peer, Body) ->
-spec make_sid() -> binary().
make_sid() ->
%% TODO: now() skews system timer and ref() is unique enough as is
list_to_binary(sha:sha(term_to_binary({now(), make_ref()}))).
sha:sha1_hex(term_to_binary({now(), make_ref()})).

%%--------------------------------------------------------------------
%% HTTP errors
Expand Down
2 changes: 1 addition & 1 deletion apps/ejabberd/src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ xfield(Type, Label, Var, Val, Lang) ->
%% http://xmpp.org/extensions/xep-0045.html#createroom-unique
-spec iq_get_unique(ejabberd:jid()) -> jlib:xmlcdata().
iq_get_unique(From) ->
#xmlcdata{content = sha:sha(term_to_binary([From, now(), randoms:get_string()]))}.
#xmlcdata{content = sha:sha1_hex(term_to_binary([From, now(), randoms:get_string()]))}.


-spec iq_get_register_info('undefined' | ejabberd:server(),
Expand Down
10 changes: 5 additions & 5 deletions apps/ejabberd/src/mod_roster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ process_local_iq(From, To, #iq{type = Type} = IQ) ->
end.

roster_hash(Items) ->
list_to_binary(sha:sha(term_to_binary(
sha:sha1_hex(term_to_binary(
lists:sort(
[R#roster{groups = lists:sort(Grs)} ||
R = #roster{groups = Grs} <- Items])))).
R = #roster{groups = Grs} <- Items]))).

roster_versioning_enabled(Host) ->
gen_mod:get_module_opt(Host, ?MODULE, versioning, false).
Expand Down Expand Up @@ -199,7 +199,7 @@ process_iq_get(From, To, #iq{sub_el = SubEl} = IQ) ->
{lists:map(fun item_to_xml/1,
ejabberd_hooks:run_fold(roster_get, To#jid.lserver, [], [US])), NewVersion};
[] ->
RosterVersion = sha:sha(term_to_binary(now())),
RosterVersion = sha:sha1_hex(term_to_binary(now())),
mnesia:dirty_write(#roster_version{us = US, version = RosterVersion}),
{lists:map(fun item_to_xml/1,
ejabberd_hooks:run_fold(roster_get, To#jid.lserver, [], [US])), RosterVersion}
Expand Down Expand Up @@ -324,7 +324,7 @@ process_item_set(From, To, #xmlel{attrs = Attrs, children = Els}) ->
Item3 = ejabberd_hooks:run_fold(roster_process_item,
LServer, Item2, [LServer]),
case roster_version_on_db(LServer) of
true -> mnesia:write(#roster_version{us = {LUser, LServer}, version = sha:sha(term_to_binary(now()))});
true -> mnesia:write(#roster_version{us = {LUser, LServer}, version = sha:sha1_hex(term_to_binary(now()))});
false -> ok
end,
{Item, Item3}
Expand Down Expand Up @@ -550,7 +550,7 @@ process_subscription(Direction, User, Server, JID1, Type, Reason) ->
askmessage = AskMessage},
mnesia:write(NewItem),
case roster_version_on_db(LServer) of
true -> mnesia:write(#roster_version{us = {LUser, LServer}, version = sha:sha(term_to_binary(now()))});
true -> mnesia:write(#roster_version{us = {LUser, LServer}, version = sha:sha1_hex(term_to_binary(now()))});
false -> ok
end,
{{push, NewItem}, AutoReply}
Expand Down
10 changes: 5 additions & 5 deletions apps/ejabberd/src/mod_roster_odbc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ process_local_iq(From, To, #iq{type = Type} = IQ) ->
end.

roster_hash(Items) ->
list_to_binary(sha:sha(term_to_binary(
sha:sha1_hex(term_to_binary(
lists:sort(
[R#roster{groups = lists:sort(Grs)} ||
R = #roster{groups = Grs} <- Items])))).
R = #roster{groups = Grs} <- Items]))).

roster_versioning_enabled(Host) ->
gen_mod:get_module_opt(Host, ?MODULE, versioning, false).
Expand Down Expand Up @@ -190,7 +190,7 @@ process_iq_get(From, To, #iq{sub_el = SubEl} = IQ) ->
{lists:map(fun item_to_xml/1,
ejabberd_hooks:run_fold(roster_get, To#jid.lserver, [], [US])), NewVersion};
{selected, [<<"version">>], []} ->
RosterVersion = sha:sha(term_to_binary(now())),
RosterVersion = sha:sha1_hex(term_to_binary(now())),
{atomic, {updated,1}} = odbc_queries:sql_transaction(LServer,
fun() ->
odbc_queries:set_roster_version(ejabberd_odbc:escape(LUser), RosterVersion)
Expand Down Expand Up @@ -364,7 +364,7 @@ process_item_set(From, To, #xmlel{attrs = Attrs, children = Els}) ->
Item3 = ejabberd_hooks:run_fold(roster_process_item,
LServer, Item2, [LServer]),
case roster_version_on_db(LServer) of
true -> odbc_queries:set_roster_version(ejabberd_odbc:escape(LUser), sha:sha(term_to_binary(now())));
true -> odbc_queries:set_roster_version(ejabberd_odbc:escape(LUser), sha:sha1_hex(term_to_binary(now())));
false -> ok
end,
{Item, Item3}
Expand Down Expand Up @@ -591,7 +591,7 @@ process_subscription(Direction, User, Server, JID1, Type, Reason) ->
ItemVals = record_to_string(NewItem),
odbc_queries:roster_subscribe(LServer, Username, SJID, ItemVals),
case roster_version_on_db(LServer) of
true -> odbc_queries:set_roster_version(ejabberd_odbc:escape(LUser), sha:sha(term_to_binary(now())));
true -> odbc_queries:set_roster_version(ejabberd_odbc:escape(LUser), sha:sha1_hex(term_to_binary(now())));
false -> ok
end,
{{push, NewItem}, AutoReply}
Expand Down
73 changes: 3 additions & 70 deletions apps/ejabberd/src/sha.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,75 +25,8 @@
%%%----------------------------------------------------------------------

-module(sha).
-author('alexey@process-one.net').

-export([start/0, sha/1, sha1/1, sha224/1, sha256/1, sha384/1,
sha512/1]).

-ifdef(HAVE_MD2).
-export([md2/1]).
-endif.

-export([sha1_hex/1]).
-include("ejabberd.hrl").

-define(DRIVER, sha_drv).

start() ->
Res = case erl_ddll:load_driver(ejabberd:get_so_path(), ?DRIVER) of
ok ->
ok;
{error, already_loaded} ->
ok;
Err ->
Err
end,
case Res of
ok ->
Port = open_port({spawn, ?DRIVER}, [binary]),
register(?DRIVER, Port);
{error, Reason} ->
?CRITICAL_MSG("unable to load driver '~s': ~s",
[driver_path(), erl_ddll:format_error(Reason)])
end.

digit_to_xchar(D) when (D >= 0) and (D < 10) ->
D + 48;
digit_to_xchar(D) ->
D + 87.

sha(Text) ->
Bin = crypto:hash(sha, Text),
lists:reverse(ints_to_rxstr(binary_to_list(Bin), [])).

ints_to_rxstr([], Res) ->
Res;
ints_to_rxstr([N | Ns], Res) ->
ints_to_rxstr(Ns, [digit_to_xchar(N rem 16),
digit_to_xchar(N div 16) | Res]).

sha1(Text) ->
crypto:hash(sha, Text).

sha224(Text) ->
erlang:port_control(?DRIVER, 224, Text).

sha256(Text) ->
erlang:port_control(?DRIVER, 256, Text).

sha384(Text) ->
erlang:port_control(?DRIVER, 384, Text).

sha512(Text) ->
erlang:port_control(?DRIVER, 512, Text).

-ifdef(HAVE_MD2).
md2(Text) ->
erlang:port_control(?DRIVER, 2, Text).
-endif.

driver_path() ->
Suffix = case os:type() of
{win32, _} -> ".dll";
_ -> ".so"
end,
filename:join(ejabberd:get_so_path(), atom_to_list(?DRIVER) ++ Suffix).
-spec sha1_hex(binary()) -> binary().
sha1_hex(Text) -> base16:encode(crypto:hash(sha, Text)).
23 changes: 23 additions & 0 deletions apps/ejabberd/test/sha_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-module(sha_SUITE).
-compile([export_all]).

-include_lib("proper/include/proper.hrl").
-import(prop_helper, [prop/2]).

all() ->
[ sanity_check,
always_produces_well_formed_output
].

sanity_check(_) ->
%% @doc vis: echo -n "Foo" | sha1sum
<<"201a6b3053cc1422d2c3670b62616221d2290929">> = sha:sha1_hex(<<"Foo">>).

always_produces_well_formed_output(_) ->
prop(always_produces_well_formed_output,
?FORALL(BinaryBlob, binary(),
true == is_well_formed(sha:sha1_hex(BinaryBlob)))).

is_well_formed(Binary) ->
40 == size(Binary) andalso
nomatch == re:run(Binary, "[^0-9a-f]").
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

{deps,
[
{base16, ".*", {git, "git://github.com/goj/base16.git", "1e9dd28bdbcfb"}},
{cuesport, ".*", {git, "git://github.com/goj/cuesport.git", "3b16d99"}},
{redo, ".*", {git, "git://github.com/JacobVorreuter/redo.git", "7c7eaef"}},
{exml, "2.1.5", {git, "git://github.com/esl/exml.git", "2.1.5"}},
Expand Down
1 change: 1 addition & 0 deletions rel/reltool.config
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
{excl_archive_filters, [".*"]}, % do no archive built libs
{excl_sys_filters, ["^bin/.*",
"^erts.*/bin/(dialyzer|typer)"]},
{app, base16, [{incl_cond, include}]},
{app, compiler, [{incl_cond, include}]},
{app, crypto, [{incl_cond, include}]},
{app, goldrush, [{incl_cond, include}]},
Expand Down