25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Move make_rand_string() to 'randoms' module

This commit is contained in:
Marc Schink 2018-02-23 18:23:54 +01:00
parent ea87bdfbe5
commit c1e5ae5308
2 changed files with 20 additions and 23 deletions

View File

@ -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;

View File

@ -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.