Java's UUID.nameUUIDFromBytes to written in JavaScript?

后端 未结 3 1898
梦谈多话
梦谈多话 2021-01-06 23:20

I have a 3rd party application that I can\'t control that uses Java\'s UUID.nameUUIDFromBytes to create a string. I need to reproduce this function written in J

3条回答
  •  猫巷女王i
    2021-01-06 23:52

    This solution gave me exact UUID for me

    const crypto = require('crypto');
    const hexToUuid = require('hex-to-uuid');
    
    const javaHash = (input) => {
        var md5Bytes = crypto.createHash('md5').update(input).digest()
        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 hexToUuid(md5Bytes.toString('hex'))
    }
    
    console.log('javaHash', javaHash("HelloWorld"));
        // 68e109f0-f40c-372a-95e0-5cc22786f8e6
    

提交回复
热议问题