Skip to content

Commit

Permalink
Create and use a single SecureRandom instance (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashatyrev authored Dec 27, 2021
1 parent d780ba4 commit 5a2c87d
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import java.math.BigInteger;
import java.nio.ByteOrder;
import java.security.SecureRandom;
Expand Down Expand Up @@ -43,6 +45,8 @@ public class Functions {
private static final int AUTH_RESP_KEY_LENGTH = 16;
private static final int MS_IN_SECOND = 1000;

private static final Supplier<SecureRandom> SECURE_RANDOM = Suppliers.memoize(SecureRandom::new);

/** SHA2 (SHA256) */
public static Bytes hash(Bytes value) {
return Hashes.sha256(value);
Expand Down Expand Up @@ -207,7 +211,7 @@ public static long getTime() {

/** Random provider */
public static Random getRandom() {
return new SecureRandom();
return SECURE_RANDOM.get();
}

/**
Expand Down

0 comments on commit 5a2c87d

Please sign in to comment.