<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    801 WORKPLACE

    追尋夢想,自由生活

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      10 Posts :: 1 Stories :: 1 Comments :: 0 Trackbacks

    地址:http://kong-bai.javaeye.com/blog/375578

      GUID是一個128位長的數字,一般用16進制表示。算法的核心思想是結合機器的網卡、當地時間、一個隨機數來生成GUID。從理論上講,如果一臺機器每秒產生10000000個GUID,則可以保證(概率意義上)3240年不重復。

        UUID是1.5中新增的一個類,在java.util下,用它可以產生一個號稱全球唯一的ID
    Java代碼:
    import java.util.UUID;
    public class Test {
     
    public static void main(String[] args) {
      UUID uuid 
    = UUID.randomUUID(); 
      System.out.println (uuid);
    }

    }
    編譯運行輸出:
    07ca3dec-b674-41d0-af9e-9c37583b08bb


    兩種方式生成guid 與uuid

    需要comm log 庫
    Java代碼:
    /**
     * 
    @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     
    */

    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 RandomGUID extends Object {
       
    protected final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
          .getLog(getClass());

       
    public String valueBeforeMD5 = "";
       
    public String valueAfterMD5 = "";
       
    private static Random myRand;
       
    private static SecureRandom mySecureRand;

       
    private static String s_id;
       
    private static final int PAD_BELOW = 0x10;
       
    private static final int TWO_BYTES = 0xFF;

       
    /*
        * Static block to take care of one time secureRandom seed.
        * It takes a few seconds to initialize SecureRandom.  You might
        * want to consider removing this static block or replacing
        * it with a "time since first loaded" seed to reduce this time.
        * This block will run only once per JVM instance.
          
    */


       
    static {
          mySecureRand 
    = new SecureRandom();
          
    long secureInitializer = mySecureRand.nextLong();
          myRand 
    = new Random(secureInitializer);
          
    try {
             s_id 
    = InetAddress.getLocalHost().toString();
          }
     catch (UnknownHostException e) {
             e.printStackTrace();
          }


       }



       
    /*
        * Default constructor.  With no specification of security option,
        * this constructor defaults to lower security, high performance.
        
    */

       
    public RandomGUID() {
          getRandomGUID(
    false);
       }


       
    /*
        * Constructor with security option.  Setting secure true
        * enables each random number generated to be cryptographically
        * strong.  Secure false defaults to the standard Random function seeded
        * with a single cryptographically strong random number.
        
    */

       
    public RandomGUID(boolean secure) {
          getRandomGUID(secure);
       }


       
    /*
        * Method to generate the random GUID
        
    */

       
    private void getRandomGUID(boolean secure) {
          MessageDigest md5 
    = null;
          StringBuffer sbValueBeforeMD5 
    = new StringBuffer(128);

          
    try {
             md5 
    = MessageDigest.getInstance("MD5");
          }
     catch (NoSuchAlgorithmException e) {
             logger.error(
    "Error: " + e);
          }


          
    try {
             
    long time = System.currentTimeMillis();
             
    long rand = 0;

             
    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();
             StringBuffer sb 
    = new StringBuffer(32);
             
    for (int j = 0; j < array.length; ++j) {
                
    int b = array[j] & TWO_BYTES;
                
    if (b < PAD_BELOW)
                   sb.append(
    '0');
                sb.append(Integer.toHexString(b));
             }


             valueAfterMD5 
    = sb.toString();

          }
     catch (Exception e) {
             logger.error(
    "Error:" + e);
          }

       }


       
    /*
        * Convert to the standard format for GUID
        * (Useful for SQL Server UniqueIdentifiers, etc.)
        * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
        
    */

       
    public String toString() {
          String raw 
    = valueAfterMD5.toUpperCase();
          StringBuffer sb 
    = new StringBuffer(64);
          sb.append(raw.substring(
    08));
          sb.append(
    "-");
          sb.append(raw.substring(
    812));
          sb.append(
    "-");
          sb.append(raw.substring(
    1216));
          sb.append(
    "-");
          sb.append(raw.substring(
    1620));
          sb.append(
    "-");
          sb.append(raw.substring(
    20));

          
    return sb.toString();
       }



         
    // Demonstraton and self test of class
         public static void main(String args[]) {
           
    for (int i=0; i< 100; i++{
             RandomGUID myGUID 
    = new RandomGUID();
             System.out.println(
    "Seeding String=" + myGUID.valueBeforeMD5);
             System.out.println(
    "rawGUID=" + myGUID.valueAfterMD5);
             System.out.println(
    "RandomGUID=" + myGUID.toString());
           }

         }



    }

    同樣

    Java代碼:
    UUID uuid = UUID.randomUUID();
    System.out.println(
    "{"+uuid.toString()+"}");

    UUID是指在一臺機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。通常平臺會提供生成UUID的API。UUID按照開放軟件基金會(OSF)制定的標準計算,用到了以太網卡地址、納秒級時間、芯片ID碼和許多可能的數字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之后,過幾秒又生成一個UUID,則第一個部分不同,其余相同),時鐘序列,全局唯一的IEEE機器識別號(如果有網卡,從網卡獲得,沒有網卡以其他方式獲得),UUID的唯一缺陷在于生成的結果串會比較長。關于UUID這個標準使用最普遍的是微軟的GUID(Globals Unique Identifiers)。
    posted on 2010-12-01 23:54 WangShishuai 閱讀(678) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲激情视频网站| 亚洲第一区香蕉_国产a| 亚洲午夜成人精品无码色欲| 久草视频免费在线| 亚洲天堂免费在线| 日产乱码一卡二卡三免费| 亚洲国产欧洲综合997久久| 女人18毛片水真多免费播放 | 亚洲人成在线观看| 美女视频黄a视频全免费网站色窝| 亚洲色偷偷综合亚洲AVYP| APP在线免费观看视频| 久久亚洲中文字幕精品有坂深雪 | 中文字幕无码免费久久9一区9| 亚洲人色婷婷成人网站在线观看 | 韩国欧洲一级毛片免费| 亚洲国产精品美女久久久久| 又粗又黄又猛又爽大片免费| 精品多毛少妇人妻AV免费久久| 亚洲爆乳无码专区| 无码国产精品一区二区免费式影视 | 免费视频精品一区二区三区| 久久久久亚洲精品无码蜜桃| 成人免费一级毛片在线播放视频| 亚洲欧美成人综合久久久| 一级毛片直播亚洲| 久久免费国产视频| 亚洲日韩国产欧美一区二区三区| 亚洲成?Ⅴ人在线观看无码| 华人在线精品免费观看| 亚洲国产日韩视频观看| JLZZJLZZ亚洲乱熟无码| 18禁止看的免费污网站| 亚洲AV一区二区三区四区| 亚洲精品无码乱码成人| 韩国免费一级成人毛片| 国产精品一区二区三区免费| 亚洲国产综合人成综合网站00| 国产免费啪嗒啪嗒视频看看| 免费看男人j放进女人j免费看| 亚洲国产成人久久一区二区三区 |