/*
*
* Copyright 2001 Sun Microsystems(TM), Inc. All Rights Reserved.
*
* The contents of this file are made available under and subject to the
* Research Use Rights of the Sun(TM) Community Source License v 3.0 (the
* "License"). Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* Contributor(s): Sun Microsystems, Inc.
*
* $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/ebxml/util/UUIDFactory.java,v 1.2 2002/11/09 01:38:16 jasilva Exp $
*
*/
package com.sun.xml.registry.ebxml.util;
import java.security.*;
/**
* A UUIDFactory
generates a UUID
*/
public class UUIDFactory {
/**
* random number generator for UUID generation
*/
private final SecureRandom secRand = new SecureRandom();
/**
* 128-bit buffer for use with secRand
*/
private final byte[] secRandBuf16 = new byte[16];
/**
* 64-bit buffer for use with secRand
*/
private final byte[] secRandBuf8 = new byte[8];
/**
* @link
* @shapeType PatternLink
* @pattern Singleton
* @supplierRole Singleton factory
*/
/*# private UUIDFactory _uuidFactory; */
private static UUIDFactory instance = null;
protected UUIDFactory() {
}
public UUID newUUID() {
secRand.nextBytes(secRandBuf16);
secRandBuf16[6] &= 0x0f;
secRandBuf16[6] |= 0x40; /* version 4 */
secRandBuf16[8] &= 0x3f;
secRandBuf16[8] |= 0x80; /* IETF variant */
secRandBuf16[10] |= 0x80; /* multicast bit */
long mostSig = 0;
for (int i = 0; i < 8; i++) {
mostSig = (mostSig << 8) | (secRandBuf16[i] & 0xff);
}
long leastSig = 0;
for (int i = 8; i < 16; i++) {
leastSig = (leastSig << 8) | (secRandBuf16[i] & 0xff);
}
return new UUID(mostSig, leastSig);
}
public boolean isValidUUID(String uuid) {
boolean isValid = true;
//57d925e0-7ad2-4dc3-ace1-b8a4064abcc7
//012345678901234567890123456789012345
int len = uuid.length();
String hexDigits = "0123456789abcdef";
for (int i=0; i