Skip to content

Instantly share code, notes, and snippets.

@trastle
Created August 18, 2015 13:35
Show Gist options
  • Save trastle/8eba3aa9c6ca6450a4b1 to your computer and use it in GitHub Desktop.
Save trastle/8eba3aa9c6ca6450a4b1 to your computer and use it in GitHub Desktop.
import org.apache.commons.codec.binary.Base64;
import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
public class TokenGenerator {
private final KeyGenerator keyGenerator;
public TokenGenerator(){
try {
this.keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to initialise KeyGenerator in TokenGenerator", e);
}
}
public String generateToken(){
return getUrlSafeAesSecretKeyAsBase64String();
}
private String getUrlSafeAesSecretKeyAsBase64String(){
return new String(Base64.encodeBase64URLSafe(keyGenerator.generateKey().getEncoded()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment