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 try_from::TryFrom;
8use std::str::FromStr;
9
10use minidom::{Element, IntoAttributeValue};
11
12use error::Error;
13
14use ns;
15use helpers::Base64;
16use base64;
17
18#[allow(non_camel_case_types)]
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20pub enum Algo {
21 Sha_1,
22 Sha_256,
23 Sha_512,
24 Sha3_256,
25 Sha3_512,
26 Blake2b_256,
27 Blake2b_512,
28 Unknown(String),
29}
30
31impl FromStr for Algo {
32 type Err = Error;
33
34 fn from_str(s: &str) -> Result<Algo, Error> {
35 Ok(match s {
36 "" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
37
38 "sha-1" => Algo::Sha_1,
39 "sha-256" => Algo::Sha_256,
40 "sha-512" => Algo::Sha_512,
41 "sha3-256" => Algo::Sha3_256,
42 "sha3-512" => Algo::Sha3_512,
43 "blake2b-256" => Algo::Blake2b_256,
44 "blake2b-512" => Algo::Blake2b_512,
45 value => Algo::Unknown(value.to_owned()),
46 })
47 }
48}
49
50impl From<Algo> for String {
51 fn from(algo: Algo) -> String {
52 String::from(match algo {
53 Algo::Sha_1 => "sha-1",
54 Algo::Sha_256 => "sha-256",
55 Algo::Sha_512 => "sha-512",
56 Algo::Sha3_256 => "sha3-256",
57 Algo::Sha3_512 => "sha3-512",
58 Algo::Blake2b_256 => "blake2b-256",
59 Algo::Blake2b_512 => "blake2b-512",
60 Algo::Unknown(text) => return text,
61 })
62 }
63}
64
65impl IntoAttributeValue for Algo {
66 fn into_attribute_value(self) -> Option<String> {
67 Some(String::from(self))
68 }
69}
70
71generate_element_with_text!(
72 #[derive(PartialEq)]
73 Hash, "hash", ns::HASHES,
74 [
75 algo: Algo = "algo" => required
76 ],
77 hash: Base64<Vec<u8>>
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
97 #[test]
98 fn test_simple() {
99 let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
100 let hash = Hash::try_from(elem).unwrap();
101 assert_eq!(hash.algo, Algo::Sha_256);
102 assert_eq!(hash.hash, base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap());
103 }
104
105 #[test]
106 fn test_unknown() {
107 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
108 let error = Hash::try_from(elem).unwrap_err();
109 let message = match error {
110 Error::ParseError(string) => string,
111 _ => panic!(),
112 };
113 assert_eq!(message, "This is not a hash element.");
114 }
115
116 #[test]
117 fn test_invalid_child() {
118 let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
119 let error = Hash::try_from(elem).unwrap_err();
120 let message = match error {
121 Error::ParseError(string) => string,
122 _ => panic!(),
123 };
124 assert_eq!(message, "Unknown child in hash element.");
125 }
126}