Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.

Commit bb6e312

Browse files
authored
Merge pull request #37 from nayato/ring
switch to ring for signing
2 parents a18b802 + b7cea13 commit bb6e312

File tree

8 files changed

+29
-43
lines changed

8 files changed

+29
-43
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["sdk", "azure", "rest", "iot", "cloud"]
1313
categories = ["api-bindings"]
1414

1515
[dependencies]
16+
ring = "0.12.1"
1617
RustyXML = "0.1.1"
1718
base64 = "0.9.1"
1819
chrono = "0.4.2"

examples/document_entries00.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct MySampleStructOwned {
4747
// This example expects you to have created a collection
4848
// with partitionKey on "id". This SDK works with
4949
// unpartitioned collections too but this example,
50-
// for semplicity sake, does not :)
50+
// for simplicity sake, does not :)
5151
fn main() {
5252
code().unwrap();
5353
}
@@ -70,7 +70,7 @@ fn code() -> Result<(), Box<Error>> {
7070

7171
let client = Client::new(&core.handle(), authorization_token)?;
7272

73-
for i in 0..50 {
73+
for i in 0..5 {
7474
let doc = MySampleStruct {
7575
id: &format!("unique_id{}", i),
7676
a_string: "Something here",

src/azure/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern crate crypto;
1+
extern crate ring;
22
extern crate hyper;
33
extern crate url;
44

src/azure/cosmos/client.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ use serde::Serialize;
3333
use serde_json::map::Map;
3434
use serde_json::Value;
3535

36-
use crypto::hmac::Hmac;
37-
use crypto::mac::Mac;
38-
use crypto::sha2::Sha256;
36+
use ring::{hmac, digest::SHA256};
3937

4038
use base64;
4139
use hyper;
@@ -1250,10 +1248,9 @@ fn generate_authorization(
12501248
}
12511249

12521250
fn encode_str_to_sign(str_to_sign: &str, authorization_token: &AuthorizationToken) -> String {
1253-
let mut hmac = Hmac::new(Sha256::new(), authorization_token.binary_form());
1254-
hmac.input(str_to_sign.as_bytes());
1255-
1256-
base64::encode(hmac.result().code())
1251+
let key = hmac::SigningKey::new(&SHA256, authorization_token.binary_form());
1252+
let sig = hmac::sign(&key, str_to_sign.as_bytes());
1253+
base64::encode(sig.as_ref())
12571254
}
12581255

12591256
fn string_to_sign(

src/azure/service_bus/event_hub/client.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ use azure::service_bus::event_hub::send_event;
66

77
use time::Duration;
88

9-
use crypto::hmac::Hmac;
10-
use crypto::sha2::Sha256;
9+
use ring::{hmac::SigningKey, digest::SHA256};
1110

1211
pub struct Client {
1312
handle: tokio_core::reactor::Handle,
1413
namespace: String,
1514
event_hub: String,
1615
policy_name: String,
17-
hmac: Hmac<Sha256>,
16+
signing_key: SigningKey,
1817
}
1918

2019
impl Client {
@@ -25,16 +24,14 @@ impl Client {
2524
policy_name: &str,
2625
key: &str,
2726
) -> Client {
28-
let mut v_hmac_key: Vec<u8> = Vec::new();
29-
v_hmac_key.extend(key.as_bytes());
30-
let hmac = Hmac::new(Sha256::new(), &v_hmac_key);
27+
let signing_key = SigningKey::new(&SHA256, key.as_bytes());
3128

3229
Client {
3330
handle: handle,
3431
namespace: namespace.to_owned(),
3532
event_hub: event_hub.to_owned(),
3633
policy_name: policy_name.to_owned(),
37-
hmac: hmac,
34+
signing_key,
3835
}
3936
}
4037

@@ -49,7 +46,7 @@ impl Client {
4946
&self.namespace,
5047
&self.event_hub,
5148
&self.policy_name,
52-
&mut self.hmac,
49+
&self.signing_key,
5350
event_body,
5451
duration,
5552
)
@@ -61,20 +58,20 @@ impl Client {
6158
mod test {
6259
#[allow(unused_imports)]
6360
use super::Client;
61+
use ring::hmac;
6462

6563
#[test]
6664
pub fn client_enc() {
6765
use base64;
68-
use crypto::mac::Mac;
6966
use tokio_core::reactor::Core;
7067

7168
let str_to_sign = "This must be secret!";
7269

7370
let core = Core::new().unwrap();
74-
let mut c = Client::new(core.handle(), "namespace", "event_hub", "policy", "key");
71+
let c = Client::new(core.handle(), "namespace", "event_hub", "policy", "key");
7572

76-
c.hmac.input(str_to_sign.as_bytes());
77-
let sig = base64::encode(c.hmac.result().code());
73+
let sig = hmac::sign(&c.signing_key, str_to_sign.as_bytes());
74+
let sig = base64::encode(sig.as_ref());
7875

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

src/azure/service_bus/event_hub/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use std::ops::Add;
2020
use url::form_urlencoded::Serializer;
2121
use url::percent_encoding::utf8_percent_encode;
2222

23-
use crypto::hmac::Hmac;
24-
use crypto::mac::Mac;
25-
use crypto::sha2::Sha256;
23+
use ring::hmac;
2624

2725
mod client;
2826
pub use self::client::Client;
@@ -35,7 +33,7 @@ fn send_event_prepare(
3533
namespace: &str,
3634
event_hub: &str,
3735
policy_name: &str,
38-
hmac: &mut Hmac<Sha256>,
36+
signing_key: &hmac::SigningKey,
3937
event_body: &str,
4038
duration: Duration,
4139
) -> Result<hyper::client::FutureResponse, AzureError> {
@@ -48,7 +46,7 @@ fn send_event_prepare(
4846
debug!("url == {:?}", url);
4947

5048
// generate sas signature based on key name, key value, url and duration.
51-
let sas = generate_signature(policy_name, hmac, &url.to_string(), duration);
49+
let sas = generate_signature(policy_name, signing_key, &url.to_string(), duration);
5250
debug!("sas == {}", sas);
5351

5452
let client = hyper::Client::configure()
@@ -73,7 +71,7 @@ fn send_event(
7371
namespace: &str,
7472
event_hub: &str,
7573
policy_name: &str,
76-
hmac: &mut Hmac<Sha256>,
74+
hmac: &hmac::SigningKey,
7775
event_body: &str,
7876
duration: Duration,
7977
) -> impl Future<Item = (), Error = AzureError> {
@@ -94,7 +92,7 @@ fn send_event(
9492

9593
fn generate_signature(
9694
policy_name: &str,
97-
hmac: &mut Hmac<Sha256>,
95+
signing_key: &hmac::SigningKey,
9896
url: &str,
9997
ttl: Duration,
10098
) -> String {
@@ -107,10 +105,9 @@ fn generate_signature(
107105
let str_to_sign = format!("{}\n{}", url_encoded, expiry);
108106
debug!("str_to_sign == {:?}", str_to_sign);
109107

110-
hmac.reset();
111-
hmac.input(str_to_sign.as_bytes());
108+
let sig = hmac::sign(signing_key, str_to_sign.as_bytes());
112109
let sig = {
113-
let sig = base64::encode(hmac.result().code());
110+
let sig = base64::encode(sig.as_ref());
114111
debug!("sig == {}", sig);
115112
let mut ser = Serializer::new(String::new());
116113
ser.append_pair("sig", &sig);

src/azure/storage/rest_client.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use azure::core::lease::{LeaseAction, LeaseDuration, LeaseId, LeaseState, LeaseS
33
use azure::core::range;
44
use base64;
55
use chrono;
6-
use crypto::hmac::Hmac;
7-
use crypto::mac::Mac;
8-
use crypto::sha2::Sha256;
6+
use ring::{hmac, digest::SHA256};
97
use hyper;
108
use hyper::header::{
119
ContentEncoding, ContentLanguage, ContentLength, ContentType, Date, Header, Headers,
@@ -65,17 +63,13 @@ fn generate_authorization(
6563
}
6664

6765
fn encode_str_to_sign(str_to_sign: &str, hmac_key: &str) -> String {
68-
let mut v_hmac_key: Vec<u8> = Vec::new();
69-
70-
v_hmac_key.extend(base64::decode(hmac_key).unwrap());
71-
72-
let mut hmac = Hmac::new(Sha256::new(), &v_hmac_key);
73-
hmac.input(str_to_sign.as_bytes());
66+
let key = hmac::SigningKey::new(&SHA256, &base64::decode(hmac_key).unwrap());
67+
let sig = hmac::sign(&key, str_to_sign.as_bytes());
7468

7569
// let res = hmac.result();
7670
// println!("{:?}", res.code());
7771

78-
base64::encode(hmac.result().code())
72+
base64::encode(sig.as_ref())
7973
}
8074

8175
#[inline]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate native_tls;
1111
extern crate tokio_core;
1212

1313
extern crate base64;
14-
extern crate crypto;
14+
extern crate ring;
1515
extern crate time;
1616
#[macro_use]
1717
extern crate url;

0 commit comments

Comments
 (0)