Skip to content

Instantly share code, notes, and snippets.

@lmammino
Last active October 20, 2021 18:35
Show Gist options
  • Save lmammino/259a6b8e17e14a35d157dfc153e8d91f to your computer and use it in GitHub Desktop.
Save lmammino/259a6b8e17e14a35d157dfc153e8d91f to your computer and use it in GitHub Desktop.
A sample Java class that generates a URL safe random token
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);
}
}
@n-dragon
Copy link

hmmm what do you call a safe token?

@Babay88
Copy link

Babay88 commented Mar 28, 2021

base64-encoded string can contain slashes. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment