Created
July 21, 2016 11:03
-
-
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
This file contains 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 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