UUID interop with c# code

后端 未结 3 768
死守一世寂寞
死守一世寂寞 2021-02-11 09:37

Can c# donet generate same UUID for following java code? if so how? i tried GUID but didn\'t work!

Text:

String cleartext = \"CN=CompanyName;mac=some mac         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-11 10:11

    If you want interoperability don't depend on code that's not under your control. It could change with each Java version or implementation.

    You could take the way that is used

    /**
     * Static factory to retrieve a type 3 (name based) {@code UUID} based on
     * the specified byte array.
     *
     * @param  name
     *         A byte array to be used to construct a {@code UUID}
     *
     * @return  A {@code UUID} generated from the specified array
     */
    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);
    }
    

    source

    and copy it into your code. Create exactly that in other languages as well and your problem should be gone.

    In case "type 3 (name based) UUID" is a standard where the result is exactly specified, you could skip copying it in Java since implementation should never return a different result. It's likely that you will find implementations for other languages as well and don't need to port it by hand.

提交回复
热议问题