36 lines
1021 B
Java
36 lines
1021 B
Java
package envoy.server.util;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
/**
|
|
* Provides a secure token generation algorithm.
|
|
* <p>
|
|
* Project: <strong>envoy-server</strong><br>
|
|
* File: <strong>AuthTokenGenerator.java</strong><br>
|
|
* Created: <strong>19.09.2020</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public final class AuthTokenGenerator {
|
|
|
|
private static final int TOKEN_LENGTH = 128;
|
|
private static final char[] CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
|
|
private static final char[] BUFF = new char[TOKEN_LENGTH];
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
|
|
private AuthTokenGenerator() {}
|
|
|
|
/**
|
|
* Generates a random authentication token.
|
|
*
|
|
* @return a random authentication token
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public static String nextToken() {
|
|
for (int i = 0; i < BUFF.length; ++i)
|
|
BUFF[i] = CHARACTERS[RANDOM.nextInt(CHARACTERS.length)];
|
|
return new String(BUFF);
|
|
}
|
|
}
|