uuid java class source code analysis

Universally Unique Identifier (English: Universally Unique Identifier, referred to as the UUID) is a software standard construction, is also a part of the organization Free Software Foundation in the field of distributed computing environments.
UUID aim is to make all the elements of a distributed system, can have a unique identification information, identification information without having to specify done by a central control terminal. In this way, everyone can create a UUID does not conflict with other people.
UUID group, by a string of 16 bits (also known as 128) is composed of 16 digits, is theoretically the total number of actual occurrence UUID 216 x 8 = 2128, equal to about 3.4 x 1038. That is if UUID generate 1 trillion per nanosecond, it takes 10 billion years to run all the UUID. There is no need to consider its repeatability.
UUID standard pattern 16 contains 32 digits, hyphens divided into five sections, the form of a 8-4-4-4-12 of 32 characters, with "-" a total of 36 bits , so we can first removed uuid, and then "-" removed.

import java.util.UUID;
import org.apache.commons.lang3.RandomStringUtils;

public class RandomUtils {
    public RandomUtils() {
    }

    public static String generateTicket() {
        String ticket = UUID.randomUUID().toString();
        return ticket.replaceAll("-", "");
    }

    public static String generateRandomString(int count) {
        return RandomStringUtils.random(count, true, true);
    }

    public static String generateRandomNum(int count) {
        return RandomStringUtils.random(count, false, true);
    }

    public static String generateRandomFileName() {
        return String.join("", generateTicket(), generateRandomString(6));
    }
}

 

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

 





public static void main(String[] args) {
    byte[] b = {0};
    System.out.println(UUID.randomUUID());
    System.out.println(UUID.randomUUID());
    System.out.println(UUID.nameUUIDFromBytes(b));
    System.out.println(UUID.nameUUIDFromBytes(b));
}

The top is the code below is the result

8844-83c9a6d22aa6-Feb860b4-9bc2-4bab
09289276-0af0-4ee8-9e14-E7a0df30aeb8
93b885ad-Fe0d-3089-8df6-34904fd59f71
93b885ad-Fe0d-3089-8df6-34904fd59f71

More obvious is randomUUID () is still not repeated,

But nameUUIDFromBytes () has been repeated

Analysis: See source is byte [] is generated MD5 uuid, i.e. according to byte [] generated uuid, so that the same byte [] returns the result must be the same.



Article Directory
One, explain
two, using the example
three principles outlined
four or source code parsing
five clever but useless
VI, references
a, explaining
Wikipedia
UUID is a certain format to meet the 8-4-4-4-12 this formats, such as the following UUID:

6b349832-0470-4692-befd-6037b280bbc5

UUID of 32 alphanumeric characters (including no hyphen), each character is a hexadecimal number (0-f). UUID has some structure:

Note: with a hexadecimal nibble, a byte is equal to two half-bytes, one byte equals eight bits, nibbles equal to 4 bits.
UUID generated by:

"Version 1" UUID is a time and node ID (typically a MAC address) generated;

"Version 2" UUID is (usually a user or group ID), and the node ID generating time based on the identifier;

"Version 3" and "Version 5" certainty UUID identifier and name generated by a hash (hashing) name space (namespace);

"Version 4" UUID generated using randomness or pseudo-randomness.

Second, using the example

public static void main(String[] args) {
        String uuid="";
        uuid=UUID.randomUUID().toString();
        System.out.println(uuid);
    }

result:

60cc1ff0-4b30-4e35-a0a6-940934ac756b

Yes, generated in java  UUID  is that simple.

Third, the principles outlined

public  static  void main (String [] args) {
         byte [] = randomBytes new new  byte [16]; // array each element is 0, it is necessary to generate a random 16-byte 
        a SecureRandom secureRandom = new new a SecureRandom (); 
        secureRandom. engineNextBytes (randomBytes); 
        System.out.println (DatatypeConverter.printHexBinary (randomBytes)); 


        / ** 
         * RFC 4122 in order to meet specifications, it is necessary to set some parameters of the random data of 16 bytes. Here we take to generate a random UUID 
         * randomly generated "Version 4" UUID. Like other UUID, 4-bit indicating "version 4", 2-bit or 3-bit for indicating variant (variant) (10 or 110, respectively, for variants 1 and 2). 
         * Thus, for a variant (i.e., most of the UUID), random "version 4" having the UUID 6 variants and versions of a predetermined bit, leaving a portion 122 of randomly generated, "version 4" variant 1 the UUID 122 may be a total power of 2.
         * "Version 4" variant 2 UUID (conventional GUID) of perhaps half, because at least one random bit is available, the variable consumption of 3 bits. 
         * / 

    } 
}

operation result:

82D68D2051EAF9610D36C33642E02594

Produce an array size of 16 bytes (16 hexadecimal is converted to 32 characters)
for the array assignment (because the initial array generated by each element is 0)
is actually here, our unique id is created. UUID just to meet certain specifications, as well as the results generated by some of the settings. As set version, provided the variant. Finally, I will print out our array of bytes in hexadecimal way through DatatypeConverter under javax.xml.bind package.

Fourth, the source parse
UUID

public  static the UUID randomUUID () { 
        a SecureRandom ng = Holder.numberGenerator; // for generating random byte array type. 

        byte [] = randomBytes new new  byte [16]; // generates a 16 byte array size 
        ng.nextBytes (randomBytes); // to assign byte random value. In fact, the call is engineNextBytes. 
        
        // UUID to meet certain specifications, as well as the results generated by some of the settings. As set version, provided the variant 
        randomBytes [. 6] = & 0x0F;   / * Clear Version         * / 
        randomBytes [ . 6] | = 0x40;   / * SET. 4 to Version      * / 
        randomBytes [ . 8] = & 0x3F;   / * Clear Variant        */
        randomBytes[8]  |= 0x80;  /* set to IETF variant  */
        return new UUID(randomBytes);
    }

 

Private the UUID ( byte [] Data) {
         Long MSB = 0 ;
         Long LSB = 0 ;
         Assert data.length == 16: "Data MUST BE 16 bytes in length" ;
         for ( int I = 0; I <. 8; I ++ )
             // Why & 0xff? Is not due to a negative or a byte, char, short of & are converted to int
             // as long type is 64, performing | operator need only be 0 complement arithmetic 
            msb = (msb << 8 ) | (Data [I] & 0xFF );
         for ( int I =. 8; I <16; I ++ ) 
            LSB = (LSB <<. 8) | (Data [I] & 0xFF );
         the this.mostSigBits = msb;
        this.leastSigBits = lsb;
    }

UUID in Java with two long type variable mostSigBits, leastSigBits to hold UUID. There should be noted 0xffthat this is in order to avoid |a negative number. To illustrate not be |a negative number, I give an example:

 

 

And finally lead to unexpected situations

Five, clever but useless

1, long turn hex

long x=15L;
String temp=Long.toHexString(x);
System.out.println(temp);

The results: f

2, byte array to a hexadecimal

byte[] randomBytes={15,14,12,11,-6};
System.out.println(DatatypeConverter.printHexBinary(randomBytes));

The results: 0F0E0C0BFA

VI. References

Wikipedia
JAVA byte array into a hexadecimal string output

https://blog.csdn.net/wobushixiaobailian/article/details/86065041

 

Guess you like

Origin www.cnblogs.com/softidea/p/11448263.html