jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vladimirsitni...@apache.org
Subject [jmeter] branch master updated: Use lazy initialization for JOrphanUtils#SECURE_RANDOM
Date Sat, 28 Sep 2019 17:07:38 GMT
This is an automated email from the ASF dual-hosted git repository.

vladimirsitnikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 3509370  Use lazy initialization for JOrphanUtils#SECURE_RANDOM
3509370 is described below

commit 350937013ab5251b65e7cef1b81b5cecb2e2d0a7
Author: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
AuthorDate: Sat Sep 28 20:02:10 2019 +0300

    Use lazy initialization for JOrphanUtils#SECURE_RANDOM
    
    JOrphanUtils#SECURE_RANDOM is used only for generateRandomAlphanumericPassword,
    so we don't want to initialize it always.
    This saves entropy pool.
---
 .../src/main/java/org/apache/jorphan/util/JOrphanUtils.java    | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java b/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
index ccae263..a8fbb2d 100644
--- a/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
+++ b/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
@@ -45,7 +45,13 @@ public final class JOrphanUtils {
 
     private static final int DEFAULT_CHUNK_SIZE = 4096;
 
-    private static final SecureRandom SECURE_RANDOM = new SecureRandom();
+    /**
+     * This enables to initialize SecureRandom only in case it is required
+     */
+    private static class LazySecureRandom {
+        private static final SecureRandom INSTANCE = new SecureRandom();
+    }
+
     /**
      * Private constructor to prevent instantiation.
      */
@@ -773,7 +779,7 @@ public final class JOrphanUtils {
     public static String generateRandomAlphanumericPassword(int length) {
         char[][] pairs = {{'a','z'}, {'A','Z'}, {'0','9'}};
         RandomStringGenerator pwdGenerator = new RandomStringGenerator.Builder()
-                .usingRandom(max -> SECURE_RANDOM.nextInt(max))
+                .usingRandom(LazySecureRandom.INSTANCE::nextInt)
                 .withinRange(pairs)
                 .build();
         return pwdGenerator.generate(length);


Mime
View raw message