Skip to content

Instantly share code, notes, and snippets.

@alejandrade
Forked from lmammino/RandomWebToken.java
Created May 5, 2018 06:39
Show Gist options
  • Save alejandrade/181cf8c49eef7bd84719c6e124fad330 to your computer and use it in GitHub Desktop.
Save alejandrade/181cf8c49eef7bd84719c6e124fad330 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment