UUID generator of 32 bytes long values : UUID GUID « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » UUID GUIDScreenshots 
UUID generator of 32 bytes long values
     
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * UUID generator of 32 bytes long values. It is builded from:
 * <ol>
 * <li> (0-7) IPAddress as HEX - 8 bytes
 * <li> (8-19) CurrentTimeMillis() as HEX - Display all 12 bytes
 * <li> (20-23) SecureRandom() as HEX - Keep only 4 significant bytes. Since this is "random" it doesn't really matter how many bytes you keep or eliminate
 * <li> (24-31) System.identityHashCode as Hex - 8 bytes
 * </ol>
 */
public class Uuid32Generator {

  public static String generateUUID() {
    return new Uuid32Generator().generate();
  }

  private static final String ZEROS = "000000000000"// 12

  public String generate() {
    StringBuilder strRetVal = new StringBuilder();
    String strTemp;
    try {
      // IPAddress segment
      InetAddress addr = InetAddress.getLocalHost();
      byte[] ipaddr = addr.getAddress();
      for (byte anIpaddr : ipaddr) {
        Byte b = new Byte(anIpaddr);
        strTemp = Integer.toHexString(b.intValue() 0x000000ff);
        strRetVal.append(ZEROS.substring(0- strTemp.length()));
        strRetVal.append(strTemp);
      }
      strRetVal.append(':');

      // CurrentTimeMillis() segment
      strTemp = Long.toHexString(System.currentTimeMillis());
      strRetVal.append(ZEROS.substring(012 - strTemp.length()));
      strRetVal.append(strTemp).append(':');

      // random segment
      SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
      strTemp = Integer.toHexString(prng.nextInt());
      while (strTemp.length() 8) {
        strTemp = '0' + strTemp;
      }
      strRetVal.append(strTemp.substring(4)).append(':');

      // IdentityHash() segment
      strTemp = Long.toHexString(System.identityHashCode(this));
      strRetVal.append(ZEROS.substring(0- strTemp.length()));
      strRetVal.append(strTemp);
    catch (UnknownHostException uhex) {
      throw new RuntimeException("Unknown host.", uhex);
    catch (NoSuchAlgorithmException nsaex) {
      throw new RuntimeException("Algorithm 'SHA1PRNG' is unavailiable.", nsaex);
    }
    return strRetVal.toString().toUpperCase();
  }

}

   
    
    
    
    
  
Related examples in the same category
1.Random GUID
2.Algorithm for generating Random GUID
3.Get a unique identifier Using java.rmi.dgc.VMID
4.Using java.util.UUID
5.Create your own basic UUID
6.Session ID generator
7.UUID generator from Sun Microsystems
8.UUID generator from http://www1.ics.uci.edu
9.Using java.util.concurrent.AtomicLong: A numerical id, start at zero and increment by one.
10.A 32 byte GUID generator
11.A unique identifier
12.Generate pseudo-GUID sequences
13.Generates a UUID
14.Generates random UUIDs
15.Generator for Globally unique Strings
16.ID generator
17.Simple Id Generator
18.UUID generation
19.Simple Long Id Generator
20.Long Sequence Generator
21.UUID generator
22.Thread-safe version of the Axiom UUIDGenerator
23.Convert an array of bytes into a List of Strings using UTF-8.
24.Your own UUID
25.Your own UUID 2
26.Create GUIDs according to the principles at http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt.
27.Get Unique Id
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.