From d83624c8a49918f72e9c5c527355961506859668 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 1 Mar 2018 17:32:50 +0100 Subject: [PATCH] component: Add constructors. --- src/component.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/component.rs b/src/component.rs index f9ac62fd..0f42e50a 100644 --- a/src/component.rs +++ b/src/component.rs @@ -11,10 +11,30 @@ use error::Error; use helpers::PlainText; use ns; +use sha1::Sha1; +use digest::Digest; + generate_element_with_text!(Handshake, "handshake", ns::COMPONENT, data: PlainText> ); +impl Handshake { + pub fn new() -> Handshake { + Handshake { + data: None, + } + } + + pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake { + let input = String::from(stream_id) + password; + let hash = Sha1::digest(input.as_bytes()); + let content = format!("{:x}", hash); + Handshake { + data: Some(content), + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -29,4 +49,13 @@ mod tests { let handshake = Handshake::try_from(elem).unwrap(); assert_eq!(handshake.data, Some(String::from("Coucou"))); } + + #[test] + fn test_constructors() { + let handshake = Handshake::new(); + assert_eq!(handshake.data, None); + + let handshake = Handshake::from_password_and_stream_id("123456", "sid"); + assert_eq!(handshake.data, Some(String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e"))); + } }