Mahesh Bongani
Mahesh Bongani

Reputation: 730

Is it possible to create a Javascript version of UUID generator equivalent to Java's

Is it possible to create a javascript version of UUID generator equivalent to Java's UUID.nameUUIDFromBytes("Hello world".getBytes(Charsets.UTF_8)). Is there anything available for Javascript or Jquery.

It should return the exact UUID for an input string for both Java and Javascript versions. Here is the Java version of doing it, https://ideone.com/GYvxCE for reference.

Please help.

Upvotes: 0

Views: 2227

Answers (2)

Mahesh Bongani
Mahesh Bongani

Reputation: 730

Thank you guys, for your help. I modified solution provided by @Akrion and it worked for me.

The modified solution is below.

const crypto = require('crypto');
const hexToUuid = require('hex-to-uuid');

const java_kind_hash = (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('java_kind_hash ', java_kind_hash ("HelloWorld"));
    // 68e109f0-f40c-372a-95e0-5cc22786f8e6

Upvotes: 2

balzee
balzee

Reputation: 56

https://www.npmjs.com/package/uuid

Look like this package has methods similar to what you are looking for

Upvotes: 1

Related Questions