use sha1::{Digest, Sha1};
use std::fs;
use std::io::Result;
use sysinfo::System;
#[cfg(target_os = "windows")]
pub mod hwid {
use super::*;
pub fn get() -> String {
let metadata = fs::metadata(r"C:\Windows\System32\ntoskrnl.exe").unwrap();
let system_time: std::time::SystemTime = metadata.created().unwrap().into();
let since_the_epoch = system_time
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap();
let in_seconds = since_the_epoch.as_secs();
let kernelage = format!("{:?}", in_seconds);
let num_cpus = num_cpus::get();
use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE};
let hive = winreg::RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags("Software\\Microsoft\\Cryptography", KEY_QUERY_VALUE)
.unwrap();
let regid: String = hive.get_value("MachineGuid").unwrap_or("ERROR".to_string());
let mut system = System::new_all();
system.refresh_all();
let total_memory = system.total_memory();
let combined = format!("{}|{}|{}|{}", num_cpus, regid, kernelage, total_memory);
let hwid = hex::encode(Sha1::digest(combined));
hwid
}
}
#[cfg(target_os = "linux")]
pub mod hwid {
use std::io;
use super::*;
pub fn get() -> String {
let num_cpus = num_cpus::get();
let regid = machineid().unwrap_or("ERROR".to_string());
let mut system = System::new_all();
system.refresh_all();
let total_memory = system.total_memory();
let combined = format!("{}|{}|{}", num_cpus, regid, total_memory);
hex::encode(Sha1::digest(combined))
}
fn machineid() -> Result<String> {
let paths = ["/var/lib/dbus/machine-id", "/etc/machine-id"];
for p in paths {
let id_contents = fs::read_to_string(p)?;
if let Some(id_str) = id_contents.lines().next() {
return Ok(id_str.to_string());
}
}
Err(io::Error::new(
io::ErrorKind::NotFound,
"Machine ID file not found",
))
}
}