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
@@ -1,62 +0,0 @@
package fr.xephi.authme.security;
import java.security.SecureRandom;
import java.util.Random;
/**
* Utility for generating random strings.
*/
public final class RandomString {
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() {
}
/**
* Generate a string of the given length consisting of random characters within the range [0-9a-z].
*
* @param length The length of the random string to generate
* @return The random string
*/
public static String generate(int length) {
return generate(length, LOWER_ALPHANUMERIC_INDEX);
}
/**
* Generate a random hexadecimal string of the given length. In other words, the generated string
* contains characters only within the range [0-9a-f].
*
* @param length The length of the random string to generate
* @return The random hexadecimal string
*/
public static String generateHex(int length) {
return generate(length, HEX_MAX_INDEX);
}
/**
* Generate a random string with digits and lowercase and uppercase letters. The result of this
* method matches the pattern [0-9a-zA-Z].
*
* @param length The length of the random string to generate
* @return The random string
*/
public static String generateLowerUpper(int length) {
return generate(length, CHARS.length());
}
private static String generate(int length, int maxIndex) {
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.charAt(RANDOM.nextInt(maxIndex)));
}
return sb.toString();
}
}
@@ -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
@@ -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);
}
}
@@ -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
@@ -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);
}
}
@@ -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);
}
@@ -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);
}
@@ -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);
}
}
@@ -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);
}
}