How to generate GUID and UUID with Java

xiaoxiao2021-03-05  46

Generate GUID and UUID in two ways

Need a comm log library

/ ** * @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 divurerandom mysecurerand;

Private static string s_id; private static final int line_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 the mobile limit per jvm installation. * /

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 lors 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.totring (rand));

ValuebeforeMD5 = sbvaluebeforemd5.toString (); md5.update (valuebeforeormd5.getbytes);

Byte [] array = md5.digest (); stringbuffer sb = new stringbuffer (32); for (int J = 0; j

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 Strin = " myguid.valuebeforeMD5); system.out.println (" Rawguid = " myguid.valueaftermd5; system.out.println (" randomguid = " myguid.toString ());}}

}

same

UUID UUID = uuid.randomuiD (); System.out.println ("{" uuid.tostring () "}");

UUID refers to the numbers generated on a machine, which guarantees that all machines in the sky are unique. The usual platform will provide an API that generates UUID. UUID is calculated according to the standards developed by the Open Software Foundation (OSF), which uses the Ethernet card address, nanosecond time, chip ID code, and many possible numbers. The combination of the following sections: the current date and time (the first part of the UUID is related to the time, if you generate a UUID, generate a UUID in a few seconds, the first part is different, the rest of the same), the clock Sequence, the only IEEE machine identifier (if there is a NIC, get from the NIC, no network card is obtained in other ways), the unique defect of UUID is that the resulting result is relatively long. About UUID This standard is the most common use of Microsoft's GUID (Globals Unique Identifiers).

转载请注明原文地址:https://www.9cbs.com/read-32888.html

New Post(0)