2017-04-29 23:14:34 +02:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2018-08-08 18:48:05 +02:00
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
2017-05-19 00:09:29 +02:00
|
|
|
|
use std::str::FromStr;
|
2017-05-06 21:46:11 +02:00
|
|
|
|
|
2018-05-14 16:17:21 +02:00
|
|
|
|
use minidom::IntoAttributeValue;
|
2017-04-21 05:21:16 +02:00
|
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
2017-11-23 16:52:06 +01:00
|
|
|
|
use helpers::Base64;
|
2018-05-04 19:10:04 +02:00
|
|
|
|
use base64;
|
2017-05-25 01:30:29 +02:00
|
|
|
|
|
2017-05-19 00:09:29 +02:00
|
|
|
|
#[allow(non_camel_case_types)]
|
2017-05-29 06:14:49 +02:00
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2017-05-19 00:09:29 +02:00
|
|
|
|
pub enum Algo {
|
|
|
|
|
Sha_1,
|
|
|
|
|
Sha_256,
|
|
|
|
|
Sha_512,
|
|
|
|
|
Sha3_256,
|
|
|
|
|
Sha3_512,
|
|
|
|
|
Blake2b_256,
|
|
|
|
|
Blake2b_512,
|
|
|
|
|
Unknown(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for Algo {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Algo, Error> {
|
|
|
|
|
Ok(match s {
|
|
|
|
|
"" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
|
|
|
|
|
|
|
|
|
|
"sha-1" => Algo::Sha_1,
|
|
|
|
|
"sha-256" => Algo::Sha_256,
|
|
|
|
|
"sha-512" => Algo::Sha_512,
|
|
|
|
|
"sha3-256" => Algo::Sha3_256,
|
|
|
|
|
"sha3-512" => Algo::Sha3_512,
|
|
|
|
|
"blake2b-256" => Algo::Blake2b_256,
|
|
|
|
|
"blake2b-512" => Algo::Blake2b_512,
|
|
|
|
|
value => Algo::Unknown(value.to_owned()),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 13:22:11 +02:00
|
|
|
|
impl From<Algo> for String {
|
|
|
|
|
fn from(algo: Algo) -> String {
|
|
|
|
|
String::from(match algo {
|
2017-05-19 00:09:29 +02:00
|
|
|
|
Algo::Sha_1 => "sha-1",
|
|
|
|
|
Algo::Sha_256 => "sha-256",
|
|
|
|
|
Algo::Sha_512 => "sha-512",
|
|
|
|
|
Algo::Sha3_256 => "sha3-256",
|
|
|
|
|
Algo::Sha3_512 => "sha3-512",
|
|
|
|
|
Algo::Blake2b_256 => "blake2b-256",
|
|
|
|
|
Algo::Blake2b_512 => "blake2b-512",
|
2017-05-27 13:22:11 +02:00
|
|
|
|
Algo::Unknown(text) => return text,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoAttributeValue for Algo {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(String::from(self))
|
2017-05-19 00:09:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:07:07 +02:00
|
|
|
|
generate_element!(
|
2017-11-23 16:52:06 +01:00
|
|
|
|
#[derive(PartialEq)]
|
2018-05-14 16:30:28 +02:00
|
|
|
|
Hash, "hash", HASHES,
|
2018-08-02 19:07:07 +02:00
|
|
|
|
attributes: [
|
2017-11-23 16:52:06 +01:00
|
|
|
|
algo: Algo = "algo" => required
|
|
|
|
|
],
|
2018-08-02 19:07:07 +02:00
|
|
|
|
text: (
|
|
|
|
|
hash: Base64<Vec<u8>>
|
|
|
|
|
)
|
2017-11-23 16:52:06 +01:00
|
|
|
|
);
|
2017-04-23 19:36:12 +02:00
|
|
|
|
|
2018-05-04 19:10:04 +02:00
|
|
|
|
impl Hash {
|
|
|
|
|
pub fn new(algo: Algo, hash: Vec<u8>) -> Hash {
|
|
|
|
|
Hash {
|
|
|
|
|
algo,
|
|
|
|
|
hash,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
|
|
|
|
|
Ok(Hash::new(algo, base64::decode(hash)?))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 05:21:16 +02:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 21:46:11 +02:00
|
|
|
|
use super::*;
|
2018-05-14 16:07:15 +02:00
|
|
|
|
use try_from::TryFrom;
|
2018-05-14 16:17:21 +02:00
|
|
|
|
use minidom::Element;
|
2017-04-21 05:21:16 +02:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
|
2017-05-24 00:31:33 +02:00
|
|
|
|
let hash = Hash::try_from(elem).unwrap();
|
2017-05-19 00:09:29 +02:00
|
|
|
|
assert_eq!(hash.algo, Algo::Sha_256);
|
2017-05-25 01:30:29 +02:00
|
|
|
|
assert_eq!(hash.hash, base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap());
|
2017-04-21 05:21:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unknown() {
|
|
|
|
|
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
2017-05-24 00:31:33 +02:00
|
|
|
|
let error = Hash::try_from(elem).unwrap_err();
|
2017-04-21 05:21:16 +02:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "This is not a hash element.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
|
|
|
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
|
2017-05-24 00:31:33 +02:00
|
|
|
|
let error = Hash::try_from(elem).unwrap_err();
|
2017-04-21 05:21:16 +02:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown child in hash element.");
|
|
|
|
|
}
|
|
|
|
|
}
|