Skip to content

Commit

Permalink
Fix otp19 deprecation (mochi#177)
Browse files Browse the repository at this point in the history
* Fix warning for deprecated of crypto:rand_bytes/1

Since OTP 19 crypto:rand_bytes/1 has been deprecated
in favour of crypto:strong_rand_bytes/1.

* Fix warning for deprecated of random module

Since OTP 19 random has been deprecated in favour of rand.
  • Loading branch information
umbec authored and etrepum committed Jun 24, 2016
1 parent 2d5c5ea commit 5ab5017
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
% -*- mode: erlang -*-
{erl_opts, [debug_info,
{platform_define, "R15", 'gen_tcp_r15b_workaround'},
{platform_define, "(R14|R15|R16B-)", 'crypto_compatibility'}]}.
{platform_define, "(R14|R15|R16B-)", 'crypto_compatibility'},
{platform_define, "(R14|R15|R16B|17)", 'rand_mod_unavailable'}]}.
{cover_enabled, true}.
{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{dialyzer_opts, [{warnings, [no_return,
Expand Down
2 changes: 1 addition & 1 deletion src/mochiweb_multipart.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ parts_to_body([{Start, End, Body}], ContentType, Size) ->
{HeaderList, Body};
parts_to_body(BodyList, ContentType, Size) when is_list(BodyList) ->
parts_to_multipart_body(BodyList, ContentType, Size,
mochihex:to_hex(crypto:rand_bytes(8))).
mochihex:to_hex(crypto:strong_rand_bytes(8))).

%% @spec parts_to_multipart_body([bodypart()], ContentType::string(),
%% Size::integer(), Boundary::string()) ->
Expand Down
4 changes: 2 additions & 2 deletions src/mochiweb_session.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ ensure_binary(L) when is_list(L) ->
-ifdef(crypto_compatibility).
-spec encrypt_data(binary(), binary()) -> binary().
encrypt_data(Data, Key) ->
IV = crypto:rand_bytes(16),
IV = crypto:strong_rand_bytes(16),
Crypt = crypto:aes_cfb_128_encrypt(Key, IV, Data),
<<IV/binary, Crypt/binary>>.

Expand All @@ -141,7 +141,7 @@ gen_hmac(ExpirationTime, Data, SessionKey, Key) ->
-else.
-spec encrypt_data(binary(), binary()) -> binary().
encrypt_data(Data, Key) ->
IV = crypto:rand_bytes(16),
IV = crypto:strong_rand_bytes(16),
Crypt = crypto:block_encrypt(aes_cfb128, Key, IV, Data),
<<IV/binary, Crypt/binary>>.

Expand Down
7 changes: 6 additions & 1 deletion test/mochiweb_base64url_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ id(X) ->
X,
mochiweb_base64url:decode(
binary_to_list(mochiweb_base64url:encode(binary_to_list(X))))).

-ifdef(rand_mod_unavailable).
random_binary(Short,Long) ->
<< <<(random:uniform(256) - 1)>>
|| _ <- lists:seq(1, Short + random:uniform(1 + Long - Short) - 1) >>.
-else.
random_binary(Short,Long) ->
<< <<(rand:uniform(256) - 1)>>
|| _ <- lists:seq(1, Short + rand:uniform(1 + Long - Short) - 1) >>.
-endif.

empty_test() ->
id(<<>>).
Expand Down
2 changes: 1 addition & 1 deletion test/mochiweb_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ do_POST(Transport, Size, Times) ->
end,
TestReqs = [begin
Path = "/stuff/" ++ integer_to_list(N),
Body = crypto:rand_bytes(Size),
Body = crypto:strong_rand_bytes(Size),
#treq{path=Path, body=Body, xreply=Body}
end || N <- lists:seq(1, Times)],
ClientFun = new_client_fun('POST', TestReqs),
Expand Down

0 comments on commit 5ab5017

Please sign in to comment.