1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
extern crate rustc_serialize;
extern crate rand;
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::time::{SystemTime, UNIX_EPOCH};
use rand::Rng;
use rustc_serialize::hex::FromHex;
#[derive(Debug)]
pub struct UUID {
mac: Vec<u8>,
}
impl UUID {
pub fn new(iface: &str) -> UUID {
let path = Path::new("/sys/class/net").join(Path::new(iface)).join("address");
let f = File::open(path)
.expect("Network interface not found.");
let mut reader = BufReader::new(f);
let mut line = String::new();
reader.read_line(&mut line)
.expect("Unable to read iface.");
let mut mac_address = String::new();
for _ in 0..6 {
let s: String = line.drain(0..2).collect();
line.drain(0..1);
mac_address = mac_address + &s;
}
let mac = mac_address.from_hex()
.expect("Unable to decode mac address.");
UUID { mac: mac }
}
pub fn generate_nonce(&self) -> [u8; 8] {
let nanosec_bytes = nanosecs_since_unix_epoch();
let mut rng = rand::thread_rng();
let r1 = rng.gen::<[u8; 2]>();
let mut bytes = [0; 8];
bytes[0] = r1[0] ^ nanosec_bytes[0];
bytes[1] = self.mac[0] ^ nanosec_bytes[1];
bytes[2] = self.mac[1] ^ nanosec_bytes[2];
bytes[3] = self.mac[2] ^ nanosec_bytes[3];
bytes[4] = self.mac[3] ^ nanosec_bytes[4];
bytes[5] = self.mac[4] ^ nanosec_bytes[5];
bytes[6] = self.mac[5] ^ nanosec_bytes[6];
bytes[7] = r1[1] ^ nanosec_bytes[7];
bytes
}
pub fn generate(&self) -> [u8; 16] {
let nanosec_bytes = nanosecs_since_unix_epoch();
let mut rng = rand::thread_rng();
let r = rng.gen::<[u8; 8]>();
let mut bytes = [0; 16];
bytes[0] = nanosec_bytes[0];
bytes[1] = nanosec_bytes[1];
bytes[2] = nanosec_bytes[2];
bytes[3] = nanosec_bytes[3];
bytes[4] = nanosec_bytes[4];
bytes[5] = nanosec_bytes[5];
bytes[6] = nanosec_bytes[6];
bytes[7] = nanosec_bytes[7];
bytes[8] = self.mac[0] ^ r[0];
bytes[9] = self.mac[1] ^ r[1];
bytes[10] = self.mac[2] ^ r[2];
bytes[11] = self.mac[3] ^ r[3];
bytes[12] = self.mac[4] ^ r[4];
bytes[13] = self.mac[5] ^ r[5];
bytes[14] = r[6];
bytes[15] = r[7];
bytes
}
}
fn little_endian_inv64(x: u64) -> [u8; 8] {
let mut bytes: [u8; 8] = [0; 8];
bytes[0] = (x & 0xFF) as u8;
for i in 1..8 {
bytes[i] = ((x & (0xFF << i * 8)) >> i * 8) as u8;
}
bytes
}
fn nanosecs_since_unix_epoch() -> [u8; 8] {
let now = SystemTime::now();
let duration = now.duration_since(UNIX_EPOCH)
.expect("Failed to get duration since unix epoch.");
let secs: u64 = duration.as_secs() as u64 * 1_000_000_0;
let nanosecs: u64 = secs + (duration.subsec_nanos() as u64 / 1_00);
little_endian_inv64(nanosecs)
}
#[cfg(test)]
mod tests {
use super::*;
use rustc_serialize::hex::ToHex;
#[test]
fn generate_uuid() {
let uuid = UUID::new("enp0s31f6");
println!("{:?}", uuid.generate_nonce().to_hex());
println!("{:?}", uuid.generate().to_hex());
}
}