From 12f158a3d66b685cfbc616955c2afc2a09397876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 6 Dec 2019 22:02:41 +0100 Subject: [PATCH 1/2] update crypto dependencies to new crates --- Cargo.toml | 6 ++++-- src/enums.rs | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 97deced..02894ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "digest_auth" -version = "0.2.1" +version = "0.2.2" authors = ["Ondřej Hruška "] edition = "2018" description = "Implementation of the Digest Auth algorithm as defined in IETF RFC 2069, 2617, and 7616, intended for HTTP clients" @@ -15,6 +15,8 @@ categories = [ license = "MIT" [dependencies] -rust-crypto = "0.2" rand = "0.6" hex = "0.3.2" +sha2 = "0.8.0" +md-5 = "0.8.0" +digest = "0.8.1" diff --git a/src/enums.rs b/src/enums.rs index 39331e6..87eeb57 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -1,9 +1,12 @@ use crate::{Error, Error::*, Result}; -use crypto::{digest::Digest, md5::Md5, sha2::Sha256, sha2::Sha512Trunc256}; use std::fmt; use std::fmt::{Display, Formatter}; use std::str::FromStr; +use digest::{Digest, DynDigest}; +use md5::Md5; +use sha2::{Sha256, Sha512Trunc256}; + /// Algorithm type #[derive(Debug, PartialEq, Clone, Copy)] #[allow(non_camel_case_types)] @@ -28,14 +31,14 @@ impl Algorithm { /// Calculate a hash of bytes using the selected algorithm pub fn hash(&self, bytes: &[u8]) -> String { - let mut hash: Box = match self.algo { + let mut hash : Box = match self.algo { AlgorithmType::MD5 => Box::new(Md5::new()), AlgorithmType::SHA2_256 => Box::new(Sha256::new()), AlgorithmType::SHA2_512_256 => Box::new(Sha512Trunc256::new()), }; hash.input(bytes); - hash.result_str() + hex::encode(hash.result()) } /// Calculate a hash of string's bytes using the selected algorithm From 8f9cc6a66030e32601eeb45047bc380ed67e3fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 6 Dec 2019 22:07:49 +0100 Subject: [PATCH 2/2] fmt and clippy fixes. Algorithm enum functions now take self by value (it's Copy and only 2 bytes). --- src/digest.rs | 8 ++++---- src/enums.rs | 6 +++--- src/lib.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/digest.rs b/src/digest.rs index 3f1b4ae..4e1e0b4 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -32,7 +32,7 @@ impl QuoteForDigest for String { } /// Join a Vec of Display items using a separator -fn join_vec(vec: &Vec, sep: &str) -> String { +fn join_vec(vec: &[T], sep: &str) -> String { vec.iter() .map(ToString::to_string) .collect::>() @@ -507,8 +507,8 @@ impl AuthorizationHeader { None => { let mut rng = rand::thread_rng(); let nonce_bytes: [u8; 16] = rng.gen(); - let cnonce = hex::encode(nonce_bytes); - cnonce + + hex::encode(nonce_bytes) } } }; @@ -654,7 +654,7 @@ impl AuthorizationHeader { }; if auth.qop.is_some() { - if !auth.cnonce.is_some() { + if auth.cnonce.is_none() { return Err(MissingRequired("cnonce", input.into())); } } else { diff --git a/src/enums.rs b/src/enums.rs index 87eeb57..c6a60a9 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -30,8 +30,8 @@ impl Algorithm { } /// Calculate a hash of bytes using the selected algorithm - pub fn hash(&self, bytes: &[u8]) -> String { - let mut hash : Box = match self.algo { + pub fn hash(self, bytes: &[u8]) -> String { + let mut hash: Box = match self.algo { AlgorithmType::MD5 => Box::new(Md5::new()), AlgorithmType::SHA2_256 => Box::new(Sha256::new()), AlgorithmType::SHA2_512_256 => Box::new(Sha512Trunc256::new()), @@ -42,7 +42,7 @@ impl Algorithm { } /// Calculate a hash of string's bytes using the selected algorithm - pub fn hash_str(&self, bytes: &str) -> String { + pub fn hash_str(self, bytes: &str) -> String { self.hash(bytes.as_bytes()) } } diff --git a/src/lib.rs b/src/lib.rs index 3f1c870..371453b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,5 +101,5 @@ Digest username="Mufasa", #[test] fn test_cast_error() { - let _m : Box = Error::UnknownAlgorithm("Uhhh".into()).into(); + let _m: Box = Error::UnknownAlgorithm("Uhhh".into()).into(); }