Move RandomStringUtils

This commit is contained in:
Gabriele C 2016-10-04 19:16:06 +02:00
parent 42dbb27728
commit f3cd193d47
14 changed files with 35 additions and 33 deletions

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.cache;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
@ -86,7 +86,7 @@ public class CaptchaManager implements SettingsDependent {
* @return the generated code
*/
public String generateCode(String name) {
String code = RandomString.generate(captchaLength);
String code = RandomStringUtils.generate(captchaLength);
captchaCodes.put(name.toLowerCase(), code);
return code;
}

View File

@ -9,7 +9,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.RecoveryCodeService;
import org.bukkit.entity.Player;
@ -101,7 +101,7 @@ public class RecoverEmailCommand extends PlayerCommand {
private void generateAndSendNewPassword(Player player, String email) {
String name = player.getName();
String thePass = RandomString.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
String thePass = RandomStringUtils.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
dataSource.updatePassword(name, hashNew);

View File

@ -7,7 +7,7 @@ import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
@ -80,7 +80,7 @@ public class RegisterCommand extends PlayerCommand {
} else if (commandService.getProperty(ENABLE_CONFIRM_EMAIL) && !email.equals(arguments.get(1))) {
commandService.send(player, MessageKey.USAGE_REGISTER);
} else {
String thePass = RandomString.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
String thePass = RandomStringUtils.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
management.performRegister(player, thePass, email, true);
}
}

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -30,7 +30,7 @@ public abstract class HexSaltedMethod implements EncryptionMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(getSaltLength());
return RandomStringUtils.generateHex(getSaltLength());
}
@Override

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -19,7 +19,7 @@ public class IPB3 extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(5);
return RandomStringUtils.generateHex(5);
}
}

View File

@ -2,7 +2,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.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -44,7 +44,7 @@ public class IPB4 implements EncryptionMethod {
@Override
public String generateSalt() {
return RandomString.generateLowerUpper(22);
return RandomStringUtils.generateLowerUpper(22);
}
@Override

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -19,7 +19,7 @@ public class MYBB extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateLowerUpper(8);
return RandomStringUtils.generateLowerUpper(8);
}
}

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.Usage;
@ -41,7 +41,7 @@ public class PHPFUSION extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(12);
return RandomStringUtils.generateHex(12);
}

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -30,7 +30,7 @@ public class SALTED2MD5 extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(saltLength);
return RandomStringUtils.generateHex(saltLength);
}

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.Usage;
@ -15,6 +15,6 @@ public class SALTEDSHA512 extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(32);
return RandomStringUtils.generateHex(32);
}
}

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
@ -19,7 +19,7 @@ public class WBB3 extends SeparateSaltMethod {
@Override
public String generateSalt() {
return RandomString.generateHex(40);
return RandomStringUtils.generateHex(40);
}
}

View File

@ -2,7 +2,7 @@ package fr.xephi.authme.service;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
@ -42,7 +42,7 @@ public class RecoveryCodeService implements SettingsDependent {
* @return the generated code
*/
public String generateCode(String player) {
String code = RandomString.generateHex(recoveryCodeLength);
String code = RandomStringUtils.generateHex(recoveryCodeLength);
recoveryCodes.put(player, new ExpiringEntry(code, System.currentTimeMillis() + recoveryCodeExpirationMillis));
return code;
}

View File

@ -1,4 +1,4 @@
package fr.xephi.authme.security;
package fr.xephi.authme.util;
import java.security.SecureRandom;
import java.util.Random;
@ -6,14 +6,15 @@ import java.util.Random;
/**
* Utility for generating random strings.
*/
public final class RandomString {
public final class RandomStringUtils {
private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
private static final int HEX_MAX_INDEX = 16;
private static final int LOWER_ALPHANUMERIC_INDEX = 36;
private RandomString() {
// Utility class
private RandomStringUtils() {
}
/**

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.security;
import fr.xephi.authme.util.RandomStringUtils;
import org.junit.Test;
import java.util.regex.Pattern;
@ -8,9 +9,9 @@ import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link RandomString}.
* Test for {@link RandomStringUtils}.
*/
public class RandomStringTest {
public class RandomStringUtilsTest {
@Test
public void shouldGenerateRandomStrings() {
@ -20,7 +21,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generate(length);
String result = RandomStringUtils.generate(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-z, 0-9",
@ -36,7 +37,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generateHex(length);
String result = RandomStringUtils.generateHex(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-f, 0-9",
@ -52,7 +53,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generateHex(length);
String result = RandomStringUtils.generateHex(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-z, A-Z, 0-9",
@ -63,7 +64,7 @@ public class RandomStringTest {
@Test(expected = IllegalArgumentException.class)
public void shouldThrowForInvalidLength() {
// given/when
RandomString.generate(-3);
RandomStringUtils.generate(-3);
// then - throw exception
}