ecaps2: Implement blake2b.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-23 19:28:03 +01:00
parent 2c95fd6786
commit 948b54d073
3 changed files with 21 additions and 6 deletions

View file

@ -7,6 +7,7 @@ authors = ["Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"]
minidom = "0.1.1" minidom = "0.1.1"
jid = "0.2.0" jid = "0.2.0"
base64 = "0.4.1" base64 = "0.4.1"
digest = "0.5.0"
sha2 = "0.5.0" sha2 = "0.5.0"
sha3 = "0.5.0" sha3 = "0.5.0"
blake2 = "0.5.0" blake2 = "0.5.0"

View file

@ -14,6 +14,7 @@ use ns;
use self::sha2::{Sha256, Sha512, Digest}; use self::sha2::{Sha256, Sha512, Digest};
use self::sha3::{Sha3_256, Sha3_512}; use self::sha3::{Sha3_256, Sha3_512};
use self::blake2::Blake2b; use self::blake2::Blake2b;
use digest::VariableOutput;
use base64; use base64;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -137,22 +138,20 @@ pub fn hash_ecaps2(data: &[u8], algo: &str) -> String {
let hash = hasher.result(); let hash = hasher.result();
base64::encode(&hash) base64::encode(&hash)
}, },
/*
"blake2b-256" => { "blake2b-256" => {
// TODO: bit length is most likely wrong here!
let mut hasher = Blake2b::default(); let mut hasher = Blake2b::default();
hasher.input(data); hasher.input(data);
let hash = hasher.result(); let mut buf: [u8; 32] = [0; 32];
let hash = hasher.variable_result(&mut buf).unwrap();
base64::encode(&hash) base64::encode(&hash)
}, },
"blake2b-512" => { "blake2b-512" => {
// TODO: bit length is most likely wrong here!
let mut hasher = Blake2b::default(); let mut hasher = Blake2b::default();
hasher.input(data); hasher.input(data);
let hash = hasher.result(); let mut buf: [u8; 64] = [0; 64];
let hash = hasher.variable_result(&mut buf).unwrap();
base64::encode(&hash) base64::encode(&hash)
}, },
*/
_ => panic!(), _ => panic!(),
} }
} }
@ -163,6 +162,7 @@ mod tests {
use error::Error; use error::Error;
use disco; use disco;
use ecaps2; use ecaps2;
use base64;
#[test] #[test]
fn test_parse() { fn test_parse() {
@ -434,4 +434,17 @@ mod tests {
let sha3_256 = ecaps2::hash_ecaps2(&ecaps2, "sha3-256"); let sha3_256 = ecaps2::hash_ecaps2(&ecaps2, "sha3-256");
assert_eq!(sha3_256, "XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg="); assert_eq!(sha3_256, "XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg=");
} }
#[test]
fn test_blake2b_512() {
let hash = ecaps2::hash_ecaps2("abc".as_bytes(), "blake2b-512");
let known_hash: [u8; 64] = [
0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D, 0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9,
0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A, 0xC4, 0xB7, 0x4B, 0x12, 0xBB, 0x6F, 0xDB, 0xFF, 0xA2, 0xD1,
0x7D, 0x87, 0xC5, 0x39, 0x2A, 0xAB, 0x79, 0x2D, 0xC2, 0x52, 0xD5, 0xDE, 0x45, 0x33, 0xCC, 0x95,
0x18, 0xD3, 0x8A, 0xA8, 0xDB, 0xF1, 0x92, 0x5A, 0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23,
];
let known_hash = base64::encode(&known_hash);
assert_eq!(hash, known_hash);
}
} }

View file

@ -10,6 +10,7 @@
extern crate minidom; extern crate minidom;
extern crate jid; extern crate jid;
extern crate base64; extern crate base64;
extern crate digest;
/// Error type returned by every parser on failure. /// Error type returned by every parser on failure.
pub mod error; pub mod error;