hashes.rs

  1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7use std::str::FromStr;
  8
  9use minidom::IntoAttributeValue;
 10
 11use error::Error;
 12
 13use helpers::Base64;
 14use base64;
 15
 16#[allow(non_camel_case_types)]
 17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 18pub enum Algo {
 19    Sha_1,
 20    Sha_256,
 21    Sha_512,
 22    Sha3_256,
 23    Sha3_512,
 24    Blake2b_256,
 25    Blake2b_512,
 26    Unknown(String),
 27}
 28
 29impl FromStr for Algo {
 30    type Err = Error;
 31
 32    fn from_str(s: &str) -> Result<Algo, Error> {
 33        Ok(match s {
 34            "" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
 35
 36            "sha-1" => Algo::Sha_1,
 37            "sha-256" => Algo::Sha_256,
 38            "sha-512" => Algo::Sha_512,
 39            "sha3-256" => Algo::Sha3_256,
 40            "sha3-512" => Algo::Sha3_512,
 41            "blake2b-256" => Algo::Blake2b_256,
 42            "blake2b-512" => Algo::Blake2b_512,
 43            value => Algo::Unknown(value.to_owned()),
 44        })
 45    }
 46}
 47
 48impl From<Algo> for String {
 49    fn from(algo: Algo) -> String {
 50        String::from(match algo {
 51            Algo::Sha_1 => "sha-1",
 52            Algo::Sha_256 => "sha-256",
 53            Algo::Sha_512 => "sha-512",
 54            Algo::Sha3_256 => "sha3-256",
 55            Algo::Sha3_512 => "sha3-512",
 56            Algo::Blake2b_256 => "blake2b-256",
 57            Algo::Blake2b_512 => "blake2b-512",
 58            Algo::Unknown(text) => return text,
 59        })
 60    }
 61}
 62
 63impl IntoAttributeValue for Algo {
 64    fn into_attribute_value(self) -> Option<String> {
 65        Some(String::from(self))
 66    }
 67}
 68
 69generate_element!(
 70    #[derive(PartialEq)]
 71    Hash, "hash", HASHES,
 72    attributes: [
 73        algo: Algo = "algo" => required
 74    ],
 75    text: (
 76        hash: Base64<Vec<u8>>
 77    )
 78);
 79
 80impl Hash {
 81    pub fn new(algo: Algo, hash: Vec<u8>) -> Hash {
 82        Hash {
 83            algo,
 84            hash,
 85        }
 86    }
 87
 88    pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
 89        Ok(Hash::new(algo, base64::decode(hash)?))
 90    }
 91}
 92
 93#[cfg(test)]
 94mod tests {
 95    use super::*;
 96    use try_from::TryFrom;
 97    use minidom::Element;
 98
 99    #[test]
100    fn test_simple() {
101        let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
102        let hash = Hash::try_from(elem).unwrap();
103        assert_eq!(hash.algo, Algo::Sha_256);
104        assert_eq!(hash.hash, base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap());
105    }
106
107    #[test]
108    fn test_unknown() {
109        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
110        let error = Hash::try_from(elem).unwrap_err();
111        let message = match error {
112            Error::ParseError(string) => string,
113            _ => panic!(),
114        };
115        assert_eq!(message, "This is not a hash element.");
116    }
117
118    #[test]
119    fn test_invalid_child() {
120        let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
121        let error = Hash::try_from(elem).unwrap_err();
122        let message = match error {
123            Error::ParseError(string) => string,
124            _ => panic!(),
125        };
126        assert_eq!(message, "Unknown child in hash element.");
127    }
128}