From 5ab5017b2b6ee98513b65f6429943d2cdc17301d Mon Sep 17 00:00:00 2001 From: Umberto Corponi Date: Fri, 24 Jun 2016 23:45:26 +0200 Subject: [PATCH] Fix otp19 deprecation (#177) * 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. --- rebar.config | 3 ++- src/mochiweb_multipart.erl | 2 +- src/mochiweb_session.erl | 4 ++-- test/mochiweb_base64url_tests.erl | 7 ++++++- test/mochiweb_tests.erl | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rebar.config b/rebar.config index 0ae370c1..59304f5e 100644 --- a/rebar.config +++ b/rebar.config @@ -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, diff --git a/src/mochiweb_multipart.erl b/src/mochiweb_multipart.erl index 1d18ae21..0d02bee9 100644 --- a/src/mochiweb_multipart.erl +++ b/src/mochiweb_multipart.erl @@ -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()) -> diff --git a/src/mochiweb_session.erl b/src/mochiweb_session.erl index c9f88e20..f89f19b7 100644 --- a/src/mochiweb_session.erl +++ b/src/mochiweb_session.erl @@ -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), <>. @@ -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), <>. diff --git a/test/mochiweb_base64url_tests.erl b/test/mochiweb_base64url_tests.erl index 69f276aa..4f736666 100644 --- a/test/mochiweb_base64url_tests.erl +++ b/test/mochiweb_base64url_tests.erl @@ -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(<<>>). diff --git a/test/mochiweb_tests.erl b/test/mochiweb_tests.erl index 22e5b26a..23f83120 100644 --- a/test/mochiweb_tests.erl +++ b/test/mochiweb_tests.erl @@ -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),