hashes: Make the hash a Vec<u8>, to avoid base64 issues.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-25 00:30:29 +01:00
commit a6b3152add
3 changed files with 27 additions and 20 deletions

View file

@ -13,6 +13,8 @@ use error::Error;
use ns;
use base64;
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq)]
pub enum Algo {
@ -63,7 +65,7 @@ impl IntoAttributeValue for Algo {
#[derive(Debug, Clone, PartialEq)]
pub struct Hash {
pub algo: Algo,
pub hash: String,
pub hash: Vec<u8>,
}
impl TryFrom<Element> for Hash {
@ -79,7 +81,7 @@ impl TryFrom<Element> for Hash {
let algo = get_attr!(elem, "algo", required);
let hash = match elem.text().as_ref() {
"" => return Err(Error::ParseError("Hash element shouldnt be empty.")),
text => text.to_owned(),
text => base64::decode(text)?,
};
Ok(Hash {
algo: algo,
@ -93,7 +95,7 @@ impl Into<Element> for Hash {
Element::builder("hash")
.ns(ns::HASHES)
.attr("algo", self.algo)
.append(self.hash.clone())
.append(base64::encode(&self.hash))
.build()
}
}
@ -107,7 +109,7 @@ mod tests {
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
let hash = Hash::try_from(elem).unwrap();
assert_eq!(hash.algo, Algo::Sha_256);
assert_eq!(hash.hash, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
assert_eq!(hash.hash, base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap());
}
#[test]