#848 Prevent "invalid salt version" when hash format is clearly not BCrypt

This commit is contained in:
ljacqu
2016-08-13 20:10:38 +02:00
parent 58308cffb5
commit 7f3c308009
5 changed files with 27 additions and 3 deletions
@@ -67,6 +67,17 @@ public final class HashUtils {
}
}
/**
* Return whether the given hash starts like a BCrypt hash. Checking with this method
* beforehand prevents the BcryptService from throwing certain exceptions.
*
* @param hash The salt to verify
* @return True if the salt is valid, false otherwise
*/
public static boolean isValidBcryptHash(String hash) {
return hash.length() > 3 && hash.substring(0, 2).equals("$2");
}
/**
* Hash the message with the given algorithm and return the hash in its hexadecimal notation.
*
@@ -1,6 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@@ -36,7 +37,7 @@ public class BCRYPT implements EncryptionMethod {
@Override
public boolean comparePassword(String password, HashedPassword hash, String name) {
try {
return hash.getHash().length() > 3 && BCryptService.checkpw(password, hash.getHash());
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("Bcrypt checkpw() returned " + StringUtils.formatException(e));
}
@@ -1,6 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
@@ -34,7 +35,7 @@ public class IPB4 implements EncryptionMethod {
@Override
public boolean comparePassword(String password, HashedPassword hash, String name) {
try {
return hash.getHash().length() > 3 && BCryptService.checkpw(password, hash.getHash());
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("Bcrypt checkpw() returned " + StringUtils.formatException(e));
}
@@ -1,6 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.util.StringUtils;
import java.util.regex.Matcher;
@@ -29,7 +30,7 @@ public class XFBCRYPT implements EncryptionMethod {
@Override
public boolean comparePassword(String password, HashedPassword hash, String salt) {
try {
return hash.getHash().length() > 3 && BCryptService.checkpw(password, hash.getHash());
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("XfBCrypt checkpw() returned " + StringUtils.formatException(e));
}