commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1814628 - /commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java
Date Wed, 08 Nov 2017 19:44:06 GMT
Author: ggregory
Date: Wed Nov  8 19:44:06 2017
New Revision: 1814628

URL: http://svn.apache.org/viewvc?rev=1814628&view=rev
Log:
Use ThreadLocalRandom instead of Random and pull out its instantiation from the loop. Preallocate
StringBuilder as well.

Modified:
    commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java

Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java?rev=1814628&r1=1814627&r2=1814628&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/B64.java Wed
Nov  8 19:44:06 2017
@@ -16,7 +16,7 @@
  */
 package org.apache.commons.codec.digest;
 
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * Base64 like method to convert binary bytes into ASCII chars.
@@ -65,14 +65,18 @@ class B64 {
 
     /**
      * Generates a string of random chars from the B64T set.
+     * <p>
+     * The salt is generated with {@link ThreadLocalRandom}.
+     * </p>
      *
      * @param num
      *            Number of chars to generate.
      */
     static String getRandomSalt(final int num) {
-        final StringBuilder saltString = new StringBuilder();
+        final StringBuilder saltString = new StringBuilder(num);
+        final ThreadLocalRandom current = ThreadLocalRandom.current();
         for (int i = 1; i <= num; i++) {
-            saltString.append(B64T.charAt(new Random().nextInt(B64T.length())));
+            saltString.append(B64T.charAt(current.nextInt(B64T.length())));
         }
         return saltString.toString();
     }



Mime
View raw message