GUID
GUID (全局唯一標識符)
全局唯一標識符(GUID,Globally Unique Identifier)是一種由算法生成的二進制長度為128位的數(shù)字標識符。GUID主要用于在擁有多個節(jié)點、多臺計算機的網(wǎng)絡(luò)或系統(tǒng)中。在理想情況下,任何計算機和計算機集群都不會生成兩個相同的GUID。GUID 的總數(shù)達到了2^128(3.4×10^38)個,所以隨機生成兩個相同GUID的可能性非常小,但并不為0。GUID一詞有時也專指微軟對UUID標準的實現(xiàn)。
在理想情況下,任何計算機和計算機集群都不會生成兩個相同的GUID。隨機生成兩個相同GUID的可能性是非常小的,但并不為0。所以,用于生成GUID的算法通常都加入了非隨機的參數(shù)(如時間),以保證這種重復的情況不會發(fā)生。
在 Windows 平臺上,GUID 廣泛應用于微軟的產(chǎn)品中,用于標識如注冊表項、類及接口標識、數(shù)據(jù)庫、系統(tǒng)目錄等對象。
中文名 全局唯一標識符 外文名 GUID,Globally Unique Identifier 類 型 數(shù)字標識符 作用于 多節(jié)點、多計算機的網(wǎng)絡(luò)或系統(tǒng)
目錄
? 格式
1 特點
2 爭議
3 程序
? VB
? C++
? Delphi
? C#
? Java
? OC
? Pascal
? 易語言
? PHP
? Excel VBA
格式
GUID 的格式為“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每個 x 是 0-9 或 a-f 范圍內(nèi)的一個4位十六進制數(shù)。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即為有效的 GUID 值。
特點
★需要GUID的時候,可以完全由算法自動生成,不需要一個權(quán)威機構(gòu)來管理。
★GUID理論上能產(chǎn)生全球唯一的值,對于以后的數(shù)據(jù)導入很方便。
爭議
由于GUID值產(chǎn)生的潛在弊端已經(jīng)引起了保密性提倡者的關(guān)注。1999年3月,美國聯(lián)邦商務委員會接到要求,對微軟的GUID值使用進行調(diào)查。爭議主要涉及Office 97和Office 2000文檔對GUID值的使用。Office文檔,如Word文件或Excel電子數(shù)據(jù)表,所使用的GUID值對用戶是不可見的。但有很多報道宣稱,文檔的作者是可以通過GUID值的跟蹤查到的,即使作者已經(jīng)采用特殊方法,他們還是可以被追蹤到。 為了回應上述問題,微軟已經(jīng)發(fā)布了一個Office 97修補版SR2,它禁止了GUID功能的使用,并且還可以將現(xiàn)存文檔的GUID去除。
潛在的對Intel處理器序列號濫用的問題與上述GUID值的問題本質(zhì)是一樣的。
程序
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 '把結(jié)構(gòu)轉(zhuǎn)換為一個可顯示的字符串
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)
{
//產(chǎn)生一個新的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"
.參數(shù) guid, 字節(jié)集
.版本 2
.子程序 生成新的GUID, 文本型
.局部變量 guid, 字節(jié)集
.局部變量 a, 長整數(shù)型
.局部變量 b, 整數(shù)型
.局部變量 c, 整數(shù)型
.局部變量 s, 文本型
.局部變量 i, 整數(shù)型
guid = 取空白字節(jié)集 (16)
CoCreateGuid (guid)
a = 取字節(jié)集數(shù)據(jù) (取字節(jié)集左邊 (guid, 4), #長整數(shù)型, )
b = 取字節(jié)集數(shù)據(jù) (取字節(jié)集中間 (guid, 5, 2), #整數(shù)型, )
c = 取字節(jié)集數(shù)據(jù) (取字節(jié)集中間 (guid, 7, 2), #整數(shù)型, )
s = “”
s = s + 取文本右邊 (“00000000” + 取十六進制文本 (a), 8) + “-”
s = s + 取文本右邊 (“0000” + 取十六進制文本 (b), 4) + “-”
s = s + 到小寫 (取文本右邊 (“0000” + 取十六進制文本 (c), 4)) + “-” ' 第三部分中的字母為小寫字母
.計次循環(huán)首 (8, i)
s = s + 取文本右邊 (“00” + 取十六進制文本 (guid [i + 8]), 2)
.如果真 (i = 2)
s = s + “-”
.如果真結(jié)束
.計次循環(huán)尾 ()
返回 (“{” + 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中,微軟為開發(fā)者提供了一個GUID結(jié)構(gòu),這個結(jié)構(gòu)想必很多人在開發(fā)中應該已經(jīng)用過,下面我們再來看一下它的備注說明:
“GUID 是一個 128 位整數(shù)(16 字節(jié)),可用于所有需要唯一標識符的計算機和網(wǎng)絡(luò)。此標識符重復的可能性非常小。”
注意紅色的標注,標識符是有重復的可能的,只不過重復的概率小之又小。到這里你我可能都會產(chǎn)生疑問,重復的可能性到底有多小呢?如何證明有重復呢?在stackoverflow上,一個善于思考勇于發(fā)現(xiàn)并提出問題挑戰(zhàn)權(quán)威的C#開發(fā)先驅(qū)拋出了一個有趣的問答題:“Simple proof that GUID is not unique”(本文的標題就是按照英文原文標題直譯過來的)。
在英文原文中,提問者說他想在一個測試程序中簡單證明一下GUID并不唯一,然后給出了如下代碼實現(xiàn):
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的構(gòu)造函數(shù)根本不存在,for循環(huán)的地方寫得也不對,估計是偽代碼)。
接著,我們看到了投票次數(shù)最多的正確答案:
GuidCollisionDetector
樓豬第一次看到代碼的時候,精神抖擻熱情洋溢地分析如下:
1、定義一個字節(jié)數(shù)組reserveSomeRam,分配一塊內(nèi)存空間;
2、通過HashSet對象填充GUID,直至內(nèi)存不足,通過GC的KeepAlive和Collect方法,釋放出預留給reserveSomeRam的內(nèi)存空間,保證程序有繼續(xù)運行的微小內(nèi)存空間(此時,樓豬驚呼,好一段驚世駭俗奇技淫巧的NB代碼啊);
3、通過兩個for循環(huán),配合并行庫,通過HashSet的Contains函數(shù)證明新產(chǎn)生的GUID有可能產(chǎn)生重復(這里主要就是CPU運算的事情了,完全用不到reserveSomeRam那一塊的內(nèi)存,充分地利用了CPU和內(nèi)存,這種思想這種境界真是令人感到匪夷所思望塵莫及,牛)。
但是看到代碼中的“throw new ApplicationException("Guids collided! Oh my gosh!");”和Console.WriteLine("Umm... why hasn't the universe ended yet?");,樓豬有一種穿越的感覺。
接著樓豬仔細看了一下這個正確答案的正文回答的細節(jié),發(fā)現(xiàn)這家伙從頭到尾都是一種煞有介事一本正經(jīng)的口氣,又是版權(quán),又是要錢,又是坐著時光機回到2010年2月28號獲得技術(shù)支持的…恍然大悟,kao,真是TM的太好玩太會扯淡了。
忍耐不住好奇,懷著強烈的求知欲望,樓豬看完了所有回答,有人用數(shù)學方法證明…祝你好運;有人寄希望于未來的量子計算機顯靈;有人提議組織志愿者現(xiàn)在就開始他們偉大的136年證明之旅;有人提議升級顯卡,NVIDIA 可能會貸款贊助這個歷史性的計算;有人說他可以幫忙,已經(jīng)被證明了,他曾經(jīng)獲得過某一個GUID數(shù)字……樓豬久違地又蛋疼了。
比較起來,個人感覺還是這個回答比較靠譜:
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)的內(nèi)存空間(假設(shè)每個DIMM內(nèi)存有10克重,所有的內(nèi)存換算成重量,大概是8個地球的重量之和)。從看似有限而又無限的時間和空間上證明,GUID重復這種概率發(fā)生的可能性實在是太太太小了,可以認為基本不可能。有一個回復說,“Personally, I think the "Big Bang" was caused when two GUIDs collided.”,即:兩個GUID重復之日,宇宙大爆炸之時。
實際上,樓豬現(xiàn)在也是這么認為的。
您有好的方法證明GUID會重復嗎?
?
GUID是一個128位長的數(shù)字,一般用16進制表示。算法的核心思想是結(jié)合機器的網(wǎng)卡、當?shù)貢r間、一個隨機數(shù)來生成GUID。從理論上講,如果一臺機器每秒產(chǎn)生10000000個GUID,則可以保證(概率意義上)3240年不重復。
UUID是1.5中新增的一個類,在java.util下,用它可以產(chǎn)生一個號稱全球唯一的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是指在一臺機器上生成的數(shù)字,它保證對在同一時空中的所有機器都是唯一的。通常平臺會提供生成UUID的API。UUID按照開放軟件基金會(OSF)制定的標準計算,用到了以太網(wǎng)卡地址、納秒級時間、芯片ID碼和許多可能的數(shù)字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關(guān),如果你在生成一個UUID之后,過幾秒又生成一個UUID,則第一個部分不同,其余相同),時鐘序列,全局唯一的IEEE機器識別號(如果有網(wǎng)卡,從網(wǎng)卡獲得,沒有網(wǎng)卡以其他方式獲得),UUID的唯一缺陷在于生成的結(jié)果串會比較長。關(guān)于UUID這個標準使用最普遍的是微軟的GUID(Globals Unique Identifiers)。
?
GUID是一個128位長的數(shù)字,一般用16進制表示。算法的核心思想是結(jié)合機器的網(wǎng)卡、當?shù)貢r間、一個隨即數(shù)來生成GUID。從理論上講,如果一臺機器每秒產(chǎn)生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下,用它可以產(chǎn)生一個號稱全球唯一的ID
轉(zhuǎn)載于:https://www.cnblogs.com/hanruyue/p/5949019.html
總結(jié)
- 上一篇: vue模拟加载更多功能(数据追加)
- 下一篇: 移动应用开发过程中的迭代式原型设计