#364 Add HashAlgorithm integration test, fix failing tests

- Create integration test for the HashAlgorithm enum
- Create AsciiRestricted annotation and make test aware of it
- Add option to skip "same hash for same salt" test (for wordpress)
- Change some EncryptionMethods to extend from a common superclass
This commit is contained in:
ljacqu
2015-12-29 13:29:26 +01:00
parent 531327dd9b
commit 922082f312
13 changed files with 123 additions and 76 deletions
@@ -522,7 +522,7 @@ public class BCRYPT implements EncryptionMethod {
@Override
public HashResult computeHash(String password, String name) {
String salt = generateSalt();
return new HashResult(hashpw(password, salt), salt);
return new HashResult(hashpw(password, salt), null);
}
@Override
@@ -1,10 +1,12 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
import fr.xephi.authme.security.pbkdf2.PBKDF2Engine;
import fr.xephi.authme.security.pbkdf2.PBKDF2Parameters;
import javax.xml.bind.DatatypeConverter;
@AsciiRestricted
public class CryptPBKDF2Django extends HexSaltedMethod {
@Override
@@ -10,31 +10,16 @@ import static fr.xephi.authme.security.HashUtils.md5;
@Recommendation(Usage.DO_NOT_USE)
@HasSalt(value = SaltType.TEXT, length = 5)
public class IPB3 implements EncryptionMethod {
public class IPB3 extends SeparateSaltMethod {
@Override
public String computeHash(String password, String salt, String name) {
return md5(md5(salt) + md5(password));
}
@Override
public HashResult computeHash(String password, String name) {
String salt = generateSalt();
return new HashResult(computeHash(password, salt, name), salt);
}
@Override
public boolean comparePassword(String hash, String password, String salt, String name) {
return hash.equals(computeHash(password, salt, name));
}
@Override
public String generateSalt() {
return RandomString.generateHex(5);
}
@Override
public boolean hasSeparateSalt() {
return true;
}
}
@@ -4,31 +4,16 @@ import fr.xephi.authme.security.RandomString;
import static fr.xephi.authme.security.HashUtils.md5;
public class MYBB implements EncryptionMethod {
public class MYBB extends SeparateSaltMethod {
@Override
public String computeHash(String password, String salt, String name) {
return md5(md5(salt) + md5(password));
}
@Override
public HashResult computeHash(String password, String name) {
String salt = generateSalt();
return new HashResult(computeHash(password, salt, name), salt);
}
@Override
public boolean comparePassword(String hash, String password, String salt, String name) {
return hash.equals(computeHash(password, salt, name));
}
@Override
public String generateSalt() {
return RandomString.generateHex(8);
}
@Override
public boolean hasSeparateSalt() {
return true;
}
}
@@ -4,10 +4,10 @@
*/
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.MessageDigestAlgorithm;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
/**
@@ -20,10 +20,10 @@ public class PHPBB extends HexSaltedMethod {
private static String md5(String data) {
try {
byte[] bytes = data.getBytes("ISO-8859-1");
MessageDigest md5er = MessageDigest.getInstance("MD5");
MessageDigest md5er = HashUtils.getDigest(MessageDigestAlgorithm.MD5);
byte[] hash = md5er.digest(bytes);
return bytes2hex(hash);
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@@ -132,9 +132,10 @@ public class PHPBB extends HexSaltedMethod {
}
private boolean phpbb_check_hash(String password, String hash) {
if (hash.length() == 34)
if (hash.length() == 34) {
return _hash_crypt_private(password, hash).equals(hash);
else return md5(password).equals(hash);
}
return md5(password).equals(hash);
}
@Override
@@ -2,6 +2,7 @@ package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.Usage;
@@ -12,7 +13,8 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Recommendation(Usage.DO_NOT_USE)
public class PHPFUSION implements EncryptionMethod {
@AsciiRestricted
public class PHPFUSION extends SeparateSaltMethod {
@Override
public String computeHash(String password, String salt, String name) {
@@ -37,25 +39,10 @@ public class PHPFUSION implements EncryptionMethod {
}
}
@Override
public HashResult computeHash(String password, String name) {
String salt = generateSalt();
return new HashResult(computeHash(password, salt, name), salt);
}
@Override
public boolean comparePassword(String hash, String password, String salt, String name) {
return hash.equals(computeHash(password, salt, name));
}
@Override
public String generateSalt() {
return RandomString.generateHex(12);
}
@Override
public boolean hasSeparateSalt() {
return true;
}
}
@@ -1,7 +1,5 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.AuthMe;
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;
@@ -9,10 +7,6 @@ import fr.xephi.authme.security.crypts.description.SaltType;
import fr.xephi.authme.security.crypts.description.Usage;
import fr.xephi.authme.settings.Settings;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static fr.xephi.authme.security.HashUtils.md5;
@Recommendation(Usage.ACCEPTABLE) // presuming that length is something sensible (>= 8)
@@ -0,0 +1,15 @@
package fr.xephi.authme.security.crypts.description;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Denotes an encryption algorithm that is restricted to the ASCII charset.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AsciiRestricted {
}