aboutsummaryrefslogtreecommitdiff
path: root/azalea-crypto/src/offline.rs
blob: b0e7c12b0b58657d40484acf6eb353819de4b0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! Offline-mode UUID generation.

use md5::{Digest, Md5};
use uuid::Uuid;

/// Return what the offline-mode UUIDv3 for the given username would be.
pub fn generate_uuid(username: &str) -> Uuid {
    uuid::Builder::from_md5_bytes(hash(format!("OfflinePlayer:{username}").as_bytes())).into_uuid()
}

fn hash(data: &[u8]) -> [u8; 16] {
    let mut hasher = Md5::new();

    hasher.update(data);

    let mut bytes = [0; 16];
    bytes.copy_from_slice(&hasher.finalize()[..16]);

    bytes
}