#364 Create first EncryptionMethod tests

This commit is contained in:
ljacqu
2015-12-20 00:50:34 +01:00
parent 4f6c7d579c
commit bf7a0c5a49
9 changed files with 201 additions and 19 deletions
@@ -1,6 +1,6 @@
package fr.xephi.authme.security;
import org.apache.commons.lang.ObjectUtils.Null;
import fr.xephi.authme.security.crypts.EncryptionMethod;
/**
*/
@@ -33,21 +33,21 @@ public enum HashAlgorithm {
CRAZYCRYPT1(fr.xephi.authme.security.crypts.CRAZYCRYPT1.class),
BCRYPT2Y(fr.xephi.authme.security.crypts.BCRYPT2Y.class),
SALTEDSHA512(fr.xephi.authme.security.crypts.SALTEDSHA512.class),
CUSTOM(Null.class);
CUSTOM(null);
final Class<?> classe;
final Class<? extends EncryptionMethod> clazz;
/**
* Constructor for HashAlgorithm.
*
* @param classe The class of the hash implementation.
* @param clazz The class of the hash implementation.
*/
HashAlgorithm(Class<?> classe) {
this.classe = classe;
HashAlgorithm(Class<? extends EncryptionMethod> clazz) {
this.clazz = clazz;
}
public Class<?> getclasse() {
return classe;
public Class<? extends EncryptionMethod> getClazz() {
return clazz;
}
}
@@ -36,7 +36,7 @@ public class PasswordSecurity {
EncryptionMethod method;
try {
if (alg != HashAlgorithm.CUSTOM)
method = (EncryptionMethod) alg.getclasse().newInstance();
method = alg.getClazz().newInstance();
else method = null;
} catch (InstantiationException | IllegalAccessException e) {
throw new NoSuchAlgorithmException("Problem with hash algorithm '" + alg + "'", e);
@@ -131,10 +131,11 @@ public class PasswordSecurity {
HashAlgorithm algorithm = Settings.getPasswordHash;
EncryptionMethod method;
try {
if (algorithm != HashAlgorithm.CUSTOM)
method = (EncryptionMethod) algorithm.getclasse().newInstance();
else
if (algorithm != HashAlgorithm.CUSTOM) {
method = algorithm.getClazz().newInstance();
} else {
method = null;
}
PasswordEncryptionEvent event = new PasswordEncryptionEvent(method, playerName);
Bukkit.getPluginManager().callEvent(event);
@@ -161,7 +162,7 @@ public class PasswordSecurity {
for (HashAlgorithm algo : HashAlgorithm.values()) {
if (algo != HashAlgorithm.CUSTOM) {
try {
EncryptionMethod method = (EncryptionMethod) algo.getclasse().newInstance();
EncryptionMethod method = algo.getClazz().newInstance();
if (method.comparePassword(hash, password, playerName)) {
PlayerAuth nAuth = AuthMe.getInstance().database.getAuth(playerName);
if (nAuth != null) {
@@ -1,20 +1,21 @@
package fr.xephi.authme.security;
import java.security.SecureRandom;
import java.util.Calendar;
import java.util.Random;
/**
* @author Xephi59
*/
public class RandomString {
private static final char[] chars = new char[36];
private static final Random RANDOM = new SecureRandom();
static {
for (int idx = 0; idx < 10; ++idx)
for (int idx = 0; idx < 10; ++idx) {
chars[idx] = (char) ('0' + idx);
for (int idx = 10; idx < 36; ++idx)
}
for (int idx = 10; idx < 36; ++idx) {
chars[idx] = (char) ('a' + idx - 10);
}
}
private final Random random = new Random();
@@ -34,4 +35,15 @@ public class RandomString {
return new String(buf);
}
public static String generate(int length) {
if (length < 0) {
throw new IllegalArgumentException("Length must be positive but was " + length);
}
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; ++i) {
sb.append(chars[RANDOM.nextInt(chars.length)]);
}
return sb.toString();
}
}
@@ -150,7 +150,8 @@ public class BCRYPT implements EncryptionMethod {
* @param s the string to decode
* @param maxolen the maximum number of bytes to decode
*
* @return an array containing the decoded bytes * @throws IllegalArgumentException if maxolen is invalid * @throws IllegalArgumentException
* @return an array containing the decoded bytes
* @throws IllegalArgumentException if maxolen is invalid
*/
private static byte[] decode_base64(String s, int maxolen)
throws IllegalArgumentException {