Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89906
  • 博文数量: 13
  • 博客积分: 1523
  • 博客等级: 上尉
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-14 14:54
文章分类

全部博文(13)

文章存档

2011年(5)

2010年(6)

2009年(1)

2008年(1)

我的朋友

分类: Java

2011-12-06 17:56:27


  1. 使用Java生成GUID的类
  2. GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复。
  3. import java.net.*;
  4. import java.util.*;
  5. import java.security.*;
  6. public class GuidCreator {
  7. private String seedingString = “”;
  8. private String rawGUID = “”;
  9. private boolean bSecure = false;
  10. private static Random myRand;
  11. private static SecureRandom mySecureRand;
  12. private static String s_id;
  13. public static final int BeforeMD5 = 1;
  14. public static final int AfterMD5 = 2;
  15. public static final int FormatString = 3;
  16. static {
  17. mySecureRand = new SecureRandom();
  18. long secureInitializer = mySecureRand.nextLong();
  19. myRand = new Random(secureInitializer);
  20. try {
  21. s_id = InetAddress.getLocalHost().toString();
  22. } catch (UnknownHostException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. /*
  27. * Default constructor. With no specification of security option,
  28. * this constructor defaults to lower security, high performance.
  29. */
  30. public GuidCreator() { }
  31. /*
  32. * Constructor with security option. Setting secure true
  33. * enables each random number generated to be cryptographically
  34. * strong. Secure false defaults to the standard Random function seeded
  35. * with a single cryptographically strong random number.
  36. */
  37. public GuidCreator(boolean secure) {
  38. bSecure = secure;
  39. }
  40. /*
  41. * Method to generate the random GUID
  42. */
  43. private void getRandomGUID(boolean secure) {
  44. MessageDigest md5 = null;
  45. StringBuffer sbValueBeforeMD5 = new StringBuffer();
  46. try {
  47. md5 = MessageDigest.getInstance(”MD5″);
  48. } catch (NoSuchAlgorithmException e) {
  49. System.out.println(”Error:+ e);
  50. }
  51. try {
  52. long time = System.currentTimeMillis();
  53. long rand = 0;
  54. if (secure) {
  55. rand = mySecureRand.nextLong();
  56. } else {
  57. rand = myRand.nextLong();
  58. }
  59. // This StringBuffer can be a long as you need; the MD5
  60. // hash will always return 128 bits. You can change
  61. // the seed to include anything you want here.
  62. // You could even stream a file through the MD5 making
  63. // the odds of guessing it at least as great as that
  64. // of guessing the contents of the file!
  65. sbValueBeforeMD5.append(s_id);
  66. sbValueBeforeMD5.append(:);
  67. sbValueBeforeMD5.append(Long.toString(time));
  68. sbValueBeforeMD5.append(:);
  69. sbValueBeforeMD5.append(Long.toString(rand));
  70. seedingString = sbValueBeforeMD5.toString();
  71. md5.update(seedingString.getBytes());
  72. byte[] array = md5.digest();
  73. StringBuffer sb = new StringBuffer();
  74. for (int j = 0; j < array.length; ++j) {
  75. int b = array[j] & 0xFF;
  76. if (b < 0×10) sb.append(’0′);
  77. sb.append(Integer.toHexString(b));
  78. }
  79. rawGUID = sb.toString();
  80. } catch (Exception e) {
  81. System.out.println(”Error:+ e);
  82. }
  83. }
  84. public String createNewGuid(int nFormatType, boolean secure) {
  85. getRandomGUID(secure);
  86. String sGuid = “”;
  87. if (BeforeMD5 == nFormatType) {
  88. sGuid = this.seedingString;
  89. } else if (AfterMD5 == nFormatType) {
  90. sGuid = this.rawGUID;
  91. } else {
  92. sGuid = this.toString();
  93. }
  94. return sGuid;
  95. }
  96. public String createNewGuid(int nFormatType) {
  97. return this.createNewGuid(nFormatType, this.bSecure);
  98. }
  99. /*
  100. * Convert to the standard format for GUID
  101. * (Useful for SQL Server UniqueIdentifiers, etc.)
  102. * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
  103. */
  104. public String toString() {
  105. String raw = rawGUID.toUpperCase();
  106. StringBuffer sb = new StringBuffer();
  107. sb.append(raw.substring(0, 8));
  108. sb.append(-);
  109. sb.append(raw.substring(8, 12));
  110. sb.append(-);
  111. sb.append(raw.substring(12, 16));
  112. sb.append(-);
  113. sb.append(raw.substring(16, 20));
  114. sb.append(-);
  115. sb.append(raw.substring(20));
  116. return sb.toString();
  117. }
  118. }
  119. /**
  120. * Use Exceple
  121. * public static void main(String args[]) {
  122. * for (int i=0; i< 100; i++) {
  123. * GuidCreator myGUID = new GuidCreator();
  124. * System.out.println(”Seeding String=” + myGUID.getNewGuid(GuidCreator.BeforeMD5));
  125. * System.out.println(”rawGUID=” + myGUID.getNewGuid(GuidCreator.AfterMD5));
  126. * System.out.println(”RandomGUID=” + myGUID.getNewGuid(GuidCreator.FormatString));
  127. * }
  128. * }
  129. */

阅读(2584) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~