From 2b65da6ff17302179e481cdd7d627516308e05d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 14 Jun 2008 16:12:15 +0200 Subject: [PATCH] add method to generate arbitrary-length IDs increase default length to 20 bytes --- src/net/pterodactylus/jsite/util/IdGenerator.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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; } -- 2.7.4