Last active
October 20, 2021 18:35
-
-
Save lmammino/259a6b8e17e14a35d157dfc153e8d91f to your computer and use it in GitHub Desktop.
A sample Java class that generates a URL safe random token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.SecureRandom; | |
import java.util.Base64; | |
import java.util.Base64.Encoder; | |
public class RandomWebToken | |
{ | |
public static void main(String[] args) | |
{ | |
SecureRandom random = new SecureRandom(); | |
byte bytes[] = new byte[128]; | |
random.nextBytes(bytes); | |
Encoder encoder = Base64.getUrlEncoder().withoutPadding(); | |
String token = encoder.encodeToString(bytes); | |
System.out.println(token); | |
} | |
} |
base64-encoded string can contain slashes. )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmmm what do you call a safe token?