From 069d28b1eda5384a67fa5fb2ac14b7cdc86eec31 Mon Sep 17 00:00:00 2001 From: Alexey Shchepin Date: Tue, 14 Mar 2017 02:31:51 +0300 Subject: [PATCH] Get rid of p1_sha calls --- src/cyrsasl_digest.erl | 2 +- src/ejabberd_bosh.erl | 4 ++-- src/ejabberd_captcha.erl | 4 ++-- src/ejabberd_config.erl | 4 ++-- src/ejabberd_service.erl | 4 ++-- src/mod_http_upload.erl | 2 +- src/mod_legacy_auth.erl | 2 +- src/mod_mix.erl | 4 ++-- src/mod_muc.erl | 2 +- src/mod_proxy65_service.erl | 2 +- src/mod_roster.erl | 4 ++-- src/mod_s2s_dialback.erl | 4 ++-- src/mod_sip_proxy.erl | 2 +- src/mod_vcard_xupdate.erl | 2 +- src/str.erl | 21 ++++++++++++++++++++- src/xmpp_stream_in.erl | 2 +- 16 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/cyrsasl_digest.erl b/src/cyrsasl_digest.erl index a91566b66..eedb5366c 100644 --- a/src/cyrsasl_digest.erl +++ b/src/cyrsasl_digest.erl @@ -237,7 +237,7 @@ get_local_fqdn2() -> end. hex(S) -> - p1_sha:to_hexlist(S). + str:to_hexlist(S). proplists_get_bin_value(Key, Pairs, Default) -> case proplists:get_value(Key, Pairs, Default) of diff --git a/src/ejabberd_bosh.erl b/src/ejabberd_bosh.erl index 204c7b6e5..ad1dcfd5f 100644 --- a/src/ejabberd_bosh.erl +++ b/src/ejabberd_bosh.erl @@ -773,7 +773,7 @@ bounce_els_from_obuf(State) -> is_valid_key(<<"">>, <<"">>) -> true; is_valid_key(PrevKey, Key) -> - p1_sha:sha(Key) == PrevKey. + str:sha(Key) == PrevKey. is_overactivity(undefined) -> false; is_overactivity(PrevPoll) -> @@ -999,7 +999,7 @@ http_error(Status, Reason, Type) -> end, {Status, Reason, ?HEADER(CType), <<"">>}. -make_sid() -> p1_sha:sha(randoms:get_string()). +make_sid() -> str:sha(randoms:get_string()). -compile({no_auto_import, [{min, 2}]}). diff --git a/src/ejabberd_captcha.erl b/src/ejabberd_captcha.erl index 9d0835bf2..0a1e1a9eb 100644 --- a/src/ejabberd_captcha.erl +++ b/src/ejabberd_captcha.erl @@ -90,7 +90,7 @@ create_captcha(SID, From, To, Lang, Limiter, Args) -> {ok, Type, Key, Image} -> Id = <<(randoms:get_string())/binary>>, JID = jid:encode(From), - CID = <<"sha1+", (p1_sha:sha(Image))/binary, "@bob.xmpp.org">>, + CID = <<"sha1+", (str:sha(Image))/binary, "@bob.xmpp.org">>, Data = #bob_data{cid = CID, 'max-age' = 0, type = Type, data = Image}, Fs = [mk_field(hidden, <<"FORM_TYPE">>, ?NS_CAPTCHA), @@ -120,7 +120,7 @@ create_captcha_x(SID, To, Lang, Limiter, #xdata{fields = Fs} = X) -> case create_image(Limiter) of {ok, Type, Key, Image} -> Id = <<(randoms:get_string())/binary>>, - CID = <<"sha1+", (p1_sha:sha(Image))/binary, "@bob.xmpp.org">>, + CID = <<"sha1+", (str:sha(Image))/binary, "@bob.xmpp.org">>, Data = #bob_data{cid = CID, 'max-age' = 0, type = Type, data = Image}, HelpTxt = translate:translate(Lang, <<"If you don't see the CAPTCHA image here, " diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index 720e4cafa..74c180728 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -71,9 +71,9 @@ start() -> UnixTime = p1_time_compat:system_time(seconds), SharedKey = case erlang:get_cookie() of nocookie -> - p1_sha:sha(randoms:get_string()); + str:sha(randoms:get_string()); Cookie -> - p1_sha:sha(jlib:atom_to_binary(Cookie)) + str:sha(jlib:atom_to_binary(Cookie)) end, State2 = set_option({node_start, global}, UnixTime, State1), State3 = set_option({shared_key, global}, SharedKey, State2), diff --git a/src/ejabberd_service.erl b/src/ejabberd_service.erl index 16cdf5ca9..716606b4c 100644 --- a/src/ejabberd_service.erl +++ b/src/ejabberd_service.erl @@ -78,13 +78,13 @@ init([State, Opts]) -> fun({H, Os}, D) -> P = proplists:get_value( password, Os, - p1_sha:sha(randoms:bytes(20))), + str:sha(randoms:bytes(20))), dict:store(H, P, D) end, dict:new(), HOpts); false -> Pass = proplists:get_value( password, Opts, - p1_sha:sha(randoms:bytes(20))), + str:sha(randoms:bytes(20))), dict:from_list([{global, Pass}]) end, CheckFrom = gen_mod:get_opt(check_from, Opts, diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index 9fdd34381..59570b0b5 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -669,7 +669,7 @@ mk_slot(PutURL, GetURL, XMLNS) -> -spec make_user_string(jid(), sha1 | node) -> binary(). make_user_string(#jid{luser = U, lserver = S}, sha1) -> - p1_sha:sha(<>); + str:sha(<>); make_user_string(#jid{luser = U}, node) -> re:replace(U, <<"[^a-zA-Z0-9_.-]">>, <<$_>>, [global, {return, binary}]). diff --git a/src/mod_legacy_auth.erl b/src/mod_legacy_auth.erl index 49e210ec6..5a4ff9108 100644 --- a/src/mod_legacy_auth.erl +++ b/src/mod_legacy_auth.erl @@ -111,7 +111,7 @@ authenticate(#{stream_id := StreamID, server := Server, resource = R}]} = IQ) -> P = if is_binary(P0) -> P0; true -> <<>> end, D = if is_binary(D0) -> D0; true -> <<>> end, - DGen = fun (PW) -> p1_sha:sha(<>) end, + DGen = fun (PW) -> str:sha(<>) end, JID = jid:make(U, Server, R), case JID /= error andalso acl:access_matches(Access, diff --git a/src/mod_mix.erl b/src/mod_mix.erl index 940e9898f..714a6b020 100644 --- a/src/mod_mix.erl +++ b/src/mod_mix.erl @@ -261,7 +261,7 @@ publish_participant(From, To) -> LFrom = jid:tolower(BareFrom), LTo = jid:tolower(jid:remove_resource(To)), Participant = #mix_participant{jid = BareFrom}, - ItemID = p1_sha:sha(jid:encode(LFrom)), + ItemID = str:sha(jid:encode(LFrom)), mod_pubsub:publish_item( LTo, To#jid.lserver, ?NS_MIX_NODES_PARTICIPANTS, From, ItemID, [xmpp:encode(Participant)]). @@ -284,7 +284,7 @@ delete_presence(From, To) -> delete_participant(From, To) -> LFrom = jid:tolower(jid:remove_resource(From)), - ItemID = p1_sha:sha(jid:encode(LFrom)), + ItemID = str:sha(jid:encode(LFrom)), delete_presence(From, To), delete_item(From, To, ?NS_MIX_NODES_PARTICIPANTS, ItemID). diff --git a/src/mod_muc.erl b/src/mod_muc.erl index 563f4c68a..f9488cf9a 100644 --- a/src/mod_muc.erl +++ b/src/mod_muc.erl @@ -609,7 +609,7 @@ process_muc_unique(#iq{type = set, lang = Lang} = IQ) -> xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang)); process_muc_unique(#iq{from = From, type = get, sub_els = [#muc_unique{}]} = IQ) -> - Name = p1_sha:sha(term_to_binary([From, p1_time_compat:timestamp(), + Name = str:sha(term_to_binary([From, p1_time_compat:timestamp(), randoms:get_string()])), xmpp:make_iq_result(IQ, #muc_unique{name = Name}). diff --git a/src/mod_proxy65_service.erl b/src/mod_proxy65_service.erl index 58aee60ca..dd58b4a3a 100644 --- a/src/mod_proxy65_service.erl +++ b/src/mod_proxy65_service.erl @@ -217,7 +217,7 @@ process_bytestreams(#iq{type = set, lang = Lang, from = InitiatorJID, to = To, Node = ejabberd_cluster:get_node_by_id(To#jid.lresource), Target = jid:encode(jid:tolower(TargetJID)), Initiator = jid:encode(jid:tolower(InitiatorJID)), - SHA1 = p1_sha:sha(<>), + SHA1 = str:sha(<>), Mod = gen_mod:ram_db_mod(global, mod_proxy65), MaxConnections = max_connections(ServerHost), case Mod:activate_stream(SHA1, Initiator, MaxConnections, Node) of diff --git a/src/mod_roster.erl b/src/mod_roster.erl index 5f499ec74..cf281528a 100644 --- a/src/mod_roster.erl +++ b/src/mod_roster.erl @@ -216,7 +216,7 @@ process_local_iq(#iq{lang = Lang} = IQ) -> xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)). roster_hash(Items) -> - p1_sha:sha(term_to_binary(lists:sort([R#roster{groups = + str:sha(term_to_binary(lists:sort([R#roster{groups = lists:sort(Grs)} || R = #roster{groups = Grs} <- Items]))). @@ -270,7 +270,7 @@ write_roster_version_t(LUser, LServer) -> write_roster_version(LUser, LServer, true). write_roster_version(LUser, LServer, InTransaction) -> - Ver = p1_sha:sha(term_to_binary(p1_time_compat:unique_integer())), + Ver = str:sha(term_to_binary(p1_time_compat:unique_integer())), Mod = gen_mod:db_mod(LServer, ?MODULE), Mod:write_roster_version(LUser, LServer, InTransaction, Ver), Ver. diff --git a/src/mod_s2s_dialback.erl b/src/mod_s2s_dialback.erl index 4d9ae1af2..f45728485 100644 --- a/src/mod_s2s_dialback.erl +++ b/src/mod_s2s_dialback.erl @@ -266,8 +266,8 @@ s2s_out_packet(State, _) -> -spec make_key(binary(), binary(), binary()) -> binary(). make_key(From, To, StreamID) -> Secret = ejabberd_config:get_option(shared_key, fun(V) -> V end), - p1_sha:to_hexlist( - crypto:hmac(sha256, p1_sha:to_hexlist(crypto:hash(sha256, Secret)), + str:to_hexlist( + crypto:hmac(sha256, str:to_hexlist(crypto:hash(sha256, Secret)), [To, " ", From, " ", StreamID])). -spec send_verify_request(ejabberd_s2s_out:state()) -> ejabberd_s2s_out:state(). diff --git a/src/mod_sip_proxy.erl b/src/mod_sip_proxy.erl index 08c90c23d..b57e431d6 100644 --- a/src/mod_sip_proxy.erl +++ b/src/mod_sip_proxy.erl @@ -332,7 +332,7 @@ make_sign(TS, Hdrs) -> FromTag = esip:get_param(<<"tag">>, FParams), CallID = esip:get_hdr('call-id', Hdrs), SharedKey = ejabberd_config:get_option(shared_key, fun(V) -> V end), - p1_sha:sha([SharedKey, LFUser, LFServer, LTUser, LTServer, + str:sha([SharedKey, LFUser, LFServer, LTUser, LTServer, FromTag, CallID, TS]). is_signed_by_me(TS_Sign, Hdrs) -> diff --git a/src/mod_vcard_xupdate.erl b/src/mod_vcard_xupdate.erl index 2a754d408..4c9c0b71a 100644 --- a/src/mod_vcard_xupdate.erl +++ b/src/mod_vcard_xupdate.erl @@ -99,7 +99,7 @@ vcard_set(LUser, LServer, VCARD) -> <<>> -> remove_xupdate(LUser, LServer); BinVal -> add_xupdate(LUser, LServer, - p1_sha:sha(jlib:decode_base64(BinVal))) + str:sha(jlib:decode_base64(BinVal))) end, ejabberd_sm:force_update_presence(US). diff --git a/src/str.erl b/src/str.erl index 28537da0f..0b30be333 100644 --- a/src/str.erl +++ b/src/str.erl @@ -65,7 +65,9 @@ prefix/2, suffix/2, format/2, - to_integer/1]). + to_integer/1, + sha/1, + to_hexlist/1]). %%%=================================================================== %%% API @@ -286,6 +288,20 @@ suffix(B1, B2) -> format(Format, Args) -> iolist_to_binary(io_lib:format(Format, Args)). + +-spec sha(binary()) -> binary(). + +sha(Text) -> + Bin = crypto:hash(sha, Text), + to_hexlist(Bin). + +-spec to_hexlist(binary()) -> binary(). + +to_hexlist(S) when is_list(S) -> + to_hexlist(iolist_to_binary(S)); +to_hexlist(Bin) when is_binary(Bin) -> + << <<(digit_to_xchar(N div 16)), (digit_to_xchar(N rem 16))>> || <> <= Bin >>. + %%%=================================================================== %%% Internal functions %%%=================================================================== @@ -293,3 +309,6 @@ join_s([], _Sep) -> []; join_s([H|T], Sep) -> [H, [[Sep, X] || X <- T]]. + +digit_to_xchar(D) when (D >= 0) and (D < 10) -> D + $0; +digit_to_xchar(D) -> D + $a - 10. diff --git a/src/xmpp_stream_in.erl b/src/xmpp_stream_in.erl index 54168261e..b781bdf14 100644 --- a/src/xmpp_stream_in.erl +++ b/src/xmpp_stream_in.erl @@ -636,7 +636,7 @@ process_handshake(#handshake{data = Digest}, {false, _} -> false; {Password, _} -> - p1_sha:sha(<>) == Digest + str:sha(<>) == Digest end, case AuthRes of true ->