Implement the encryption method and test unit.

This commit is contained in:
DNx5 2016-02-09 05:58:59 +07:00
parent 9959c0f7d5
commit 8e38384a0d
2 changed files with 47 additions and 4 deletions

View File

@ -1,35 +1,56 @@
package fr.xephi.authme.security.crypts.description;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.crypts.BCryptService;
import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.StringUtils;
import java.security.SecureRandom;
@Recommendation(Usage.DOES_NOT_WORK)
@HasSalt(value = SaltType.TEXT)
public class IPB4 implements EncryptionMethod {
SecureRandom random = new SecureRandom();
@Override
public String computeHash(String password, String salt, String name) {
return null;
return BCryptService.hashpw(password, "$2a$13$" + salt);
}
@Override
public HashedPassword computeHash(String password, String name) {
return null;
String salt = generateSalt();
return new HashedPassword(computeHash(password, salt, name), salt);
}
@Override
public boolean comparePassword(String password, HashedPassword hash, String name) {
try {
return hash.getHash().length() > 3 && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.showError("Bcrypt checkpw() returned " + StringUtils.formatException(e));
}
return false;
}
@Override
public String generateSalt() {
return null;
StringBuilder sb = new StringBuilder(22);
for (int i = 0; i < 22; i++) {
char chr;
do {
chr = (char) (random.nextInt((122 - 48) + 1) + 48);
}
while ((chr >= 58 && chr <= 64) || (chr >= 91 && chr <= 96));
sb.append(chr);
}
return sb.toString();
}
@Override
public boolean hasSeparateSalt() {
return false;
return true;
}
}

View File

@ -0,0 +1,22 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.crypts.description.IPB4;
import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass;
public class IPB4Test extends AbstractEncryptionMethodTest {
@BeforeClass
public static void setUpSettings() {
WrapperMock.createInstance();
}
public IPB4Test() {
super(new IPB4(),
new HashedPassword("$2a$13$leEvXu77OIwPwNvtZIJvaeAx8EItGHuR3nIlq8416g0gXeJaQdrr2", "leEvXu77OIwPwNvtZIJval"), //password
new HashedPassword("$2a$13$xyTTP9zhQQtRRKIJPv5AuuOGJ6Ni9FLbDhcuIAcPjt3XzCxIWe3Uu", "xyTTP9zhQQtRRKIJPv5Au3"), //PassWord1
new HashedPassword("$2a$13$rGBrqErm9DZyzbxIGHlgf.xfA15/4d5Ay/TK.3y9lG3AljcoG9Lsi", "rGBrqErm9DZyzbxIGHlgfN"), //&^%te$t?Pw@_
new HashedPassword("$2a$13$18dKXZLoGpeHHL81edM9HuipiUcMjn5VDJHlxwRUMRXfJ1b.ZQrJ.", "18dKXZLoGpeHHL81edM9H6")); //âË_3(íù*
}
}