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
This answer attempts to improve on the previous ones as follows
const crypto = require('crypto');
function javaHash(input:string) {
let 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 */
const hex = md5Bytes.toString('hex')
const uuid = hex.replace(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/, "$1-$2-$3-$4-$5");
return uuid;
}
It relies on the UUID spec being exactly 128bits in length