|
| 1 | +//! Stable, hashed device identifier for fair-use license tracking. |
| 2 | +//! |
| 3 | +//! Generates a one-way hash of the hardware UUID, prefixed with a version tag. |
| 4 | +//! The hash is salted with `"cmdr:"` so it can't be correlated across products. |
| 5 | +//! Result format: `v1:<64-char hex SHA-256>`. |
| 6 | +
|
| 7 | +use sha2::{Digest, Sha256}; |
| 8 | +use std::sync::OnceLock; |
| 9 | + |
| 10 | +/// Cached device ID (computed once per session). |
| 11 | +static DEVICE_ID: OnceLock<Option<String>> = OnceLock::new(); |
| 12 | + |
| 13 | +/// Returns a stable, hashed device identifier, or `None` if the platform UUID can't be read. |
| 14 | +/// |
| 15 | +/// The result is cached in memory — the hardware UUID won't change during a session. |
| 16 | +pub fn get_device_id() -> Option<String> { |
| 17 | + DEVICE_ID.get_or_init(compute_device_id).clone() |
| 18 | +} |
| 19 | + |
| 20 | +fn compute_device_id() -> Option<String> { |
| 21 | + let uuid = read_platform_uuid()?; |
| 22 | + let salted = format!("cmdr:{uuid}"); |
| 23 | + let hash = Sha256::digest(salted.as_bytes()); |
| 24 | + Some(format!("v1:{:x}", hash)) |
| 25 | +} |
| 26 | + |
| 27 | +/// Read `IOPlatformUUID` from the IOKit registry via FFI. |
| 28 | +#[cfg(target_os = "macos")] |
| 29 | +fn read_platform_uuid() -> Option<String> { |
| 30 | + use core_foundation::base::TCFType; |
| 31 | + use core_foundation::string::{CFString, CFStringRef}; |
| 32 | + use std::ffi::c_void; |
| 33 | + |
| 34 | + #[link(name = "IOKit", kind = "framework")] |
| 35 | + unsafe extern "C" { |
| 36 | + fn IOServiceGetMatchingService(main_port: u32, matching: *const c_void) -> u32; |
| 37 | + fn IOServiceMatching(name: *const std::ffi::c_char) -> *const c_void; |
| 38 | + fn IORegistryEntryCreateCFProperty( |
| 39 | + entry: u32, |
| 40 | + key: CFStringRef, |
| 41 | + allocator: *const c_void, |
| 42 | + options: u32, |
| 43 | + ) -> *const c_void; |
| 44 | + fn IOObjectRelease(object: u32) -> i32; |
| 45 | + } |
| 46 | + |
| 47 | + unsafe { |
| 48 | + let matching = IOServiceMatching(c"IOPlatformExpertDevice".as_ptr()); |
| 49 | + if matching.is_null() { |
| 50 | + log::warn!("IOServiceMatching returned null"); |
| 51 | + return None; |
| 52 | + } |
| 53 | + |
| 54 | + // kIOMasterPortDefault / kIOMainPortDefault = 0 |
| 55 | + let service = IOServiceGetMatchingService(0, matching); |
| 56 | + // IOServiceMatching result is consumed by IOServiceGetMatchingService — don't CFRelease it. |
| 57 | + if service == 0 { |
| 58 | + log::warn!("IOServiceGetMatchingService found no platform expert"); |
| 59 | + return None; |
| 60 | + } |
| 61 | + |
| 62 | + let key = CFString::new("IOPlatformUUID"); |
| 63 | + let cf_value = IORegistryEntryCreateCFProperty(service, key.as_concrete_TypeRef(), std::ptr::null(), 0); |
| 64 | + IOObjectRelease(service); |
| 65 | + |
| 66 | + if cf_value.is_null() { |
| 67 | + log::warn!("IORegistryEntryCreateCFProperty returned null for IOPlatformUUID"); |
| 68 | + return None; |
| 69 | + } |
| 70 | + |
| 71 | + let cf_string = CFString::wrap_under_create_rule(cf_value as CFStringRef); |
| 72 | + Some(cf_string.to_string()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Linux stub — returns `None` for now. |
| 77 | +// TODO: Read `/etc/machine-id`, apply the same salt-and-hash approach as macOS. |
| 78 | +#[cfg(target_os = "linux")] |
| 79 | +fn read_platform_uuid() -> Option<String> { |
| 80 | + None |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use super::*; |
| 86 | + |
| 87 | + #[test] |
| 88 | + #[cfg(target_os = "macos")] |
| 89 | + fn returns_some_on_macos() { |
| 90 | + let id = get_device_id(); |
| 91 | + assert!(id.is_some(), "get_device_id() should return Some on macOS"); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + #[cfg(target_os = "macos")] |
| 96 | + fn matches_expected_format() { |
| 97 | + let id = get_device_id().expect("should return Some on macOS"); |
| 98 | + assert!(id.starts_with("v1:"), "should start with 'v1:' prefix, got: {id}"); |
| 99 | + let hex_part = &id[3..]; |
| 100 | + assert_eq!( |
| 101 | + hex_part.len(), |
| 102 | + 64, |
| 103 | + "hex part should be 64 chars, got: {}", |
| 104 | + hex_part.len() |
| 105 | + ); |
| 106 | + assert!( |
| 107 | + hex_part.chars().all(|c| c.is_ascii_hexdigit()), |
| 108 | + "hex part should be lowercase hex, got: {hex_part}" |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + #[cfg(target_os = "macos")] |
| 114 | + fn returns_stable_value() { |
| 115 | + let first = get_device_id(); |
| 116 | + let second = get_device_id(); |
| 117 | + assert_eq!( |
| 118 | + first, second, |
| 119 | + "get_device_id() should return the same value on repeated calls" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn hash_is_deterministic() { |
| 125 | + // Verify the hashing logic directly (platform-independent). |
| 126 | + let uuid = "TEST-UUID-1234"; |
| 127 | + let salted = format!("cmdr:{uuid}"); |
| 128 | + let hash = Sha256::digest(salted.as_bytes()); |
| 129 | + let result = format!("v1:{:x}", hash); |
| 130 | + |
| 131 | + let salted2 = format!("cmdr:{uuid}"); |
| 132 | + let hash2 = Sha256::digest(salted2.as_bytes()); |
| 133 | + let result2 = format!("v1:{:x}", hash2); |
| 134 | + |
| 135 | + assert_eq!(result, result2); |
| 136 | + assert!(result.starts_with("v1:")); |
| 137 | + assert_eq!(result.len(), 3 + 64); // "v1:" + 64 hex chars |
| 138 | + } |
| 139 | +} |
0 commit comments