GUID


GUID (全局唯一標識符)
全局唯一標識符(GUID,Globally Unique Identifier)是一種由算法生成的二進制長度為128位的數字標識符。GUID主要用於在擁有多個節點、多台計算機的網絡或系統中。在理想情況下,任何計算機和計算機集群都不會生成兩個相同的GUID。GUID 的總數達到了2^128(3.4×10^38)個,所以隨機生成兩個相同GUID的可能性非常小,但並不為0。GUID一詞有時也專指微軟對UUID標准的實現。
在理想情況下,任何計算機和計算機集群都不會生成兩個相同的GUID。隨機生成兩個相同GUID的可能性是非常小的,但並不為0。所以,用於生成GUID的算法通常都加入了非隨機的參數(如時間),以保證這種重復的情況不會發生。
在 Windows 平台上,GUID 廣泛應用於微軟的產品中,用於標識如注冊表項、類及接口標識、數據庫、系統目錄等對象。
中文名 全局唯一標識符 外文名 GUID,Globally Unique Identifier 類 型 數字標識符 作用於 多節點、多計算機的網絡或系統
目錄
▪ 格式
1 特點
2 爭議
3 程序
▪ VB
▪ C++
▪ Delphi
▪ C#
▪ Java
▪ OC
▪ Pascal
▪ 易語言
▪ PHP
▪ Excel VBA
格式
GUID 的格式為“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每個 x 是 0-9 或 a-f 范圍內的一個4位十六進制數。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即為有效的 GUID 值。
特點
★需要GUID的時候,可以完全由算法自動生成,不需要一個權威機構來管理。
★GUID理論上能產生全球唯一的值,對於以后的數據導入很方便。
爭議
由於GUID值產生的潛在弊端已經引起了保密性提倡者的關注。1999年3月,美國聯邦商務委員會接到要求,對微軟的GUID值使用進行調查。爭議主要涉及Office 97和Office 2000文檔對GUID值的使用。Office文檔,如Word文件或Excel電子數據表,所使用的GUID值對用戶是不可見的。但有很多報道宣稱,文檔的作者是可以通過GUID值的跟蹤查到的,即使作者已經采用特殊方法,他們還是可以被追蹤到。 為了回應上述問題,微軟已經發布了一個Office 97修補版SR2,它禁止了GUID功能的使用,並且還可以將現存文檔的GUID去除。
潛在的對Intel處理器序列號濫用的問題與上述GUID值的問題本質是一樣的。
程序
VB
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(8) As Byte
End Type
Private Declare Function CoCreateGuid Lib "ole32.dll" (pguid As GUID) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" (rguid As Any, ByVal lpstrClsId As Long, ByVal cbMax As Long) As Long
Private Function GUIDGen() As String '生成GUID
Dim uGUID As GUID
Dim sGUID As String
Dim bGUID() As Byte
Dim lLen As Long
Dim RetVal As Long
lLen = 40
bGUID = String(lLen, 0)
CoCreateGuid uGUID '把結構轉換為一個可顯示的字符串
RetVal = StringFromGUID2(uGUID, VarPtr(bGUID(0)), lLen)
sGUID = bGUID
If (Asc(Mid$(sGUID, RetVal, 1)) = 0) Then RetVal = RetVal - 1
GUIDGen = Left$(sGUID, RetVal)
End Function
C++
#include <objbase.h>
#include <stdio.h>
//--生成GUID
const char* newGUID()
{
static char buf[64] = {0};
GUID guid;
if (S_OK == ::CoCreateGuid(&guid))
{
_snprintf(buf, sizeof(buf)
, "{%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}"
, guid.Data1
, guid.Data2
, guid.Data3
, guid.Data4[0], guid.Data4[1]
, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
, guid.Data4[6], guid.Data4[7]
);
}
return (const char*)buf;
}
int main(int argc, char* argv[])
{
//--COM
CoInitialize(NULL);
printf(newGUID());
printf("\n");
//--COM
CoUninitialize();
return 0;
}
Delphi
uses ActiveX;
function ctGUID:string;
var
id:TGUID;
begin
CoCreateGuid(id);
Result:=GUIDToString(id);
end;
C#
using System;
namespace GUID測試 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
//產生一個新的GUID並輸出
Console.WriteLine(System.Guid.NewGuid()); 
Console.ReadKey(); 
} 
} 
}
Java
import java.util.UUID;
public class guid {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UUID uuid = UUID.randomUUID();
System.out.println(".{"+uuid.toString()+"}");
}
}
OC
NSString * get_uuid()
{
CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
CFRelease(uuid_ref);
NSString *uuid = [NSStringstringWithString:(NSString*)uuid_string_ref];
CFRelease(uuid_string_ref);
return uuid;
}
Pascal
【注:貌似上面的Delphi無法在free pascal下使用】
Function Guid_Gen:ansistring;
Var
s:string;
i:longint;
Begin
s:='0123456789abcdef';
//8-4-4-4-12
Guid_Gen:='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
for i:=1 to length(Guid_Gen) do begin
if Guid_Gen[i]='x' then Guid_Gen[i]:=s[Random(16)+1];
end;
End;
易語言
.版本 2

.DLL命令 CoCreateGuid, , "ole32", "CoCreateGuid"
.參數 guid, 字節集

.版本 2

.子程序 生成新的GUID, 文本型
.局部變量 guid, 字節集
.局部變量 a, 長整數型
.局部變量 b, 整數型
.局部變量 c, 整數型
.局部變量 s, 文本型
.局部變量 i, 整數型

guid = 取空白字節集 (16)
CoCreateGuid (guid)
a = 取字節集數據 (取字節集左邊 (guid, 4), #長整數型, )
b = 取字節集數據 (取字節集中間 (guid, 5, 2), #整數型, )
c = 取字節集數據 (取字節集中間 (guid, 7, 2), #整數型, )
s = “”
s = s + 取文本右邊 (“00000000” + 取十六進制文本 (a), 8) + “-”
s = s + 取文本右邊 (“0000” + 取十六進制文本 (b), 4) + “-”
s = s + 到小寫 (取文本右邊 (“0000” + 取十六進制文本 (c), 4)) + “-” ' 第三部分中的字母為小寫字母
.計次循環首 (8, i)
s = s + 取文本右邊 (“00” + 取十六進制文本 (guid [i + 8]), 2)
.如果真 (i = 2)
s = s + “-”
.如果真結束

.計次循環尾 ()
返回 (“{” + s + “}”)

PHP
public function create_guid(){ $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 6, 2).substr($charid, 4, 2).substr($charid, 2, 2).substr($charid, 0, 2).$hyphen .substr($charid, 10, 2).substr($charid, 8, 2).$hyphen .substr($charid,14, 2).substr($charid,12, 2).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12); return $uuid; }
Excel VBA
Function GetGuidToString() As String
GetGuidToString = LCase(Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36))
End Function

 

 


“簡單證明GUID(全局唯一標識符)並不唯一”

GUID是什么,大家理所當然地應該都知道(百度百科介紹的GUID)。在.net framework中,微軟為開發者提供了一個GUID結構,這個結構想必很多人在開發中應該已經用過,下面我們再來看一下它的備注說明:

“GUID 是一個 128 位整數(16 字節),可用於所有需要唯一標識符的計算機和網絡。此標識符重復的可能性非常小。”

注意紅色的標注,標識符是有重復的可能的,只不過重復的概率小之又小。到這里你我可能都會產生疑問,重復的可能性到底有多小呢?如何證明有重復呢?在stackoverflow上,一個善於思考勇於發現並提出問題挑戰權威的C#開發先驅拋出了一個有趣的問答題:“Simple proof that GUID is not unique”(本文的標題就是按照英文原文標題直譯過來的)。

在英文原文中,提問者說他想在一個測試程序中簡單證明一下GUID並不唯一,然后給出了如下代碼實現:

1
2
3
BigInteger begin = new BigInteger((long)0);
BigInteger end = new BigInteger("340282366920938463463374607431768211456",10); //2^128
for(begin; begin<end; begin++) Console.WriteLine(System.Guid.NewGuid().ToString());
令人感到遺憾的是,“it's not working”(實際上,拷貝這份代碼到VS中,編譯無法通過,BigInteger的構造函數根本不存在,for循環的地方寫得也不對,估計是偽代碼)。

接着,我們看到了投票次數最多的正確答案:

GuidCollisionDetector
樓豬第一次看到代碼的時候,精神抖擻熱情洋溢地分析如下:

1、定義一個字節數組reserveSomeRam,分配一塊內存空間;

2、通過HashSet對象填充GUID,直至內存不足,通過GC的KeepAlive和Collect方法,釋放出預留給reserveSomeRam的內存空間,保證程序有繼續運行的微小內存空間(此時,樓豬驚呼,好一段驚世駭俗奇技淫巧的NB代碼啊);

3、通過兩個for循環,配合並行庫,通過HashSet的Contains函數證明新產生的GUID有可能產生重復(這里主要就是CPU運算的事情了,完全用不到reserveSomeRam那一塊的內存,充分地利用了CPU和內存,這種思想這種境界真是令人感到匪夷所思望塵莫及,牛)。

但是看到代碼中的“throw new ApplicationException("Guids collided! Oh my gosh!");”和Console.WriteLine("Umm... why hasn't the universe ended yet?");,樓豬有一種穿越的感覺。

接着樓豬仔細看了一下這個正確答案的正文回答的細節,發現這家伙從頭到尾都是一種煞有介事一本正經的口氣,又是版權,又是要錢,又是坐着時光機回到2010年2月28號獲得技術支持的…恍然大悟,kao,真是TM的太好玩太會扯淡了。

忍耐不住好奇,懷着強烈的求知欲望,樓豬看完了所有回答,有人用數學方法證明…祝你好運;有人寄希望於未來的量子計算機顯靈;有人提議組織志願者現在就開始他們偉大的136年證明之旅;有人提議升級顯卡,NVIDIA 可能會貸款贊助這個歷史性的計算;有人說他可以幫忙,已經被證明了,他曾經獲得過某一個GUID數字……樓豬久違地又蛋疼了。

比較起來,個人感覺還是這個回答比較靠譜:

Well if the running time of 83 billion years does not scare you, think that you will also need to store the generated GUIDs somewhere to check if you have a duplicate; storing 2^128 16-byte numbers would only require you to allocate 4951760157141521099596496896 terabytes of RAM upfront, so imagining you have a computer which could fit all that and that you somehow find a place to buy terabyte DIMMs at 10 grams each, combined they will weigh more than 8 Earth masses, so you can seriously shift it off the current orbit, before you even press "Run". Think twice!

大致意思就是說,跑完證明程序,需要大概830億年時間和4951760157141521099596496896 TB(1TB=1024GB)的內存空間(假設每個DIMM內存有10克重,所有的內存換算成重量,大概是8個地球的重量之和)。從看似有限而又無限的時間和空間上證明,GUID重復這種概率發生的可能性實在是太太太小了,可以認為基本不可能。有一個回復說,“Personally, I think the "Big Bang" was caused when two GUIDs collided.”,即:兩個GUID重復之日,宇宙大爆炸之時。

實際上,樓豬現在也是這么認為的。
您有好的方法證明GUID會重復嗎?

 


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(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();
}


// 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)。

 


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

復制代碼 代碼如下:
package com.cn.str;
import java.util.UUID;
/**
* Create GUID
* @author Administrator
*
*/
public class CreateGUID {

public static final String GenerateGUID(){
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(GenerateGUID());
}
}

UUID是1.5中新增的一個類,在java.util下,用它可以產生一個號稱全球唯一的ID


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM