mirror of
https://e.coding.net/circlecloud/AuthMe.git
synced 2025-11-27 21:56:26 +00:00
38
src/main/java/fr/xephi/authme/security/RandomString.java
Normal file
38
src/main/java/fr/xephi/authme/security/RandomString.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package fr.xephi.authme.security;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Xephi59
|
||||
*/
|
||||
public class RandomString {
|
||||
|
||||
private static final char[] chars = new char[36];
|
||||
|
||||
private final char[] buf;
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
public RandomString(int length) {
|
||||
if (length < 1)
|
||||
throw new IllegalArgumentException("length < 1: " + length);
|
||||
buf = new char[length];
|
||||
random.setSeed(Calendar.getInstance().getTimeInMillis());
|
||||
}
|
||||
|
||||
static {
|
||||
for (int idx = 0; idx < 10; ++idx)
|
||||
chars[idx] = (char) ('0' + idx);
|
||||
for (int idx = 10; idx < 36; ++idx)
|
||||
chars[idx] = (char) ('a' + idx - 10);
|
||||
}
|
||||
|
||||
public String nextString() {
|
||||
for (int idx = 0; idx < buf.length; ++idx)
|
||||
buf[idx] = chars[random.nextInt(chars.length)];
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user