What namespace does the JDK use to generate a UUID with nameUUIDFromBytes?

前端 未结 2 803
耶瑟儿~
耶瑟儿~ 2020-12-29 10:34

The Sun/Oracle JDK exposes a function to create a type 3 (name based) UUID in the java.util package: java.util.UUID.nameUUIDFromBytes(byte[] name).

I need to be able

2条回答
  •  余生分开走
    2020-12-29 10:54

    A Sample Code:

    String NameSpace_OID_string = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
    UUID NameSpace_OID_uuid = UUID.fromString(NameSpace_OID_string);
    
    long msb = NameSpace_OID_uuid.getMostSignificantBits();
    long lsb = NameSpace_OID_uuid.getLeastSignificantBits();
    
        byte[] NameSpace_OID_buffer = new byte[16];
    
        for (int i = 0; i < 8; i++) {
            NameSpace_OID_buffer[i] = (byte) (msb >>> 8 * (7 - i));
        }
        for (int i = 8; i < 16; i++) {
            NameSpace_OID_buffer[i] = (byte) (lsb >>> 8 * (7 - i));
        }
    
        String name = "user123";
        byte[] name_buffer = name.getBytes();
    
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
    try {
        outputStream.write( NameSpace_OID_buffer);
        outputStream.write( name_buffer );
    } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    byte byteArray[] = outputStream.toByteArray();
    
    System.out.println(UUID.nameUUIDFromBytes(byteArray).toString());
    

提交回复
热议问题