Skip to content

Instantly share code, notes, and snippets.

@Rabbit0w0
Created September 7, 2024 11:21
Show Gist options
  • Save Rabbit0w0/89c664a8e6f3eb3a9ec39b3aa5b2fbec to your computer and use it in GitHub Desktop.
Save Rabbit0w0/89c664a8e6f3eb3a9ec39b3aa5b2fbec to your computer and use it in GitHub Desktop.
Generate offline UUID for Minecraft player in Rust
use md5::{Md5, Digest};
use std::fmt::Write;
fn build_uuid(username: &str) -> String {
let mut hasher = Md5::new();
hasher.update(format!("OfflinePlayer:{}", username).as_bytes());
let result = hasher.finalize();
let mut data = result.into_iter().collect::<Vec<u8>>();
data[6] = (data[6] & 0x0f) | 0x30;
data[8] = (data[8] & 0x3f) | 0x80;
to_formatted(&data)
}
fn to_formatted(data: &[u8]) -> String {
let mut hex_string = String::new();
for byte in data {
write!(&mut hex_string, "{:02x}", byte).unwrap();
}
format!(
"{}-{}-{}-{}-{}",
&hex_string[0..8],
&hex_string[8..12],
&hex_string[12..16],
&hex_string[16..20],
&hex_string[20..32]
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment