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::{Element, IntoAttributeValue};
10
11use error::Error;
12
13use ns;
14use helpers::Base64;
15use base64;
16
17#[allow(non_camel_case_types)]
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub enum Algo {
20 Sha_1,
21 Sha_256,
22 Sha_512,
23 Sha3_256,
24 Sha3_512,
25 Blake2b_256,
26 Blake2b_512,
27 Unknown(String),
28}
29
30impl FromStr for Algo {
31 type Err = Error;
32
33 fn from_str(s: &str) -> Result<Algo, Error> {
34 Ok(match s {
35 "" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
36
37 "sha-1" => Algo::Sha_1,
38 "sha-256" => Algo::Sha_256,
39 "sha-512" => Algo::Sha_512,
40 "sha3-256" => Algo::Sha3_256,
41 "sha3-512" => Algo::Sha3_512,
42 "blake2b-256" => Algo::Blake2b_256,
43 "blake2b-512" => Algo::Blake2b_512,
44 value => Algo::Unknown(value.to_owned()),
45 })
46 }
47}
48
49impl From<Algo> for String {
50 fn from(algo: Algo) -> String {
51 String::from(match algo {
52 Algo::Sha_1 => "sha-1",
53 Algo::Sha_256 => "sha-256",
54 Algo::Sha_512 => "sha-512",
55 Algo::Sha3_256 => "sha3-256",
56 Algo::Sha3_512 => "sha3-512",
57 Algo::Blake2b_256 => "blake2b-256",
58 Algo::Blake2b_512 => "blake2b-512",
59 Algo::Unknown(text) => return text,
60 })
61 }
62}
63
64impl IntoAttributeValue for Algo {
65 fn into_attribute_value(self) -> Option<String> {
66 Some(String::from(self))
67 }
68}
69
70generate_element_with_text!(
71 #[derive(PartialEq)]
72 Hash, "hash", ns::HASHES,
73 [
74 algo: Algo = "algo" => required
75 ],
76 hash: Base64<Vec<u8>>
77);
78
79impl Hash {
80 pub fn new(algo: Algo, hash: Vec<u8>) -> Hash {
81 Hash {
82 algo,
83 hash,
84 }
85 }
86
87 pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
88 Ok(Hash::new(algo, base64::decode(hash)?))
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95 use try_from::TryFrom;
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}