From efedb215c4933cef80114c9e0f774ea7a43fec1b Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 21 Apr 2017 00:01:57 +0100 Subject: [PATCH] ecaps2: Add Rust-Crypto and base64 dependencies, and implement hashing. --- Cargo.toml | 4 ++++ src/ecaps2.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10c55dd..7102bc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,7 @@ authors = ["Emmanuel Gil Peyrot "] [dependencies] minidom = "0.1.1" +base64 = "0.4.1" +sha2 = "0.5.0" +sha3 = "0.5.0" +blake2 = "0.5.0" diff --git a/src/ecaps2.rs b/src/ecaps2.rs index fddc712..21efc9c 100644 --- a/src/ecaps2.rs +++ b/src/ecaps2.rs @@ -1,4 +1,8 @@ extern crate minidom; +extern crate sha2; +extern crate sha3; +extern crate blake2; +extern crate base64; use minidom::Element; @@ -7,6 +11,10 @@ use error::Error; use disco::{Feature, Identity, Disco, parse_disco}; use data_forms::DataForm; +use self::sha2::{Sha256, Sha512, Digest}; +use self::sha3::{Sha3_256, Sha3_512}; +use self::blake2::{Blake2b}; + fn compute_item(field: &String) -> Vec { let mut bytes = field.as_bytes().to_vec(); bytes.push(0x1f); @@ -73,6 +81,52 @@ pub fn convert_element(root: &Element) -> Result, Error> { Ok(final_string) } +pub fn hash_ecaps2(data: &Vec, algo: String) -> String { + match algo.as_ref() { + "sha-256" => { + let mut hasher = Sha256::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + "sha-512" => { + let mut hasher = Sha512::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + "sha3-256" => { + let mut hasher = Sha3_256::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + "sha3-512" => { + let mut hasher = Sha3_512::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + /* + "blake2b-256" => { + // TODO: bit length is most likely wrong here! + let mut hasher = Blake2b::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + "blake2b-512" => { + // TODO: bit length is most likely wrong here! + let mut hasher = Blake2b::default(); + hasher.input(data); + let hash = hasher.result(); + base64::encode(&hash) + }, + */ + _ => panic!(), + } +} + #[cfg(test)] mod tests { use minidom::Element; @@ -147,12 +201,10 @@ mod tests { assert_eq!(ecaps2.len(), 0x1d9); assert_eq!(ecaps2, expected); - /* - let sha_256 = hash(ecaps2, "sha-256"); + let sha_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha-256")); assert_eq!(sha_256, "kzBZbkqJ3ADrj7v08reD1qcWUwNGHaidNUgD7nHpiw8="); - let sha3_256 = hash(ecaps2, "sha3-256"); + let sha3_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha3-256")); assert_eq!(sha3_256, "79mdYAfU9rEdTOcWDO7UEAt6E56SUzk/g6TnqUeuD9Q="); - */ } #[test] @@ -320,11 +372,9 @@ mod tests { assert_eq!(ecaps2.len(), 0x543); assert_eq!(ecaps2, expected); - /* - let sha_256 = hash(ecaps2, "sha-256"); + let sha_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha-256")); assert_eq!(sha_256, "u79ZroNJbdSWhdSp311mddz44oHHPsEBntQ5b1jqBSY="); - let sha3_256 = hash(ecaps2, "sha3-256"); + let sha3_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha3-256")); assert_eq!(sha3_256, "XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg="); - */ } }