How to use Java. util in JDK to generate a guid

Source: Internet
Author: User
Tags local time

Introduction to guid
The Open Software Foundation (OSF) has developed an algorithm that can generate unique identifiers to generate a globally unique identifier (UUID ). Microsoft uses the same algorithm in the Naming Standard of com! In COM, Microsoft renamed it globally unique identifier (guid ).
The GUID generation algorithm is based on the following aspects: 1. Current date and time. 2. network adapter card address. 3. clockwise. 4. Auto increment counter. The NIC addresses are different from each other. For machines without NICs, the addresses are unique to the machines in use.

GUID is actually a 128-bit long number, which is generally expressed in hexadecimal notation. The core idea of an algorithm 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.

 

Guid implementation

UUID is a new class in 1.5. in Java. util, it can generate a globally unique ID.

 

Code
Import java. util. UUID;
Public class test {
Public static void main (string [] ARGs ){
UUID = UUID. randomuuid ();
System. Out. println (UUID );
}
}

Import java. util. UUID;
Public class test {
Public static void main (string [] ARGs ){
UUID = UUID. randomuuid ();
System. Out. println (UUID );
}
}

 

 

There are also two ways to generate data on the Internet:

Import java.net .*;
Import java. util .*;
Import java. Security .*;

Public class guid extends object {

Private string seedingstring = "";

Private string rawguid = "";

Private Boolean bsecure = false;

Private Static random myrand;

Private Static securerandom mysecurerand;

Private Static string s_id;

Public static final int beforemd5 = 1;

Public static final int aftermd5 = 2;

Public static final int formatstring = 3;

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 GUID (){
}

/*
* Constructor with security option. Setting secure true enables each random
* Number generated to be cryptographically strong. Secure false defaults
* The standard random function seeded with a single cryptographically
* Strong random number.
*/
Public GUID (Boolean secure ){
Bsecure = secure;
}

/*
* Method to generate the random guid
*/
Private string getrandomguid (Boolean secure ){
Messagedigest MD5 = NULL;
Stringbuffer sbvaluebeforemd5 = new stringbuffer ();

Try {
MD5 = messagedigest. getinstance ("MD5 ");
} Catch (nosuchalgorithmexception e ){
System. Out. println ("error:" + E );
}

Try {
Long time = system. currenttimemillis ();
Long Rand = 0;

If (secure ){
Rand = mysecurerand. nextlong ();
} Else {
Rand = myrand. nextlong ();
}

// This stringbuffer can be a long as you need; the MD5
// Hash will always return 128 bits. You can change
// The seed to include anything you want here.
// You cocould even stream a file through the MD5 making
// The odds of guessing it at least as great as that
// Of guessing the contents of the file!
Sbvaluebeforemd5.append (s_id );
Sbvaluebeforemd5.append (":");
Sbvaluebeforemd5.append (long. tostring (time ));
Sbvaluebeforemd5.append (":");
Sbvaluebeforemd5.append (long. tostring (RAND ));

Seedingstring = sbvaluebeforemd5.tostring ();
Md5.update (seedingstring. getbytes ());

Byte [] array = md5.digest ();
Stringbuffer sb = new stringbuffer ();
For (Int J = 0; j <array. length; ++ J ){
Int B = array [J] & 0xff;
If (B <0x10)
SB. append ('0 ');
SB. append (integer. tohexstring (B ));
}

Rawguid = sb. tostring ();

} Catch (exception e ){
System. Out. println ("error:" + E );
}
Return rawguid;
}

Public String createnewguid (INT nformattype, Boolean secure ){
Getrandomguid (secure );
String sguid = "";
If (beforemd5 = nformattype ){
Sguid = This. seedingstring;
} Else if (aftermd5 = nformattype ){
Sguid = This. rawguid;
} Else {
Sguid = This. tostring ();
}
Return sguid;
}

Public String createnewguid (INT nformattype ){
Return this. createnewguid (nformattype, this. bsecure );
}

/*
* Convert to the standard format for GUID (useful for SQL Server
* Uniqueidentifiers, etc.) Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
Public String tostring (){
String raw = rawguid. touppercase ();
Stringbuffer sb = new stringbuffer ();
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 ();
}

Public static void main (string ARGs [])
{
For (INT I = 0; I <10; I ++)
{
Guid = new GUID ();
System. Out. println ("ID =" + guid. getrandomguid (true ));
}
 

}
}

Similarly

Java code
UUID = UUID. randomuuid ();
System. Out. println ("{" + UUID. tostring () + "}");

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 is calculated according to the standards set by the Open Software Foundation (OSF), and uses Ethernet Card addresses, nanoseconds, chip ID codes, and many possible numbers. The combination of the following parts: the current date and time (the first part of UUID is related to the time. If you generate a uuid in seconds, the first part is different and the rest are the same), the clock sequence, and the globally unique IEEE machine identification number (if there is a nic, it is obtained from the NIC, 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 ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.