Merge branch 'update-crypto'

master
Ondřej Hruška 5 years ago
commit 2cd02e3eb5
Signed by untrusted user: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      Cargo.toml
  2. 8
      src/digest.rs
  3. 13
      src/enums.rs
  4. 2
      src/lib.rs

@ -1,6 +1,6 @@
[package] [package]
name = "digest_auth" name = "digest_auth"
version = "0.2.1" version = "0.2.2"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"] authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
edition = "2018" edition = "2018"
description = "Implementation of the Digest Auth algorithm as defined in IETF RFC 2069, 2617, and 7616, intended for HTTP clients" 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" license = "MIT"
[dependencies] [dependencies]
rust-crypto = "0.2"
rand = "0.6" rand = "0.6"
hex = "0.3.2" hex = "0.3.2"
sha2 = "0.8.0"
md-5 = "0.8.0"
digest = "0.8.1"

@ -32,7 +32,7 @@ impl QuoteForDigest for String {
} }
/// Join a Vec of Display items using a separator /// Join a Vec of Display items using a separator
fn join_vec<T: ToString>(vec: &Vec<T>, sep: &str) -> String { fn join_vec<T: ToString>(vec: &[T], sep: &str) -> String {
vec.iter() vec.iter()
.map(ToString::to_string) .map(ToString::to_string)
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -507,8 +507,8 @@ impl AuthorizationHeader {
None => { None => {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let nonce_bytes: [u8; 16] = rng.gen(); 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.qop.is_some() {
if !auth.cnonce.is_some() { if auth.cnonce.is_none() {
return Err(MissingRequired("cnonce", input.into())); return Err(MissingRequired("cnonce", input.into()));
} }
} else { } else {

@ -1,9 +1,12 @@
use crate::{Error, Error::*, Result}; use crate::{Error, Error::*, Result};
use crypto::{digest::Digest, md5::Md5, sha2::Sha256, sha2::Sha512Trunc256};
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
use digest::{Digest, DynDigest};
use md5::Md5;
use sha2::{Sha256, Sha512Trunc256};
/// Algorithm type /// Algorithm type
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
@ -27,19 +30,19 @@ impl Algorithm {
} }
/// Calculate a hash of bytes using the selected algorithm /// Calculate a hash of bytes using the selected algorithm
pub fn hash(&self, bytes: &[u8]) -> String { pub fn hash(self, bytes: &[u8]) -> String {
let mut hash: Box<dyn Digest> = match self.algo { let mut hash: Box<dyn DynDigest> = match self.algo {
AlgorithmType::MD5 => Box::new(Md5::new()), AlgorithmType::MD5 => Box::new(Md5::new()),
AlgorithmType::SHA2_256 => Box::new(Sha256::new()), AlgorithmType::SHA2_256 => Box::new(Sha256::new()),
AlgorithmType::SHA2_512_256 => Box::new(Sha512Trunc256::new()), AlgorithmType::SHA2_512_256 => Box::new(Sha512Trunc256::new()),
}; };
hash.input(bytes); hash.input(bytes);
hash.result_str() hex::encode(hash.result())
} }
/// Calculate a hash of string's bytes using the selected 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()) self.hash(bytes.as_bytes())
} }
} }

@ -101,5 +101,5 @@ Digest username="Mufasa",
#[test] #[test]
fn test_cast_error() { fn test_cast_error() {
let _m : Box<dyn std::error::Error> = Error::UnknownAlgorithm("Uhhh".into()).into(); let _m: Box<dyn std::error::Error> = Error::UnknownAlgorithm("Uhhh".into()).into();
} }

Loading…
Cancel
Save