生成guid

This commit is contained in:
lbl8603
2024-07-24 22:11:50 +08:00
parent be966c4f83
commit 352f322f61
3 changed files with 12 additions and 5 deletions
Generated
+1
View File
@@ -1968,6 +1968,7 @@ dependencies = [
"libloading",
"log",
"rand",
"sha2",
"widestring",
"winapi",
]
+1
View File
@@ -11,6 +11,7 @@ libc = "0.2.153"
log = { version = "0.4.20", features = [] }
rand = "0.8.5"
sha2 = { version = "0.10.6", features = ["oid"] }
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
ioctl = { version = "0.8", package = "ioctl-sys" }
+10 -5
View File
@@ -1,9 +1,8 @@
#![allow(dead_code)]
use libloading::Library;
use sha2::Digest;
use std::io;
use std::net::Ipv4Addr;
use rand::Rng;
use winapi::um::winbase;
use winapi::um::{synchapi, winnt};
@@ -81,8 +80,7 @@ impl Device {
if Self::delete_for_name(&win_tun, &name_utf16).is_ok() {
std::thread::sleep(std::time::Duration::from_millis(500));
}
let mut guid_bytes: [u8; 16] = [0u8; 16];
rand::thread_rng().fill(&mut guid_bytes);
let guid_bytes: [u8; 16] = hash_guid(&name);
let guid = u128::from_ne_bytes(guid_bytes);
//SAFETY: guid is a unique integer so transmuting either all zeroes or the user's preferred
//guid to the winapi guid type is safe and will allow the windows kernel to see our GUID
@@ -154,7 +152,14 @@ impl Device {
Ok(())
}
}
fn hash_guid(input: &str) -> [u8; 16] {
let mut hasher = sha2::Sha256::new();
hasher.update(b"VNT");
hasher.update(input.as_bytes());
hasher.update(b"2024");
let hash: [u8; 32] = hasher.finalize().into();
hash[..16].try_into().unwrap()
}
impl IFace for Device {
fn version(&self) -> io::Result<String> {
let version = unsafe { self.win_tun.WintunGetRunningDriverVersion() };