/* URN5Namespace.java * * Copyright (c) 2002, Tuomas Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Tuomas Lukka */ package org.fenfire.util; import java.util.*; import java.security.SecureRandom; import java.io.*; import java.lang.String; /** A class which is able to generate globally unique URN-5 * identifiers. * URN-5 identifiers are globally unique, nonhierarchical identifiers. *
* The names generated by the same instance share the same "random number" * part and differ only by the "local part". * See http://www.iana.org/assignments/urn-informal/urn-5 *
* The java.security.SecureRandom class is used as a source of random numbers.
*/
public class URN5Namespace {
/** 21 bytes ^= 28 base64 namespace chars */
public static final int NAMESPACECHARS = 28;
/** 64 characters for encoding.
*/
private static String base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
/** The random number generator to use for generating the initial string.
*/
private static SecureRandom sr;
/** Static instance to use when security concerns aren't too high.
* The security concerns are that everything using this
* in the same session will have the same 'random number' part,
* making it obvious that they were created in the same session.
*/
public static final URN5Namespace instance = new URN5Namespace();
private String namespace;
private long nextNumber = 1;
/** Generate a sequence of base64 characters
* with at least 160 bits of randomness.
*/
public static synchronized String generateRandomChars() {
if(sr == null)
sr = new SecureRandom();
StringBuffer chars = new StringBuffer(NAMESPACECHARS);
for(int i=0; i