1
0
mirror of synced 2026-05-22 13:23:17 +00:00

Defer SecureRandom Construction Until Usage

Issue gh-17824

Signed-off-by: Josh Cummings <3627351+jzheaux@users.noreply.github.com>
This commit is contained in:
Josh Cummings
2026-04-07 16:09:27 -06:00
parent c21fc6c433
commit 229eb3ea46
@@ -17,6 +17,7 @@
package org.springframework.security.crypto.bcrypt; package org.springframework.security.crypto.bcrypt;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.function.Supplier;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -44,7 +45,7 @@ public class BCryptPasswordEncoder extends AbstractValidatingPasswordEncoder {
private final BCryptVersion version; private final BCryptVersion version;
private final SecureRandom random; private final Supplier<SecureRandom> random;
public BCryptPasswordEncoder() { public BCryptPasswordEncoder() {
this(-1); this(-1);
@@ -99,7 +100,7 @@ public class BCryptPasswordEncoder extends AbstractValidatingPasswordEncoder {
} }
this.version = version; this.version = version;
this.strength = (strength == -1) ? 10 : strength; this.strength = (strength == -1) ? 10 : strength;
this.random = (random != null) ? random : SecureRandomHolder.INSTANCE; this.random = (random != null) ? () -> random : SecureRandomHolder::getInstance;
} }
@Override @Override
@@ -109,7 +110,7 @@ public class BCryptPasswordEncoder extends AbstractValidatingPasswordEncoder {
} }
private String getSalt() { private String getSalt() {
return BCrypt.gensalt(this.version.getVersion(), this.strength, this.random); return BCrypt.gensalt(this.version.getVersion(), this.strength, this.random.get());
} }
@Override @Override
@@ -160,6 +161,10 @@ public class BCryptPasswordEncoder extends AbstractValidatingPasswordEncoder {
private static final SecureRandom INSTANCE = new SecureRandom(); private static final SecureRandom INSTANCE = new SecureRandom();
private static SecureRandom getInstance() {
return INSTANCE;
}
} }
} }