Move RandomStringUtils
This commit is contained in:
parent
42dbb27728
commit
f3cd193d47
@ -1,7 +1,7 @@
|
|||||||
package fr.xephi.authme.cache;
|
package fr.xephi.authme.cache;
|
||||||
|
|
||||||
import fr.xephi.authme.initialization.SettingsDependent;
|
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.Settings;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ public class CaptchaManager implements SettingsDependent {
|
|||||||
* @return the generated code
|
* @return the generated code
|
||||||
*/
|
*/
|
||||||
public String generateCode(String name) {
|
public String generateCode(String name) {
|
||||||
String code = RandomString.generate(captchaLength);
|
String code = RandomStringUtils.generate(captchaLength);
|
||||||
captchaCodes.put(name.toLowerCase(), code);
|
captchaCodes.put(name.toLowerCase(), code);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import fr.xephi.authme.datasource.DataSource;
|
|||||||
import fr.xephi.authme.mail.SendMailSSL;
|
import fr.xephi.authme.mail.SendMailSSL;
|
||||||
import fr.xephi.authme.output.MessageKey;
|
import fr.xephi.authme.output.MessageKey;
|
||||||
import fr.xephi.authme.security.PasswordSecurity;
|
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.security.crypts.HashedPassword;
|
||||||
import fr.xephi.authme.service.RecoveryCodeService;
|
import fr.xephi.authme.service.RecoveryCodeService;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -101,7 +101,7 @@ public class RecoverEmailCommand extends PlayerCommand {
|
|||||||
|
|
||||||
private void generateAndSendNewPassword(Player player, String email) {
|
private void generateAndSendNewPassword(Player player, String email) {
|
||||||
String name = player.getName();
|
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);
|
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
|
||||||
|
|
||||||
dataSource.updatePassword(name, hashNew);
|
dataSource.updatePassword(name, hashNew);
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import fr.xephi.authme.mail.SendMailSSL;
|
|||||||
import fr.xephi.authme.output.MessageKey;
|
import fr.xephi.authme.output.MessageKey;
|
||||||
import fr.xephi.authme.process.Management;
|
import fr.xephi.authme.process.Management;
|
||||||
import fr.xephi.authme.security.HashAlgorithm;
|
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.EmailSettings;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
import org.bukkit.entity.Player;
|
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))) {
|
} else if (commandService.getProperty(ENABLE_CONFIRM_EMAIL) && !email.equals(arguments.get(1))) {
|
||||||
commandService.send(player, MessageKey.USAGE_REGISTER);
|
commandService.send(player, MessageKey.USAGE_REGISTER);
|
||||||
} else {
|
} 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);
|
management.performRegister(player, thePass, email, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -30,7 +30,7 @@ public abstract class HexSaltedMethod implements EncryptionMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(getSaltLength());
|
return RandomStringUtils.generateHex(getSaltLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -19,7 +19,7 @@ public class IPB3 extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(5);
|
return RandomStringUtils.generateHex(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package fr.xephi.authme.security.crypts;
|
|||||||
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.security.HashUtils;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -44,7 +44,7 @@ public class IPB4 implements EncryptionMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateLowerUpper(22);
|
return RandomStringUtils.generateLowerUpper(22);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -19,7 +19,7 @@ public class MYBB extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateLowerUpper(8);
|
return RandomStringUtils.generateLowerUpper(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
package fr.xephi.authme.security.crypts;
|
||||||
|
|
||||||
import fr.xephi.authme.security.HashUtils;
|
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.AsciiRestricted;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.Usage;
|
import fr.xephi.authme.security.crypts.description.Usage;
|
||||||
@ -41,7 +41,7 @@ public class PHPFUSION extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(12);
|
return RandomStringUtils.generateHex(12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -30,7 +30,7 @@ public class SALTED2MD5 extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(saltLength);
|
return RandomStringUtils.generateHex(saltLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
package fr.xephi.authme.security.crypts;
|
||||||
|
|
||||||
import fr.xephi.authme.security.HashUtils;
|
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.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.Usage;
|
import fr.xephi.authme.security.crypts.description.Usage;
|
||||||
|
|
||||||
@ -15,6 +15,6 @@ public class SALTEDSHA512 extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(32);
|
return RandomStringUtils.generateHex(32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
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.HasSalt;
|
||||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||||
import fr.xephi.authme.security.crypts.description.SaltType;
|
import fr.xephi.authme.security.crypts.description.SaltType;
|
||||||
@ -19,7 +19,7 @@ public class WBB3 extends SeparateSaltMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateSalt() {
|
public String generateSalt() {
|
||||||
return RandomString.generateHex(40);
|
return RandomStringUtils.generateHex(40);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package fr.xephi.authme.service;
|
|||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import fr.xephi.authme.initialization.SettingsDependent;
|
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.Settings;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ public class RecoveryCodeService implements SettingsDependent {
|
|||||||
* @return the generated code
|
* @return the generated code
|
||||||
*/
|
*/
|
||||||
public String generateCode(String player) {
|
public String generateCode(String player) {
|
||||||
String code = RandomString.generateHex(recoveryCodeLength);
|
String code = RandomStringUtils.generateHex(recoveryCodeLength);
|
||||||
recoveryCodes.put(player, new ExpiringEntry(code, System.currentTimeMillis() + recoveryCodeExpirationMillis));
|
recoveryCodes.put(player, new ExpiringEntry(code, System.currentTimeMillis() + recoveryCodeExpirationMillis));
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package fr.xephi.authme.security;
|
package fr.xephi.authme.util;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -6,14 +6,15 @@ import java.util.Random;
|
|||||||
/**
|
/**
|
||||||
* Utility for generating random strings.
|
* Utility for generating random strings.
|
||||||
*/
|
*/
|
||||||
public final class RandomString {
|
public final class RandomStringUtils {
|
||||||
|
|
||||||
private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
private static final Random RANDOM = new SecureRandom();
|
private static final Random RANDOM = new SecureRandom();
|
||||||
private static final int HEX_MAX_INDEX = 16;
|
private static final int HEX_MAX_INDEX = 16;
|
||||||
private static final int LOWER_ALPHANUMERIC_INDEX = 36;
|
private static final int LOWER_ALPHANUMERIC_INDEX = 36;
|
||||||
|
|
||||||
private RandomString() {
|
// Utility class
|
||||||
|
private RandomStringUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme.security;
|
package fr.xephi.authme.security;
|
||||||
|
|
||||||
|
import fr.xephi.authme.util.RandomStringUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -8,9 +9,9 @@ import static org.hamcrest.Matchers.equalTo;
|
|||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for {@link RandomString}.
|
* Test for {@link RandomStringUtils}.
|
||||||
*/
|
*/
|
||||||
public class RandomStringTest {
|
public class RandomStringUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGenerateRandomStrings() {
|
public void shouldGenerateRandomStrings() {
|
||||||
@ -20,7 +21,7 @@ public class RandomStringTest {
|
|||||||
|
|
||||||
// when / then
|
// when / then
|
||||||
for (int length : lengths) {
|
for (int length : lengths) {
|
||||||
String result = RandomString.generate(length);
|
String result = RandomStringUtils.generate(length);
|
||||||
assertThat("Result '" + result + "' should have length " + length,
|
assertThat("Result '" + result + "' should have length " + length,
|
||||||
result.length(), equalTo(length));
|
result.length(), equalTo(length));
|
||||||
assertThat("Result '" + result + "' should only have characters a-z, 0-9",
|
assertThat("Result '" + result + "' should only have characters a-z, 0-9",
|
||||||
@ -36,7 +37,7 @@ public class RandomStringTest {
|
|||||||
|
|
||||||
// when / then
|
// when / then
|
||||||
for (int length : lengths) {
|
for (int length : lengths) {
|
||||||
String result = RandomString.generateHex(length);
|
String result = RandomStringUtils.generateHex(length);
|
||||||
assertThat("Result '" + result + "' should have length " + length,
|
assertThat("Result '" + result + "' should have length " + length,
|
||||||
result.length(), equalTo(length));
|
result.length(), equalTo(length));
|
||||||
assertThat("Result '" + result + "' should only have characters a-f, 0-9",
|
assertThat("Result '" + result + "' should only have characters a-f, 0-9",
|
||||||
@ -52,7 +53,7 @@ public class RandomStringTest {
|
|||||||
|
|
||||||
// when / then
|
// when / then
|
||||||
for (int length : lengths) {
|
for (int length : lengths) {
|
||||||
String result = RandomString.generateHex(length);
|
String result = RandomStringUtils.generateHex(length);
|
||||||
assertThat("Result '" + result + "' should have length " + length,
|
assertThat("Result '" + result + "' should have length " + length,
|
||||||
result.length(), equalTo(length));
|
result.length(), equalTo(length));
|
||||||
assertThat("Result '" + result + "' should only have characters a-z, A-Z, 0-9",
|
assertThat("Result '" + result + "' should only have characters a-z, A-Z, 0-9",
|
||||||
@ -63,7 +64,7 @@ public class RandomStringTest {
|
|||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowForInvalidLength() {
|
public void shouldThrowForInvalidLength() {
|
||||||
// given/when
|
// given/when
|
||||||
RandomString.generate(-3);
|
RandomStringUtils.generate(-3);
|
||||||
|
|
||||||
// then - throw exception
|
// then - throw exception
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user