use sha2::{Digest, Sha256};
mod macos;
#[derive(Default)]
pub struct SystemIDBuilder {
components: Vec<String>,
}
impl SystemIDBuilder {
pub fn new() -> Self {
SystemIDBuilder {
components: Vec::new(),
}
}
pub fn on_macos<F>(&mut self, mut run: F) -> &mut Self
where
F: FnMut(&mut Self),
{
#[cfg(target_os = "macos")]
{
run(self);
}
self
}
pub fn build(&self) -> String {
let mut hasher = Sha256::new();
hasher.update(self.components.join("-").as_bytes());
hasher
.finalize()
.iter()
.map(|b| format!("{:02x}", b))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn macos_id() {
println!(
"HWID: {}",
SystemIDBuilder::new()
.on_macos(|b| {
b.add_platform_serial()
.add_platform_uuid()
.add_drive_serial()
.add_cpu_brand();
})
.build()
);
}
}