Skip to content

Instantly share code, notes, and snippets.

@ErnestoRB
Created July 3, 2022 05:05
Show Gist options
  • Save ErnestoRB/f407757ca49390b4a09ede1f4435fa03 to your computer and use it in GitHub Desktop.
Save ErnestoRB/f407757ca49390b4a09ede1f4435fa03 to your computer and use it in GitHub Desktop.
Generate Minecraft OfflinePlayer UUID
const { exit } = require("process");
const createHash = require("crypto").createHash;
// Reads from cli
if (process.argv.length < 3) {
process.stdout.write("\n");
exit(1);
}
const hash = createHash("md5"); // v3
hash.once("readable", () => {
const data = hash.read();
const uuid = [...data];
// https://www.rfc-editor.org/rfc/rfc4122#section-4.3
uuid[6] = (data[6] & 0x0f) | 0x30; // v3
uuid[8] = (data[8] & 0x3f) | 0x80;
process.stdout.write(splittedUUID(toHexString(uuid)));
});
function toHexString(byteArray) {
return byteArray
.map((byte) => ("0" + (byte & 0xff).toString(16)).slice(-2))
.join("");
}
function splittedUUID(uuid) {
return [
uuid.substring(0, 8),
uuid.substring(8, 12),
uuid.substring(12, 16),
uuid.substring(16, 20),
uuid.substring(20, 32)
].join("-");
}
hash.write("OfflinePlayer:" + process.argv[2]);
hash.end();
@siraly1636
Copy link

Hi, could you change this for me so that the player name is not from a cli, but from a const?

@ErnestoRB
Copy link
Author

Do you want it to be a function to be called elsewhere ? @siraly1636

@siraly1636
Copy link

Yes, because I want to use it in an express on a website.

@ErnestoRB
Copy link
Author

@siraly1636
Copy link

Thank you so much. @ErnestoRB I have already successfully integrated it into my website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment