Created
August 18, 2015 13:35
-
-
Save trastle/8eba3aa9c6ca6450a4b1 to your computer and use it in GitHub Desktop.
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 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