aboutsummaryrefslogtreecommitdiffstats
path: root/stanza
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2025-04-03 05:30:35 +0100
committerLibravatar cel 🌸 <cel@bunny.garden>2025-04-03 05:30:35 +0100
commit6664a9df01b8b6f89e72f9568f90ad7397ceae36 (patch)
treec221139702270cf9f413d579812989e37f346fc6 /stanza
parent91f1994af940085d5d475a97820900ebbf0eb553 (diff)
downloadluz-6664a9df01b8b6f89e72f9568f90ad7397ceae36.tar.gz
luz-6664a9df01b8b6f89e72f9568f90ad7397ceae36.tar.bz2
luz-6664a9df01b8b6f89e72f9568f90ad7397ceae36.zip
feat(stanza): xep-0300: use of cryptographic hash functions
Diffstat (limited to 'stanza')
-rw-r--r--stanza/Cargo.toml1
-rw-r--r--stanza/src/lib.rs2
-rw-r--r--stanza/src/xep_0300.rs129
3 files changed, 132 insertions, 0 deletions
diff --git a/stanza/Cargo.toml b/stanza/Cargo.toml
index 3c3c3e0..c492f1b 100644
--- a/stanza/Cargo.toml
+++ b/stanza/Cargo.toml
@@ -19,3 +19,4 @@ xep_0131 = []
xep_0172 = []
xep_0199 = []
xep_0203 = ["dep:chrono"]
+xep_0300 = []
diff --git a/stanza/src/lib.rs b/stanza/src/lib.rs
index 5474aee..bc8d59a 100644
--- a/stanza/src/lib.rs
+++ b/stanza/src/lib.rs
@@ -25,5 +25,7 @@ pub mod xep_0172;
pub mod xep_0199;
#[cfg(feature = "xep_0203")]
pub mod xep_0203;
+#[cfg(feature = "xep_0300")]
+pub mod xep_0300;
pub static XML_VERSION: VersionInfo = VersionInfo::One;
diff --git a/stanza/src/xep_0300.rs b/stanza/src/xep_0300.rs
new file mode 100644
index 0000000..9efad46
--- /dev/null
+++ b/stanza/src/xep_0300.rs
@@ -0,0 +1,129 @@
+use std::str::FromStr;
+
+use peanuts::{
+ element::{FromElement, IntoElement},
+ DeserializeError, Element,
+};
+
+pub const XMLNS: &str = "urn:xmpp:hashes:2";
+
+#[derive(Debug, Clone)]
+pub struct Hash {
+ algo: Algo,
+ hash: String,
+}
+
+impl FromElement for Hash {
+ fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
+ element.check_name("hash")?;
+ element.check_namespace(XMLNS)?;
+
+ let algo = element.attribute("algo")?;
+
+ let hash = element.pop_value_opt()?.unwrap_or_default();
+
+ Ok(Self { algo, hash })
+ }
+}
+
+impl IntoElement for Hash {
+ fn builder(&self) -> peanuts::element::ElementBuilder {
+ let builder =
+ Element::builder("hash", Some(XMLNS)).push_attribute("algo", self.algo.clone());
+
+ // TODO: AAAAAAAAAAAAAAAAAAAAAA
+ if self.hash.is_empty() {
+ builder
+ } else {
+ builder.push_text(self.hash.clone())
+ }
+ }
+}
+
+#[derive(Debug, Clone)]
+pub struct HashUsed {
+ algo: Algo,
+}
+
+impl FromElement for HashUsed {
+ fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
+ element.check_name("hash-used")?;
+ element.check_namespace(XMLNS)?;
+
+ let algo = element.attribute("algo")?;
+
+ Ok(Self { algo })
+ }
+}
+
+impl IntoElement for HashUsed {
+ fn builder(&self) -> peanuts::element::ElementBuilder {
+ Element::builder("hash-used", Some(XMLNS)).push_attribute("algo", self.algo.clone())
+ }
+}
+
+#[derive(Debug, Clone)]
+pub enum Algo {
+ MD5,
+ SHA1,
+ SHA224,
+ SHA256,
+ SHA384,
+ SHA512,
+ SHA3224,
+ SHA3256,
+ SHA3384,
+ SHA3512,
+ BLAKE2b160,
+ BLAKE2b256,
+ BLAKE2b384,
+ BLAKE2b512,
+ Other(String),
+}
+
+impl FromStr for Algo {
+ type Err = DeserializeError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "md5" => Ok(Self::MD5),
+ "sha-1" => Ok(Self::SHA1),
+ "sha-224" => Ok(Self::SHA224),
+ "sha-256" => Ok(Self::SHA256),
+ "sha-384" => Ok(Self::SHA384),
+ "sha-512" => Ok(Self::SHA512),
+ "sha3-224" => Ok(Self::SHA3224),
+ "sha3-256" => Ok(Self::SHA3256),
+ "sha3-384" => Ok(Self::SHA3384),
+ "sha3-512" => Ok(Self::SHA3512),
+ "blake2b-160" => Ok(Self::BLAKE2b160),
+ "blake2b-256" => Ok(Self::BLAKE2b256),
+ "blake2b-384" => Ok(Self::BLAKE2b384),
+ "blake2b-512" => Ok(Self::BLAKE2b512),
+ s => Ok(Self::Other(s.to_owned())),
+ }
+ }
+}
+
+impl ToString for Algo {
+ fn to_string(&self) -> String {
+ match self {
+ Algo::MD5 => "md5",
+ Algo::SHA1 => "sha-1",
+ Algo::SHA224 => "sha-224",
+ Algo::SHA256 => "sha-256",
+ Algo::SHA384 => "sha-384",
+ Algo::SHA512 => "sha-512",
+ Algo::SHA3224 => "sha3-224",
+ Algo::SHA3256 => "sha3-256",
+ Algo::SHA3384 => "sha3-384",
+ Algo::SHA3512 => "sha3-512",
+ Algo::BLAKE2b160 => "blake2b-160",
+ Algo::BLAKE2b256 => "blake2b-256",
+ Algo::BLAKE2b384 => "blake2b-384",
+ Algo::BLAKE2b512 => "blake2b-512",
+ Algo::Other(s) => s,
+ }
+ .to_owned()
+ }
+}