|
@@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
+import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
@@ -32,7 +33,7 @@ import java.util.regex.Pattern;
|
|
|
*/
|
|
|
public class BCryptPasswordEncoder implements PasswordEncoder {
|
|
|
private Pattern BCRYPT_PATTERN = Pattern
|
|
|
- .compile("\\A\\$2(a|y|b)?\\$\\d\\d\\$[./0-9A-Za-z]{53}");
|
|
|
+ .compile("\\A\\$2(a|y|b)?\\$(\\d\\d)\\$[./0-9A-Za-z]{53}");
|
|
|
private final Log logger = LogFactory.getLog(getClass());
|
|
|
|
|
|
private final int strength;
|
|
@@ -93,20 +94,16 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
|
|
throw new IllegalArgumentException("Bad strength");
|
|
|
}
|
|
|
this.version = version;
|
|
|
- this.strength = strength;
|
|
|
+ this.strength = strength == -1 ? 10 : strength;
|
|
|
this.random = random;
|
|
|
}
|
|
|
|
|
|
public String encode(CharSequence rawPassword) {
|
|
|
String salt;
|
|
|
- if (strength > 0) {
|
|
|
- if (random != null) {
|
|
|
- salt = BCrypt.gensalt(version.getVersion(), strength, random);
|
|
|
- } else {
|
|
|
- salt = BCrypt.gensalt(version.getVersion(), strength);
|
|
|
- }
|
|
|
+ if (random != null) {
|
|
|
+ salt = BCrypt.gensalt(version.getVersion(), strength, random);
|
|
|
} else {
|
|
|
- salt = BCrypt.gensalt(version.getVersion());
|
|
|
+ salt = BCrypt.gensalt(version.getVersion(), strength);
|
|
|
}
|
|
|
return BCrypt.hashpw(rawPassword.toString(), salt);
|
|
|
}
|
|
@@ -125,6 +122,23 @@ public class BCryptPasswordEncoder implements PasswordEncoder {
|
|
|
return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public boolean upgradeEncoding(String encodedPassword) {
|
|
|
+ if (encodedPassword == null || encodedPassword.length() == 0) {
|
|
|
+ logger.warn("Empty encoded password");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Matcher matcher = BCRYPT_PATTERN.matcher(encodedPassword);
|
|
|
+ if (!matcher.matches()) {
|
|
|
+ throw new IllegalArgumentException("Encoded password does not look like BCrypt: " + encodedPassword);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ int strength = Integer.parseInt(matcher.group(2));
|
|
|
+ return strength < this.strength;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Stores the default bcrypt version for use in configuration.
|
|
|
*
|