From c1e5ae53086b990e14d2ed80c2539df5aace0e7a Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Fri, 23 Feb 2018 18:23:54 +0100 Subject: [PATCH] Move make_rand_string() to 'randoms' module --- src/mod_http_upload.erl | 23 +---------------------- src/randoms.erl | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index 9f5894499..9a5952926 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -589,7 +589,7 @@ create_slot(#state{service_url = undefined, case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow, [JID, UserDir, Size, Lang]) of allow -> - RandStr = make_rand_string(SecretLength), + RandStr = randoms:get_alphanum_string(SecretLength), FileStr = make_file_string(File), ?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)", [jid:encode(JID), File]), @@ -687,27 +687,6 @@ make_user_string(#jid{luser = U}, node) -> make_file_string(File) -> re:replace(File, <<"[^a-zA-Z0-9_.-]">>, <<$_>>, [global, {return, binary}]). --spec make_rand_string(non_neg_integer()) -> binary(). - -make_rand_string(Length) -> - list_to_binary(make_rand_string([], Length)). - --spec make_rand_string(string(), non_neg_integer()) -> string(). - -make_rand_string(S, 0) -> S; -make_rand_string(S, N) -> make_rand_string([make_rand_char() | S], N - 1). - --spec make_rand_char() -> char(). - -make_rand_char() -> - map_int_to_char(randoms:uniform(0, 61)). - --spec map_int_to_char(0..61) -> char(). - -map_int_to_char(N) when N =< 9 -> N + 48; % Digit. -map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character. -map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character. - -spec yield_content_type(binary()) -> binary(). yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE; diff --git a/src/randoms.erl b/src/randoms.erl index a64012ca3..101241a3c 100644 --- a/src/randoms.erl +++ b/src/randoms.erl @@ -28,7 +28,7 @@ -author('alexey@process-one.net'). -export([get_string/0, uniform/0, uniform/1, uniform/2, bytes/1, - round_robin/1]). + round_robin/1, get_alphanum_string/1]). -define(THRESHOLD, 16#10000000000000000). @@ -71,3 +71,21 @@ bytes(N) -> -spec round_robin(pos_integer()) -> non_neg_integer(). round_robin(N) -> p1_time_compat:unique_integer([monotonic, positive]) rem N. + +-spec get_alphanum_string(non_neg_integer()) -> binary(). +get_alphanum_string(Length) -> + list_to_binary(get_alphanum_string([], Length)). + +-spec get_alphanum_string(string(), non_neg_integer()) -> string(). +get_alphanum_string(S, 0) -> S; +get_alphanum_string(S, N) -> + get_alphanum_string([make_rand_char() | S], N - 1). + +-spec make_rand_char() -> char(). +make_rand_char() -> + map_int_to_char(uniform(0, 61)). + +-spec map_int_to_char(0..61) -> char(). +map_int_to_char(N) when N =< 9 -> N + 48; % Digit. +map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character. +map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character.