Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created July 21, 2016 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freewayz/a6b3515091c5143fd72dc5bf4d487763 to your computer and use it in GitHub Desktop.
Save freewayz/a6b3515091c5143fd72dc5bf4d487763 to your computer and use it in GitHub Desktop.
How to generate a secure random safe URL in Java, using apache libary
import org.apache.commons.codec.binary.Base64;
import java.security.SecureRandom;
/**
* Created by pitaside on 7/12/2015.
*/
public class RandomURLKeyGenerator {
public static String generateURLKey(int length) {
SecureRandom generator = new SecureRandom();
//get 32 byte number, thats a lot of byte
synchronized (generator) {
final byte[] randomByte = new byte[length];
generator.nextBytes(randomByte);
return Base64.encodeBase64URLSafeString(randomByte);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment