java GUID生成器 产生随机GUID的方法

 
  1. package test;
  2.  
  3. import java.net.*;
  4. import java.util.*;
  5.  
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.security.SecureRandom;
  9.  
  10. /**
  11. * <p>描述: GUID生成器,参考了来自www.JavaExchange.com的RandomGUID</p>
  12. */
  13. public class GuidGenerator extends Object
  14. {
  15. private static Random myRand; //基本java随机对象
  16. private static SecureRandom mySecureRand; //安全随机对象
  17.  
  18. private static String s_id; //ip地址字符串
  19.  
  20. static
  21. {
  22. mySecureRand = new SecureRandom();
  23. //用安全随机对象产生一随机数并用该随机数初始化基本java随机对象
  24. long secureInitializer = mySecureRand.nextLong();
  25. myRand = new Random(secureInitializer);
  26.  
  27. try
  28. {
  29. //获得当前主机的ip地址字符串
  30. s_id = InetAddress.getLocalHost().toString();
  31. }
  32. catch (UnknownHostException e)
  33. {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. /**
  39. * 缺省构造器
  40. */
  41. public GuidGenerator()
  42. {
  43. }
  44.  
  45. /**
  46. * 缺省的产生随机GUID的方法
  47. * @return 正确返回32字节的字符串,错误则返回长度为零的字符串
  48. */
  49. public static String genRandomGUID()
  50. {
  51. return genRandomGUID(false);
  52. }
  53.  
  54. /**
  55. * 产生随机GUID的方法,考虑产生GUID的效率,将来可以应用设计模式,先生成一堆id并缓存
  56. * @param secure true : 带安全选项,用安全随机数对象生成
  57. * false : 不带安全选项,用基本随机数对象生成
  58. * @return 正确返回32字节的字符串,错误则返回长度为零的字符串
  59. */
  60. public static String genRandomGUID(boolean secure)
  61. {
  62. String valueBeforeMD5 = ""; //消息消化对象消化前的字符串
  63. String valueAfterMD5 = ""; //经消息消化对象消化后的GUID字符串
  64.  
  65. MessageDigest md5 = null; //消息消化对象
  66. StringBuffer sbValueBeforeMD5 = new StringBuffer();
  67.  
  68. try
  69. {
  70. md5 = MessageDigest.getInstance("MD5");
  71. }
  72. catch (NoSuchAlgorithmException e)
  73. {
  74. System.out.println("Error: " + e);
  75. return valueBeforeMD5;
  76. }
  77.  
  78. long time = System.currentTimeMillis(); //获得系统时间
  79. long rand = 0; //随机数
  80.  
  81. if (secure) //用安全随机对象获得随机数
  82. {
  83. rand = mySecureRand.nextLong();
  84. }
  85. else
  86. //用基本随机对象获得随机数
  87. {
  88. rand = myRand.nextLong();
  89. }
  90.  
  91. //拼接组成GUID的各个信息
  92. sbValueBeforeMD5.append(s_id);
  93. sbValueBeforeMD5.append(":");
  94. sbValueBeforeMD5.append(Long.toString(time));
  95. sbValueBeforeMD5.append(":");
  96. sbValueBeforeMD5.append(Long.toString(rand));
  97. valueBeforeMD5 = sbValueBeforeMD5.toString();
  98. md5.update(valueBeforeMD5.getBytes());
  99. byte[] array = md5.digest(); //消息消化对象进行消化动作,返回128bit
  100.  
  101. String strTemp = "";
  102. for (int i = 0; i < array.length; i++)
  103. {
  104. strTemp = (Integer.toHexString(array[i] & 0XFF));
  105. if (strTemp.length() == 1)
  106. {
  107. valueAfterMD5 = valueAfterMD5 + "0" + strTemp;
  108. }
  109. else
  110. {
  111. valueAfterMD5 = valueAfterMD5 + strTemp;
  112. }
  113. }
  114. //GUID标准格式如:C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
  115. return valueAfterMD5.toUpperCase();
  116. }
  117.  
  118. public static void main(String args[])
  119. {
  120. for (int i = 1; i < 10; i++)
  121. {
  122. System.out.println(Integer.toString(i) + " : " + genRandomGUID());
  123. }
  124. }
  125. }
 

运行结果

 
  1. 1 : BDE4A953342C1B44CF186E6CA30E54FC
  2. 2 : C21659FA319D0AECBC5F56BF07055B12
  3. 3 : 9B0FE8CC5EB5E5E509BAF50730A163AF
  4. 4 : 674CAEF13B787AB1105AB15A5F6B0162
  5. 5 : 38D4D67BA67301703D347C13D92B80CF
  6. 6 : A66305848E697DB9F65F906ECF9A4E9F
  7. 7 : F4317957D5A38FAC6220D77D49EF408C
  8. 8 : 33CF7899B1DEA3E124935FC8312A3B6C
  9. 9 : C87E0C364F4FEF7B84519C2C3AE2322D
 

 

posted @ 2024-02-21 16:04  CharyGao  阅读(47)  评论(0)    收藏  举报