UUID简记

1、概述

wiki上的解释:html

universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.java

128位的UUID也并不是没有重复的可能,理论证实这个重复的几率很小近乎为零以致于能够忽略。node

UUID规范的表示格式一般用32个16进制字符表示(也就是"0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /"a" / "b" / "c" / "d" / "e" / "f"这16个字符)。mysql

这32个字符一般被连字符“-”分红5个组(简记为8-4-4-4-12格式),以下:git

8a2986cc-256d-470b-bc3d-027113f76553

5个组所表示的含义以下表所示(图表来自wiki):算法

UUID record layout
Name Length (bytes) Length (hex digits) Contents
time_low 4 8 integer giving the low 32 bits of the time
time_mid 2 4 integer giving the middle 16 bits of the time
time_hi_and_version 2 4 4-bit "version" in the most significant bits, followed by the high 12 bits of the time
clock_seq_hi_and_res clock_seq_low 2 4 1-3 bit "variant" in the most significant bits, followed by the 13-15 bit clock sequence
node 6 12 the 48-bit node id

其中version表明了UUID按照何种规则产生。UUID有四种版本:sql

一、Time-based UUID数组

二、DCE security UUID网络

三、Name-based UUIDdom

四、Randomly generated UUID

Version 1 UUIDs are generated from a time and a node id (usually the MAC address); version 2 UUIDs are generated from an identifier (usually a group or user id), time, and a node id; versions 3 and 5 produce deterministic UUIDs generated by hashing a namespace identifier and name; and version 4 UUIDs are generated using a random or pseudo-random number.

variant(变体,或者更通俗的理解就是类型,type)表明了UUID的位布局。UUID也有四种变体,除了variant 位的不一样,variant 1和variant 2的区别在于存储和传输时,variant 1用网络字节序(大端模式),variant 2用本地字节序(小端模式)。

2、Java中UUID实现

java中提供了UUID类。在UUID类的内部,将128位分两部分存储:最低有效位(64位)leastSigBits和最高有效位(64位)mostSigBits

其文档中对这两部分的位布局作了简单说明,可是这里好像与wiki上面的有点出入,按照wiki的说法,这里应该是variant 1而不是variant 2,而且在randomUUID()中重置variant位时也是重置了2个bit位:10xx,总之这里关注重点便可。

 * <p>The layout of a variant 2 (Leach-Salz) UUID is as follows:
 *
 * The most significant long consists of the following unsigned fields:
 * <pre>
 * 0xFFFFFFFF00000000 time_low
 * 0x00000000FFFF0000 time_mid
 * 0x000000000000F000 version
 * 0x0000000000000FFF time_hi
 * </pre>
 * The least significant long consists of the following unsigned fields:
 * <pre>
 * 0xC000000000000000 variant
 * 0x3FFF000000000000 clock_seq
 * 0x0000FFFFFFFFFFFF node
 * </pre>

UUID经过静态工厂方法产生伪随机的UUID,生成UUID实例的步骤大体以下:

一、产生长度为16的伪随机字节数组

二、重置version位和variant位,4bit的version位重置为0100,2bit的variant位重置为10xx。

三、调用私有构造器UUID(byte[] data),将步骤2中的随机字节数组的前8字节给mostSigBits,后8字节给leastSigBits,至此,一个UUID实例产生。

    public static UUID randomUUID() {
        SecureRandom ng = numberGenerator;
        if (ng == null) {
            numberGenerator = ng = new SecureRandom();
        }

        byte[] randomBytes = new byte[16];
        ng.nextBytes(randomBytes);
        randomBytes[6]  &= 0x0f;  /* clear version        */
        randomBytes[6]  |= 0x40;  /* set to version 4     */
        randomBytes[8]  &= 0x3f;  /* clear variant        */
        randomBytes[8]  |= 0x80;  /* set to IETF variant  */
        return new UUID(randomBytes);
    }
    private UUID(byte[] data) {
        long msb = 0;
        long lsb = 0;
        assert data.length == 16;
        for (int i=0; i<8; i++)
            msb = (msb << 8) | (data[i] & 0xff);
        for (int i=8; i<16; i++)
            lsb = (lsb << 8) | (data[i] & 0xff);
        this.mostSigBits = msb;
        this.leastSigBits = lsb;
    }

用法以下:

            UUID uuid = UUID.randomUUID();
            System.out.println(uuid.toString());//f2fbeb3c-07e0-41e2-8dd2-1a49e77d6d67
            System.out.println(Long.toHexString(uuid.getMostSignificantBits()));//f2fbeb3c07e041e2
            System.out.println(Long.toHexString(uuid.getLeastSignificantBits()));//8dd21a49e77d6d67

也能够产生version 3的UUID,以传入的字节数组name为参数,经过MD5算法生成长度为16的字节数组,以后的处理过程与上面的相似。

    public static UUID nameUUIDFromBytes(byte[] name) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            throw new InternalError("MD5 not supported");
        }
        byte[] md5Bytes = md.digest(name);
        md5Bytes[6]  &= 0x0f;  /* clear version        */
        md5Bytes[6]  |= 0x30;  /* set to version 3     */
        md5Bytes[8]  &= 0x3f;  /* clear variant        */
        md5Bytes[8]  |= 0x80;  /* set to IETF variant  */
        return new UUID(md5Bytes);
    }

用法以下:

            UUID uuid = UUID.nameUUIDFromBytes("2018".getBytes());
            System.out.println(uuid.toString());//84ddfb34-126f-33a4-8ee3-8d7044e87276

3、MySQL中的UUID

 MySQL中提供了UUID()函数来获取UUID,必要的状况下能够方便地以此充当表的主键。此外,UUID_SHORT()函数返回一个64位的无符号数字。

4、参考资料

一、https://en.wikipedia.org/wiki/Universally_unique_identifier

二、https://tools.ietf.org/html/rfc4122#section-4.1

三、https://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_uuid

四、JDK1.6 src

相关文章
相关标签/搜索