摘要:優(yōu)點隨機性從定義中可以看出,時間和隨機數(shù)的加入使得生成的是基本隨機的。唯一性由于給予的機器識別號的唯一性,保證了不同設備的也是不同的,而時間加隨機數(shù),從理論上講,如果一臺機器每秒產(chǎn)生個,則可以保證概率意義上年不重復。
??UUID(Universally Unique Identifier) : 全局唯一標識符,是指在一臺機器上生成的數(shù)字,它保證對在同一時空中的所有機器都是唯一的。按照開放軟件基金會(OSF)制定的標準計算,用到了以太網(wǎng)卡地址、納秒級時間、芯片ID碼和許多可能的數(shù)字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之后,過幾秒又生成一個UUID,則第一個部分不同,其余相同),時鐘序列,全局唯一的IEEE機器識別號(如果有網(wǎng)卡,從網(wǎng)卡獲得,沒有網(wǎng)卡以其他方式獲得),UUID的唯一缺陷在于生成的結果串會比較長。
1. 優(yōu)點
1.1. 隨機性
??從定義中可以看出,時間和隨機數(shù)的加入使得生成的UUID是基本隨機的。算法的核心思想是結合機器的網(wǎng)卡、當?shù)貢r間、一個隨即數(shù)來生成GUID。
1.2. 唯一性
??由于給予的IEEE機器識別號的唯一性,保證了不同設備的UUID也是不同的,而時間加隨機數(shù),從理論上講,如果一臺機器每秒產(chǎn)生10000000個GUID,則可以保證(概率意義上)3240年不重復。
2. 樣式
??UUID是由一個十六位的數(shù)字組成,表現(xiàn)出來的形式例如
550E8400-E29B-11D4-A716-446655440000
3. 適用范圍
適用于需要唯一的ID,例如數(shù)據(jù)庫主鍵,分布式系統(tǒng)的模塊ID等
4. 使用方法(Java)
UUID uuid = UUID.randomUUID(); String s = UUID.randomUUID().toString();
5. 代碼分析
/** * @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(); //s_id表示網(wǎng)卡地址,這個是固定的,所以只需要第一次調用定義就可以了 } 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(0, 8)); sb.append("-"); sb.append(raw.substring(8, 12)); sb.append("-"); sb.append(raw.substring(12, 16)); sb.append("-"); sb.append(raw.substring(16, 20)); sb.append("-"); sb.append(raw.substring(20)); return sb.toString(); }
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://www.ezyhdfw.cn/yun/66910.html
摘要:簡介的目的,是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識信息,而不需要通過中央控制端來做辨識信息的指定。總結由于參考的生成方式性能強勁,是和的到倍的。 簡介 UUID 的目的,是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識信息,而不需要通過中央控制端來做辨識信息的指定。 uuid 常用場景 IOT 設備,設備號; 網(wǎng)站 sessionid,cookie 用戶id; 數(shù)據(jù)庫主鍵id; ...
摘要:規(guī)范定義來自于發(fā)布的一個規(guī)范。其中的字母是進制表示,大小寫無關。在里面的使用的例子其中,最后的個字符就是我電腦網(wǎng)卡的地址版本安全的安全的和基于時間的算法相同,但會把時間戳的前位置換為的或。 一、簡介 UUID,是Universally Unique Identifier的縮寫,UUID出現(xiàn)的目的,是為了讓分布式系統(tǒng)可以不借助中心節(jié)點,就可以生成UUID來標識一些唯一的信息; GUID,...
摘要:什么是全稱,即通用唯一識別碼。目前最廣泛應用的,是微軟公司的全局唯一標識符,而其他重要的應用,則有文件系統(tǒng)加密分區(qū)等等。的唯一缺陷在于生成的結果串會比較長。關于這個標準使用最普遍的是微軟的。 什么是UUID? UUID全稱:Universally Unique Identifier,即通用唯一識別碼。 UUID是由一組32位數(shù)的16進制數(shù)字所構成,是故UUID理論上的總數(shù)為16^32 ...
摘要:用戶指定一個名字空間和一個字符串,通過散列,生成。字符串本身需要是唯一的。。雖然是基于隨機數(shù),但是重復的可能性可以忽略不計,因此該版本也是被經(jīng)常使用的版本。。當前正在使用的。。 UUID的生成策略: UUID的方式能生成一串唯一隨機32位長度數(shù)據(jù),它是無序的一串數(shù)據(jù),按照開放軟件基金會(OSF)制定的標準計算,UUID的生成用到了以太網(wǎng)卡地址、納秒級時間、芯片ID碼和許多可能的數(shù)字。U...
閱讀 3540·2021-10-18 13:33
閱讀 888·2019-08-30 14:20
閱讀 2681·2019-08-30 13:14
閱讀 2573·2019-08-29 18:38
閱讀 2937·2019-08-29 16:44
閱讀 1256·2019-08-29 15:23
閱讀 3585·2019-08-29 13:28
閱讀 1965·2019-08-28 18:00