couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vatam...@apache.org
Subject couchdb-b64url git commit: Replace deprecated random module and crypto:rand_bytes calls
Date Mon, 02 Oct 2017 17:51:10 GMT
Repository: couchdb-b64url
Updated Branches:
  refs/heads/remove-deprecated-rand-and-crypto [created] afa766fd4


Replace deprecated random module and crypto:rand_bytes calls


Project: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/commit/afa766fd
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/tree/afa766fd
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/diff/afa766fd

Branch: refs/heads/remove-deprecated-rand-and-crypto
Commit: afa766fd48fd5d5b34d684600e7a1d749a6e9dea
Parents: 6895652
Author: Nick Vatamaniuc <vatamane@apache.org>
Authored: Mon Oct 2 13:49:54 2017 -0400
Committer: Nick Vatamaniuc <vatamane@apache.org>
Committed: Mon Oct 2 13:49:54 2017 -0400

----------------------------------------------------------------------
 test/b64url_tests.erl  | 36 ++++++++++++++----------------------
 test/benchmark.escript |  9 +++------
 2 files changed, 17 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-b64url/blob/afa766fd/test/b64url_tests.erl
----------------------------------------------------------------------
diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index b3d7779..7b21459 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -64,16 +64,8 @@ decode_bad_length_test() ->
 
 
 gen_binary() ->
-    case get(random_seed) of
-        undefined ->
-            random:seed(os:timestamp());
-        _ ->
-            ok
-    end,
-    % -1 here so that zero is a possibility
-    Length = random:uniform(?MAX_SIZE) - 1,
-    Bytes = tl([random:uniform(256)-1 || _ <- lists:seq(0, Length)]),
-    list_to_binary(Bytes).
+    Length = crypto:rand_uniform(0, ?MAX_SIZE),
+    crypto:strong_rand_bytes(Length).
 
 
 shallow_iolist() ->
@@ -93,31 +85,31 @@ bad_len_binary() ->
 
 
 to_iolist(<<>>) ->
-    case random:uniform(2) of
-        1 -> <<>>;
-        2 -> [<<>>]
+    case crypto:rand_uniform(0,2) of
+        0 -> <<>>;
+        1 -> [<<>>]
     end;
 to_iolist(B) when is_binary(B), size(B) > 0 ->
-    S = random:uniform(size(B)),
+    S = crypto:rand_uniform(1, size(B) + 1),
     <<First:S/binary, Second/binary>> = B,
-    case random:uniform(3) of
-        1 ->
+    case crypto:rand_uniform(0, 3) of
+        0 ->
             [to_iolist(First), Second];
-        2 ->
+        1 ->
             [First, to_iolist(Second)];
-        3 ->
+        2 ->
             [First, Second]
     end.
 
 
 insert_error(B) when is_binary(B), size(B) < 2 ->
-    case random:uniform(2) of
-        1 -> {<<122, 255>>, 0};
-        2 -> {<<122, 122, 255>>, 0}
+    case crypto:rand_uniform(0, 2) of
+        0 -> {<<122, 255>>, 0};
+        1 -> {<<122, 122, 255>>, 0}
     end;
 insert_error(B) when is_binary(B) ->
     B64 = couch_encode_base64url(B),
-    S = random:uniform(size(B64)-1),
+    S = crypto:rand_uniform(0, size(B64)),
     <<First:S/binary, _:1/binary, Second/binary>> = B64,
     {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}.
 

http://git-wip-us.apache.org/repos/asf/couchdb-b64url/blob/afa766fd/test/benchmark.escript
----------------------------------------------------------------------
diff --git a/test/benchmark.escript b/test/benchmark.escript
index 5c2bdd5..e1a1f70 100755
--- a/test/benchmark.escript
+++ b/test/benchmark.escript
@@ -97,7 +97,6 @@ spawn_workers(NumWorkers, St) ->
 
 
 run_worker(St) ->
-    random:seed(erlang:now()),
     receive
         start -> ok
     end,
@@ -116,8 +115,8 @@ run_worker(St, Started) ->
 
 
 do_round_trip(St) ->
-    Size = St#st.minsize + random:uniform(St#st.maxsize - St#st.minsize),
-    Data = crypto:rand_bytes(Size),
+    Size = crypto:rand_uniform(St#st.minsize,  St#st.maxsize + 1),
+    Data = crypto:strong_rand_bytes(Size),
     Encoded = (St#st.module):encode(Data),
     Data = (St#st.module):decode(Encoded),
     St#st{total_bytes=St#st.total_bytes+Size}.
@@ -137,10 +136,8 @@ decode(Url64) ->
     Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
     base64:decode(<<Url2/binary, Padding/binary>>).
 
-
 randomize(List) ->
-    random:seed(erlang:now()),
-    List0 = [{random:uniform(), L} || L <- List],
+    List0 = [{crypto:rand_uniform(0, 1 bsl 32), L} || L <- List],
     List1 = lists:sort(List0),
     [L || {_, L} <- List1].
 


Mime
View raw message