ecaps2: Add Rust-Crypto and base64 dependencies, and implement hashing.
This commit is contained in:
parent
7288c2c74f
commit
efedb215c4
2 changed files with 62 additions and 8 deletions
|
@ -5,3 +5,7 @@ authors = ["Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
minidom = "0.1.1"
|
minidom = "0.1.1"
|
||||||
|
base64 = "0.4.1"
|
||||||
|
sha2 = "0.5.0"
|
||||||
|
sha3 = "0.5.0"
|
||||||
|
blake2 = "0.5.0"
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
extern crate minidom;
|
extern crate minidom;
|
||||||
|
extern crate sha2;
|
||||||
|
extern crate sha3;
|
||||||
|
extern crate blake2;
|
||||||
|
extern crate base64;
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
|
||||||
|
@ -7,6 +11,10 @@ use error::Error;
|
||||||
use disco::{Feature, Identity, Disco, parse_disco};
|
use disco::{Feature, Identity, Disco, parse_disco};
|
||||||
use data_forms::DataForm;
|
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<u8> {
|
fn compute_item(field: &String) -> Vec<u8> {
|
||||||
let mut bytes = field.as_bytes().to_vec();
|
let mut bytes = field.as_bytes().to_vec();
|
||||||
bytes.push(0x1f);
|
bytes.push(0x1f);
|
||||||
|
@ -73,6 +81,52 @@ pub fn convert_element(root: &Element) -> Result<Vec<u8>, Error> {
|
||||||
Ok(final_string)
|
Ok(final_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hash_ecaps2(data: &Vec<u8>, 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
@ -147,12 +201,10 @@ mod tests {
|
||||||
assert_eq!(ecaps2.len(), 0x1d9);
|
assert_eq!(ecaps2.len(), 0x1d9);
|
||||||
assert_eq!(ecaps2, expected);
|
assert_eq!(ecaps2, expected);
|
||||||
|
|
||||||
/*
|
let sha_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha-256"));
|
||||||
let sha_256 = hash(ecaps2, "sha-256");
|
|
||||||
assert_eq!(sha_256, "kzBZbkqJ3ADrj7v08reD1qcWUwNGHaidNUgD7nHpiw8=");
|
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=");
|
assert_eq!(sha3_256, "79mdYAfU9rEdTOcWDO7UEAt6E56SUzk/g6TnqUeuD9Q=");
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -320,11 +372,9 @@ mod tests {
|
||||||
assert_eq!(ecaps2.len(), 0x543);
|
assert_eq!(ecaps2.len(), 0x543);
|
||||||
assert_eq!(ecaps2, expected);
|
assert_eq!(ecaps2, expected);
|
||||||
|
|
||||||
/*
|
let sha_256 = ecaps2::hash_ecaps2(&ecaps2, String::from("sha-256"));
|
||||||
let sha_256 = hash(ecaps2, "sha-256");
|
|
||||||
assert_eq!(sha_256, "u79ZroNJbdSWhdSp311mddz44oHHPsEBntQ5b1jqBSY=");
|
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=");
|
assert_eq!(sha3_256, "XpUJzLAc93258sMECZ3FJpebkzuyNXDzRNwQog8eycg=");
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue