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
extern crate crypto;
extern crate clock_ticks;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use crypto::hmac::Hmac;
use crypto::mac::{Mac,MacResult};
use clock_ticks::precise_time_ns;
#[derive(Debug)]
pub struct Client {
tid: u64,
et: u64,
utc: f64,
key: u64,
}
impl Client {
pub fn blank() -> Client {
Client {
tid: 0,
et: 0,
utc: 0.,
key: 0,
}
}
}
pub struct Msg {
mid: MacResult,
dt: u64,
data: Vec<u8>,
}
impl Msg {
pub fn new (client: &Client, data: &[u8]) -> Msg {
let dt = precise_time_ns() - client.et;
let key = b"Jefe".to_vec();
let mut hmac = Hmac::new(Sha256::new(),&key[..]);
Msg {
dt: dt,
data: data.to_vec(),
mid: MacResult::new(hmac.result().code()),
}
}
pub fn auth (client: &Client, msg: &Msg) -> bool {
let key = b"Jefe".to_vec();
let mut hmac = Hmac::new(Sha256::new(),&key[..]);
msg.mid == MacResult::new(hmac.result().code())
}
}