ohwid/
lib.rs

1#[cfg(target_os = "windows")]
2mod hwid {
3    use winreg::{enums::HKEY_LOCAL_MACHINE, RegKey};
4
5    pub fn get_hwid() -> std::io::Result<String> {
6        let key = RegKey::predef(HKEY_LOCAL_MACHINE)
7            .open_subkey("SOFTWARE\\Microsoft\\Cryptography")?;
8        
9        key.get_value::<String, _>("MachineGuid")
10    }
11}
12
13#[cfg(target_os = "linux")]
14mod hwid {
15    pub fn get_hwid() -> std::io::Result<String> {
16        Ok(std::fs::read_to_string("/etc/machine-id")?.trim_end_matches("\n").to_string())
17    }
18}
19
20#[cfg(not(any(target_os = "windows", target_os = "linux")))]
21pub fn get_hwid() -> std::io::Result<String> {
22    Err(std::io::Error::new(
23        std::io::ErrorKind::Other,
24        "get_hwid is only supported on Windows",
25    ))
26}
27
28/// Get the hardware ID of the current machine
29/// 
30/// # Example
31/// ```
32/// use ohwid::get_hwid;
33/// let hwid = get_hwid();
34/// 
35/// match hwid {
36///     Ok(hwid) => println!("HWID: {}", hwid),
37///     Err(e) => println!("Failed to get HWID: {}", e),
38/// }
39/// ```
40pub fn get_hwid() -> std::io::Result<String> {
41    hwid::get_hwid()
42}