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
use azure::service_bus::event_hub::send_event;
use azure::core::errors::AzureError;

use time::Duration;
use std::io::Read;

use crypto::sha2::Sha256;
use crypto::hmac::Hmac;

pub struct Client {
    namespace: String,
    event_hub: String,
    policy_name: String,
    hmac: Hmac<Sha256>,
}

impl Client {
    pub fn new(namespace: &str, event_hub: &str, policy_name: &str, key: &str) -> Client {
        let mut v_hmac_key: Vec<u8> = Vec::new();
        v_hmac_key.extend(key.as_bytes());
        let hmac = Hmac::new(Sha256::new(), &v_hmac_key);

        Client {
            namespace: namespace.to_owned(),
            event_hub: event_hub.to_owned(),
            policy_name: policy_name.to_owned(),
            hmac: hmac,
        }
    }

    pub fn send_event(&mut self,
                      event_body: (&mut Read, u64),
                      duration: Duration)
                      -> Result<(), AzureError> {
        send_event(&self.namespace,
                   &self.event_hub,
                   &self.policy_name,
                   &mut self.hmac,
                   event_body,
                   duration)
    }
}

#[cfg(test)]
mod test {
    #[allow(unused_imports)]
    use super::Client;

    #[test]
    pub fn client_ctor() {
        Client::new("namespace", "event_hub", "policy", "key");
    }

    #[test]
    pub fn client_enc() {
        use crypto::mac::Mac;
        use serialize::base64::{STANDARD, ToBase64};

        let str_to_sign = "This must be secret!";

        let mut c = Client::new("namespace", "event_hub", "policy", "key");

        c.hmac.input(str_to_sign.as_bytes());
        let sig = c.hmac.result().code().to_base64(STANDARD);

        assert_eq!(sig, "2UNXaoPpeJBAhh6qxmTqXyNzTpOflGO6IhxegeUQBcU=");
    }
}