Skip to content

Commit 43cb01f

Browse files
authored
Merge pull request #34 from hugues31/replace-rust-crypto-crate
Replace rust crypto crate
2 parents b4e831f + e527762 commit 43cb01f

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "coinnect"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
license = "MIT"
55
authors = ["Hugues Gaillard <[email protected]>", "Alejandro Inestal"]
66
description = """
@@ -34,9 +34,10 @@ path = "examples/generic_api.rs"
3434
hyper = "0.10.9"
3535
serde_json = "1.0.0"
3636
time = "0.1.37"
37-
rust-crypto = "0.2"
3837
hyper-native-tls = "0.2.2"
3938
lazy_static = "0.2"
4039
bidir-map = "0.3.2"
4140
data-encoding = "2.0.0-rc.1"
4241
error-chain = "0.7.1"
42+
sha2 = "0.6.0"
43+
hmac = "0.3"

src/bitstamp/utils.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bidir_map::BidirMap;
2-
use crypto::sha2::Sha256;
3-
use crypto::hmac::Hmac;
4-
use crypto::mac::Mac;
2+
3+
use hmac::{Hmac, Mac};
4+
use sha2::{Sha256};
55

66
use serde_json;
77
use serde_json::Value;
@@ -58,10 +58,11 @@ pub fn build_signature(nonce: &str,
5858
const C: &'static [u8] = b"0123456789ABCDEF";
5959

6060
let message = nonce.to_owned() + customer_id + api_key;
61-
let mut hmac = Hmac::new(Sha256::new(), api_secret.as_bytes());
6261

63-
hmac.input(message.as_bytes());
64-
let result = hmac.result();
62+
let mut mac = Hmac::<Sha256>::new(api_secret.as_bytes());
63+
64+
mac.input(message.as_bytes());
65+
let result = mac.result();
6566

6667
let raw_signature = result.code();
6768
let mut signature = Vec::with_capacity(raw_signature.len() * 2);

src/kraken/api.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
44
#![allow(too_many_arguments)]
55

6-
use crypto::digest::Digest;
7-
use crypto::hmac::Hmac;
8-
use crypto::mac::Mac;
9-
use crypto::sha2::{Sha256, Sha512};
6+
use hmac::{Hmac, Mac};
7+
use sha2::{Sha256, Sha512, Digest};
108

119
use hyper_native_tls::NativeTlsClient;
1210
use hyper::Client;
@@ -23,7 +21,6 @@ use std::io::Read;
2321
use std::thread;
2422
use std::time::Duration;
2523
use std::str;
26-
use std::iter::repeat;
2724

2825
use error::*;
2926
use helpers;
@@ -153,20 +150,20 @@ impl KrakenApi {
153150
fn create_signature(&self, urlpath: String, postdata: &str, nonce: &str) -> Result<String> {
154151
let message_presha256 = nonce.to_string() + postdata;
155152

156-
let mut sha256 = Sha256::new();
157-
sha256.input_str(&message_presha256);
158-
let mut buffer: Vec<u8> = repeat(0).take((sha256.output_bits() + 7) / 8).collect();
159-
sha256.result(&mut buffer);
153+
let mut sha256 = Sha256::default();
154+
sha256.input(&message_presha256.as_bytes());
155+
156+
let output = sha256.result();
160157

161158
let mut concatenated = urlpath.as_bytes().to_vec();
162-
for elem in buffer {
159+
for elem in output {
163160
concatenated.push(elem);
164161
}
165162

166163
let hmac_key = BASE64.decode(self.api_secret.as_bytes())?;
167-
let mut hmac = Hmac::new(Sha512::new(), &hmac_key);
168-
hmac.input(&concatenated);
169-
Ok(BASE64.encode(hmac.result().code()))
164+
let mut mac = Hmac::<Sha512>::new(&hmac_key[..]);
165+
mac.input(&concatenated);
166+
Ok(BASE64.encode(mac.result().code()))
170167
}
171168

172169
/// Result: Server's time

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
#[macro_use]
3232
extern crate hyper;
33-
extern crate crypto;
33+
extern crate sha2;
34+
extern crate hmac;
3435
extern crate hyper_native_tls;
3536
extern crate serde_json;
3637
extern crate time;

src/poloniex/api.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Use this module to interact with Poloniex exchange.
22
//! See examples for more informations.
33
4-
use crypto::hmac::Hmac;
5-
use crypto::mac::Mac;
6-
use crypto::sha2::Sha512;
4+
use hmac::{Hmac, Mac};
5+
use sha2::{Sha512};
76

87
use hyper_native_tls::NativeTlsClient;
98
use hyper::Client;
@@ -113,10 +112,10 @@ impl PoloniexApi {
113112
helpers::strip_empties(&mut post_params);
114113
let post_data = helpers::url_encode_hashmap(&post_params);
115114

116-
let mut hmac = Hmac::new(Sha512::new(), self.api_secret.as_bytes());
117-
hmac.input(post_data.as_bytes());
115+
let mut mac = Hmac::<Sha512>::new(self.api_secret.as_bytes());
116+
mac.input(post_data.as_bytes());
118117

119-
let sign = HEXLOWER.encode(hmac.result().code());
118+
let sign = HEXLOWER.encode(mac.result().code());
120119

121120
let mut custom_header = header::Headers::new();
122121
custom_header.set(KeyHeader(self.api_key.to_owned()));

0 commit comments

Comments
 (0)