java GUID生成器 产生随机GUID的方法
-
package test;
-
-
import java.net.*;
-
import java.util.*;
-
-
import java.security.MessageDigest;
-
import java.security.NoSuchAlgorithmException;
-
import java.security.SecureRandom;
-
-
/**
-
* <p>描述: GUID生成器,参考了来自www.JavaExchange.com的RandomGUID</p>
-
*/
-
public class GuidGenerator extends Object
-
{
-
private static Random myRand; //基本java随机对象
-
private static SecureRandom mySecureRand; //安全随机对象
-
-
private static String s_id; //ip地址字符串
-
-
static
-
{
-
mySecureRand = new SecureRandom();
-
//用安全随机对象产生一随机数并用该随机数初始化基本java随机对象
-
long secureInitializer = mySecureRand.nextLong();
-
myRand = new Random(secureInitializer);
-
-
try
-
{
-
//获得当前主机的ip地址字符串
-
s_id = InetAddress.getLocalHost().toString();
-
}
-
catch (UnknownHostException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* 缺省构造器
-
*/
-
public GuidGenerator()
-
{
-
}
-
-
/**
-
* 缺省的产生随机GUID的方法
-
* @return 正确返回32字节的字符串,错误则返回长度为零的字符串
-
*/
-
public static String genRandomGUID()
-
{
-
return genRandomGUID(false);
-
}
-
-
/**
-
* 产生随机GUID的方法,考虑产生GUID的效率,将来可以应用设计模式,先生成一堆id并缓存
-
* @param secure true : 带安全选项,用安全随机数对象生成
-
* false : 不带安全选项,用基本随机数对象生成
-
* @return 正确返回32字节的字符串,错误则返回长度为零的字符串
-
*/
-
public static String genRandomGUID(boolean secure)
-
{
-
String valueBeforeMD5 = ""; //消息消化对象消化前的字符串
-
String valueAfterMD5 = ""; //经消息消化对象消化后的GUID字符串
-
-
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 = 0; //随机数
-
-
if (secure) //用安全随机对象获得随机数
-
{
-
rand = mySecureRand.nextLong();
-
}
-
else
-
//用基本随机对象获得随机数
-
{
-
rand = myRand.nextLong();
-
}
-
-
//拼接组成GUID的各个信息
-
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(); //消息消化对象进行消化动作,返回128bit
-
-
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;
-
}
-
}
-
//GUID标准格式如:C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
-
return valueAfterMD5.toUpperCase();
-
}
-
-
public static void main(String args[])
-
{
-
for (int i = 1; i < 10; i++)
-
{
-
System.out.println(Integer.toString(i) + " : " + genRandomGUID());
-
}
-
}
-
}
运行结果
-
1 : BDE4A953342C1B44CF186E6CA30E54FC
-
2 : C21659FA319D0AECBC5F56BF07055B12
-
3 : 9B0FE8CC5EB5E5E509BAF50730A163AF
-
4 : 674CAEF13B787AB1105AB15A5F6B0162
-
5 : 38D4D67BA67301703D347C13D92B80CF
-
6 : A66305848E697DB9F65F906ECF9A4E9F
-
7 : F4317957D5A38FAC6220D77D49EF408C
-
8 : 33CF7899B1DEA3E124935FC8312A3B6C
-
9 : C87E0C364F4FEF7B84519C2C3AE2322D
摘抄自网络,便于检索查找。