GUID is a 128-bit long number, which is generally expressed in hexadecimal notation.AlgorithmThe core idea is to combine the machine's Nic, local time, and a random number to generate a guid. Theoretically, if a machine generates 10000000 guids per second, it can ensure (in probability) that there will be no duplicates in 3240.
UUID is a new class in 1.5. in Java. util, it can generate a globally unique ID.
Package com. mytest;
Import java. util. UUID;
Public class Utest {
Public static void main (string [] ARGs ){
UUID = UUID. randomuuid ();
System. Out. println (UUID );
}
}
UUID (universally unique identifier)
A globally unique identifier is a number generated on a machine. It ensures that all machines in the same time and space are unique. According to the standards set by the Open Software Foundation (OSF), the ethernet card is used.
Address, Nanosecond-level time, chip ID code, and many possible numbers. The combination of the following parts: current date and time (the first part of UUID is related to time. If you generate a uuid,
After several seconds, a uuid is generated, the first part is different, and the rest is the same), the clock sequence, and the globally unique IEEE machine identification number (if there is a nic, it is obtained from the NIC, no Nic, otherwise
The only defect of UUID is that the generated result string is long.
InJavaUUID generation in the following ways:
Jdk1.5
If JDK is used, it is easy to generate a uuid, so that JDK implements the uuid:
Java. Util. UUID, which can be called directly.
UUID = UUID. randomuuid ();
String S = UUID. randomuuid (). tostring (); // The primary key ID used to generate the database is very good ..
UUID is composed of Sixteen digits.
550e8400-e29b-11d4-a716-446655440000
// The following describes how to obtain a unique primary key ID for the database. Code
Public class uuidgenerator {
Public uuidgenerator (){
}
/**
* Obtain a uuid
* @ Return string UUID
*/
Public static string getuuid (){
String S = UUID. randomuuid (). tostring ();
// Remove the "-" symbol
Return S. substring () + S. substring () + S. substring (24 );
}
/**
* Obtain the specified UUID.
* @ Param number int the number of UUID to be obtained
* @ Return string [] UUID Array
*/
Public static string [] getuuid (INT number ){
If (number <1 ){
Return NULL;
}
String [] Ss = new string [number];
For (INT I = 0; I <number; I ++ ){
Ss [I] = getuuid ();
}
Return SS;
}
Public static void main (string [] ARGs ){
String [] Ss = getuuid (10 );
For (INT I = 0; I <ss. length; I ++ ){
System. Out. println (ss [I]);
}
}
}
Two methods are used to generate guid and UUID
Comm log library required
/**
* @ Author Administrator
*
* Todo to change the template for this generated type comment go
* 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 ();
}< br> 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 ());
}
}
}
Similarly
UUID = UUID. randomuuid ();
System. Out. println ("{" + UUID. tostring () + "}");
UUID refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides APIs for UUID generation. UUID follows the Open Software Fund
Will (OSF) the standard calculation, uses the ethernet card address, the nanosecond level time, the chip ID code and many possible numbers. The combination of the following parts: current date and time (the first part of UUID
Related to time. If you generate a uuid after several seconds, the first part is different and the rest are the same.), clock sequence, globally Unique IEEE machine identification number (if
Network Card, obtained from the network card, without the network card in other ways), the unique defect of UUID is that the generated result string will be relatively long. The most common use of UUID standards is Microsoft's
GUID (globals unique identifiers ).
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiajing12345/archive/2005/04/22/358976.aspx
UUID indicates a universal unique identifier.
It is a software construction standard and is organized by the Open Software Foundation (OSF) in the distributed computing environment.
(Distributed computing environment, DCE. UUID
The purpose is to allow all elements in the distributed system to have unique identification information without specifying the information through the central control terminal. In this way, everyone can establish a non-conflict with others.
UUID. In this case, duplicate names are not required during database creation. At present, the most widely used UUID is Microsoft's globally
Unique identifiers (guids), while other important applications include Linux ext2/ext3 file system, luks
Encryption partition, gnome, Kde, Mac OS X, and so on.
The following is an example of UUID generation:
View plaincopy to clipboardprint?
Package test;
Import java. util. UUID;
Public class uuidgenerator {
Public uuidgenerator (){
}
Public static string getuuid (){
UUID = UUID. randomuuid ();
String STR = UUID. tostring ();
// Remove the "-" symbol
String temp = Str. substring (0, 8) + Str. substring (9, 13) +
Str. substring (14, 18) + Str. substring (19, 23) + Str. substring (24 );
Return STR + "," + temp;
}
// Obtain the specified number of UUID
Public static string [] getuuid (INT number ){
If (number <1 ){
Return NULL;
}
String [] Ss = new string [number];
For (INT I = 0; I <number; I ++ ){
Ss [I] = getuuid ();
}
Return SS;
}
Public static void main (string [] ARGs ){
String [] Ss = getuuid (10 );
For (INT I = 0; I <ss. length; I ++ ){
System. Out. println ("SS [" + I + "] ====" + SS [I]);
}
}
}
Package test;
Import java. util. UUID;
Public class uuidgenerator {
Public uuidgenerator (){
}
Public static string getuuid (){
UUID = UUID. randomuuid ();
String STR = UUID. tostring ();
// Remove the "-" symbol
String temp = Str. substring (0, 8) + Str. substring (9, 13) + Str. substring (14, 18) + Str. substring (19, 23) + Str. substring (24 );
Return STR + "," + temp;
}
// Obtain the specified number of UUID
Public static string [] getuuid (INT number ){
If (number <1 ){
Return NULL;
}
String [] Ss = new string [number];
For (INT I = 0; I <number; I ++ ){
Ss [I] = getuuid ();
}
Return SS;
}
Public static void main (string [] ARGs ){
String [] Ss = getuuid (10 );
For (INT I = 0; I <ss. length; I ++ ){
System. Out. println ("SS [" + I + "] ====" + SS [I]);
}
}
}
Result:
View plaincopy to clipboardprint?
Ss [0] ==== 4cdbc040-657a-4847-b266-7e31d9e2c3d9, 4cdbc040657a4847b2667e31d9e2c3d9
Ss [1] ==== 72297c88-109-4c05-9b05-d28bfb11d10b, 72297c881094c059b05d28bfb11d10b
Ss [2] ==== 6d513b6a-69bd-4f79-b94c-d65fc841ea95, 6d513b6a69bd4f79b94cd65fc841ea95
Ss [3] ==== d897a7d3-87a3-4e38-9e0b-71013a6dbe4c, d897a7d387a34e389e0b71013a6dbe4c
Ss [4] ==== 5709f0ba-31e3-42bd-a28d-03485b257c94, 5709f0ba31e342bda28d03485b257c94
Ss [5] ==== 530fbb8c-eec9-48d1-ae1b-5f792daf09f3, 530fbb8ceec948d1ae1b5f792daf09f3
Ss [6] ==== 4bf07297-65b2-45ca-b905-6fc6f2f39158, 4bf0729765b245cab9056fc6f2f39158
Ss [7] ==== 6e5a0e85-b4a0-485f-be54-a758115317e1, 6e5a0e85b4a0485fbe54a758115317e1
Ss [8] ==== 245accec-3c12-4642-967f-e476cef558c4, 245accec3c124642967fe476cef558c4
Ss [9] ==== ddd4b5a9-fecd-446c-bd78-63b70bb500a1, ddd4b5a9fe9806cbd7863b70bb500a1
Ss [0] ==== 4cdbc040-657a-4847-b266-7e31d9e2c3d9, 4cdbc040657a4847b2667e31d9e2c3d9
Ss [1] ==== 72297c88-109-4c05-9b05-d28bfb11d10b, 72297c881094c059b05d28bfb11d10b
Ss [2] ==== 6d513b6a-69bd-4f79-b94c-d65fc841ea95, 6d513b6a69bd4f79b94cd65fc841ea95
Ss [3] ==== d897a7d3-87a3-4e38-9e0b-71013a6dbe4c, d897a7d387a34e389e0b71013a6dbe4c
Ss [4] ==== 5709f0ba-31e3-42bd-a28d-03485b257c94, 5709f0ba31e342bda28d03485b257c94
Ss [5] ==== 530fbb8c-eec9-48d1-ae1b-5f792daf09f3, 530fbb8ceec948d1ae1b5f792daf09f3
Ss [6] ==== 4bf07297-65b2-45ca-b905-6fc6f2f39158, 4bf0729765b245cab9056fc6f2f39158
Ss [7] ==== 6e5a0e85-b4a0-485f-be54-a758115317e1, 6e5a0e85b4a0485fbe54a758115317e1
Ss [8] ==== 245accec-3c12-4642-967f-e476cef558c4, 245accec3c124642967fe476cef558c4
Ss [9] ==== ddd4b5a9-fecd-446c-bd78-63b70bb500a1, ddd4b5a9fe9806cbd7863b70bb500a1
It can be seen that UUID refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides the generated API. According to the standards set by the Open Software Foundation (OSF), ethernet card addresses, nanoseconds, chip ID codes, and many possible numbers are used.
UUID consists of the following parts:
(1) The current date and time. The first part of UUID is related to the time. If you generate a uuid after several seconds and then generate a uuid, the first part is different, the rest are the same.
(2) Clock Sequence
(3) Globally Unique IEEE machine identification number. If there is a nic, it is obtained from the nic mac address, and no Nic is obtained in other ways.
The unique defect of UUID is that the generated result string is long. The most common UUID standard is Microsoft's guid (globals unique
Identifiers ). In ColdFusion, you can use the createuuid () function to generate a uuid in the format of XXXXXXXX-XXXX-
XXXX-xxxxxxxxxxxxxxxx (8-4-4-16), where each X is 0-9 or a-f
A hexadecimal number in the range. The standard UUID format is: XXXXXXXX-XXXX-xxxxxx-xxxxxxxxxx
(8-4-4-4-12), you can download createguid () UDF from cflib for conversion.
Benefits of using UUID in distributed software systems (such as DCE/RPC,
COM +, CORBA), which ensures that the IDS generated by each node are not repeated. With the development of integration technologies such as Web Services, the advantages of UUID will become more obvious. Enable
UUID must not only ensure that they are different from each other, but also have a very large difference with any other universal unique identifier generated before January 1, 3400 AD.
A universal unique identifier can also be used to point to most possible objects. Microsoft and some other software companies tend to use globally unique identifiers (guid), which is also a type of generic unique identifiers.
Type, which can be used to point to the object module object and other software components. The first universal unique identifier was created in the networked computer system (NCS) and subsequently became the Open Software Foundation (OSF)
Distributed Computing Environment (DCE) components.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/carefree31441/archive/2009/03/17/3998553.aspx