caps: Do the same fixes, unbreak blake2 and simplify code.

This commit is contained in:
Emmanuel Gil Peyrot 2017-11-15 23:16:37 +00:00
parent 702c4d2932
commit 9c9ffe70a7

View file

@ -15,12 +15,11 @@ use error::Error;
use ns; use ns;
use base64; use base64;
use digest::Digest;
use sha1::Sha1; use sha1::Sha1;
use sha2::{Sha256, Sha512}; use sha2::{Sha256, Sha512};
use sha3::{Sha3_256, Sha3_512}; use sha3::{Sha3_256, Sha3_512};
//use blake2::Blake2b; use blake2::Blake2b;
//use digest::{Digest, VariableOutput}; use digest::{Digest, VariableOutput};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Caps { pub struct Caps {
@ -141,38 +140,25 @@ pub fn hash_caps(data: &[u8], algo: Algo) -> Result<Hash, String> {
Ok(Hash { Ok(Hash {
hash: match algo { hash: match algo {
Algo::Sha_1 => { Algo::Sha_1 => {
let mut hasher = Sha1::default(); let hash = Sha1::digest(data);
hasher.input(data);
let hash = hasher.result();
get_hash_vec(hash.as_slice()) get_hash_vec(hash.as_slice())
}, },
Algo::Sha_256 => { Algo::Sha_256 => {
let mut hasher = Sha256::default(); let hash = Sha256::digest(data);
hasher.input(data);
let hash = hasher.result();
get_hash_vec(hash.as_slice()) get_hash_vec(hash.as_slice())
}, },
Algo::Sha_512 => { Algo::Sha_512 => {
let mut hasher = Sha512::default(); let hash = Sha512::digest(data);
hasher.input(data);
let hash = hasher.result();
get_hash_vec(hash.as_slice()) get_hash_vec(hash.as_slice())
}, },
Algo::Sha3_256 => { Algo::Sha3_256 => {
let mut hasher = Sha3_256::default(); let hash = Sha3_256::digest(data);
hasher.input(data);
let hash = hasher.result();
get_hash_vec(hash.as_slice()) get_hash_vec(hash.as_slice())
}, },
Algo::Sha3_512 => { Algo::Sha3_512 => {
let mut hasher = Sha3_512::default(); let hash = Sha3_512::digest(data);
hasher.input(data);
let hash = hasher.result();
get_hash_vec(hash.as_slice()) get_hash_vec(hash.as_slice())
}, },
Algo::Blake2b_256
| Algo::Blake2b_512 => panic!("See https://github.com/RustCrypto/hashes/issues/34"),
/*
Algo::Blake2b_256 => { Algo::Blake2b_256 => {
let mut hasher = Blake2b::default(); let mut hasher = Blake2b::default();
hasher.input(data); hasher.input(data);
@ -187,7 +173,6 @@ pub fn hash_caps(data: &[u8], algo: Algo) -> Result<Hash, String> {
let hash = hasher.variable_result(&mut buf).unwrap(); let hash = hasher.variable_result(&mut buf).unwrap();
get_hash_vec(hash) get_hash_vec(hash)
}, },
*/
Algo::Unknown(algo) => return Err(format!("Unknown algorithm: {}.", algo)), Algo::Unknown(algo) => return Err(format!("Unknown algorithm: {}.", algo)),
}, },
algo: algo, algo: algo,