From: David ‘Bombe’ Roden Date: Sat, 14 Jun 2008 14:12:15 +0000 (+0200) Subject: add method to generate arbitrary-length IDs X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2b65da6ff17302179e481cdd7d627516308e05d8;p=jSite2.git add method to generate arbitrary-length IDs increase default length to 20 bytes --- diff --git a/src/net/pterodactylus/jsite/util/IdGenerator.java b/src/net/pterodactylus/jsite/util/IdGenerator.java index 4bcb093..a997512 100644 --- a/src/net/pterodactylus/jsite/util/IdGenerator.java +++ b/src/net/pterodactylus/jsite/util/IdGenerator.java @@ -28,16 +28,32 @@ import java.util.Random; */ public class IdGenerator { + /** The default length of IDs (in bytes before conversion to a string). */ + public static final int DEFAULT_LENGTH = 20; + /** The RNG used to created IDs. */ private static Random random = new Random(); /** - * Generates a new random ID, consisting of 16 random bytes. + * Generates a new random ID, consisting of {@link #DEFAULT_LENGTH} random + * bytes. * * @return The new ID */ public static byte[] generateId() { - byte[] idBytes = new byte[16]; + return generateId(DEFAULT_LENGTH); + } + + /** + * Generates a new random ID, consisting of length random + * bytes. + * + * @param length + * The length of the ID in bytes before conversion to a string + * @return The new ID + */ + public static byte[] generateId(int length) { + byte[] idBytes = new byte[length]; random.nextBytes(idBytes); return idBytes; }