//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
public class UUIDGenerator
{
	private static Random myRand;
	private static SecureRandom mySecureRand;
	private static String s_id;
	static {
		mySecureRand = new SecureRandom();
		long secureInitializer = mySecureRand.nextLong();
		myRand = new Random(secureInitializer);
		try
		{
			s_id = InetAddress.getLocalHost().toString();
		}
		catch (UnknownHostException e)
		{
			e.printStackTrace();
		}
	}
	
	public UUIDGenerator()
	{
	}
	
	public static String genRandomUUID()
	{
		return genRandomUUID(false);
	}
	public static String genRandomUUID(boolean secure)
	{
		String valueBeforeMD5 = "";
		String valueAfterMD5 = "";
		MessageDigest md5 = null;
		StringBuffer sbValueBeforeMD5 = new StringBuffer();
		try
		{
			md5 = MessageDigest.getInstance("MD5");
		}
		catch (NoSuchAlgorithmException e)
		{
			System.out.println("Error: " + e);
			return valueBeforeMD5;
		}
		long time = System.currentTimeMillis();
		long rand = 0L;
		if (secure)
			rand = mySecureRand.nextLong();
		else
			rand = myRand.nextLong();
		sbValueBeforeMD5.append(s_id);
		sbValueBeforeMD5.append(":");
		sbValueBeforeMD5.append(Long.toString(time));
		sbValueBeforeMD5.append(":");
		sbValueBeforeMD5.append(Long.toString(rand));
		valueBeforeMD5 = sbValueBeforeMD5.toString();
		md5.update(valueBeforeMD5.getBytes());
		byte array[] = md5.digest();
		String strTemp = "";
		for (int i = 0; i < array.length; i++)
		{
			strTemp = Integer.toHexString(array[i] & 0xff);
			if (strTemp.length() == 1)
				valueAfterMD5 = valueAfterMD5 + "0" + strTemp;
			else
				valueAfterMD5 = valueAfterMD5 + strTemp;
		}
		return valueAfterMD5.toUpperCase();
	}
}