Java的UUID.nameUUIDFromBytes用JavaScript编写? [英] Java's UUID.nameUUIDFromBytes to written in JavaScript?
问题描述
您好我有一个我无法控制的第三方应用程序,它使用Java的 UUID.nameUUIDFromBytes
来创建一个字符串。我需要重现用JS编写的这个函数。
Hi 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 JS.
在 OpenJDK的来源我发现了这个:
public static UUID nameUUIDFromBytes(byte[] name) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported", nsae);
}
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);
}
对于我的生活,我无法理解,主要是因为JS如何抽取字节。
For the life of me I can't figure it out, mostly because of how JS abstracts bytes.
TL:DR我只需要一个js函数,它将产生与 UUID.nameUUIDFromBytes((VARIABLE)相同的字符串。 getBytes(Charsets.UTF_8))。toString()
TL:DR I just need a js function that will produce the same string as UUID.nameUUIDFromBytes((VARIABLE).getBytes(Charsets.UTF_8)).toString()
推荐答案
好的,这不是纯粹的-js,但它是nodejs。
Okay so, this is not pure-js, but it is nodejs.
const crypto = require('crypto')
function javaHash(in) {
let md5Bytes = crypto.createHash('md5').update(in).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 md5bytes.toString(16)
}
这篇关于Java的UUID.nameUUIDFromBytes用JavaScript编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!