问题
I am wondering, what goes into the creation of a GUID. I don't mean what is used to create a GUID in a specific language (NewID() in SQL Server, Guid.NewGuid() in C#), I mean when you call those methods/functions, what do they do to make the GUID?
回答1:
Also, RFC 4122 (which is referenced in the Wikipedia article) describes how GUIDs should be built.
回答2:
The details of GUIDs, including the algorithm used to generate them is described on wikipedia.
回答3:
In short, it's not complicated at all. GUID (or UUID) Version 4 (current) is a partially random number, plain and simple (122 out of 128 bits are random, the rest are used for storing version and revision). The trick is that the possible values of this number are so many that the probability of a hit is for most practical purposes, zero.
回答4:
Hash function. Its complicated.
http://en.wikipedia.org/wiki/GUID#Algorithm Knows more than I do.
回答5:
A word of caution that a very great deal of what you read on the Internet about GUID creation may well be wrong, or at least out of date for your specific platform.
I once single-stepped through a heap of Windows code to settle an argument about GUID creation on WinXP. Unfortunately, it turned out that I was wrong (i.e. I lost the argument), but so was Larry Osterman, so I felt slightly better about it.
回答6:
There are five official ways of generating GUID's (and certainly many more unofficial ones).
Version 1 is a time based GUID usually using MAC addresses of the primary network card of the using used to compute the GUID. This is normally not used due to privacy issues, but I do believe that Microsoft SQL Servers from 2005 and onwards use a modified version of this (claiming to be version 14), to create sequential GUID's useful for id's in a database to avoid fractioning of data blocks (NewSequentialId()).
Version 2 is DCE Security version. I have never found this kind of GUID, but I have not worked a lot with POSIX either and there seems to be a connection between version 2 GUID's and POSIX.
Version 3 is a "name based" version, meaning you can take a text and create a GUID representation of that, given a namespace. Version 3 uses a MD5 hashing algorithm. See also version 5.
Version 4 is basically a random number type GUID. The random number is of sequrity level, not just your average random number generator though. This is the version usually used in the world today. The C# Guid.NewGuid() uses this version, according to Microsoft documentation. Also the normal function for generating an uniqueidentifier in MS SQL Server (NewId()) generates a version 4 GUID.
Version 5 is just like version 3, but uses a SHA-3 hashing algorithm instead. The extended guid C# project uses the version 5 algorithm.
For one implementation of GUID making I'd recomend looking at the extended guid project. As many has pointed out the RFC 4122 gives a detailed description on how all five algorithms work. However, there are no guarantees all implementations are correct.
来源:https://stackoverflow.com/questions/87127/guid-behind-the-scenes