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_with_text!(
70 #[derive(PartialEq)]
71 Hash, "hash", HASHES,
72 [
73 algo: Algo = "algo" => required
74 ],
75 hash: Base64<Vec<u8>>
76);
77
78impl Hash {
79 pub fn new(algo: Algo, hash: Vec<u8>) -> Hash {
80 Hash {
81 algo,
82 hash,
83 }
84 }
85
86 pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
87 Ok(Hash::new(algo, base64::decode(hash)?))
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use try_from::TryFrom;
95 use minidom::Element;
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}