mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
hashes: Add an hex-encoded SHA-1 attribute helper.
This commit is contained in:
parent
7160397912
commit
82015de964
1 changed files with 43 additions and 0 deletions
|
@ -8,6 +8,8 @@ use crate::util::error::Error;
|
|||
use crate::util::helpers::Base64;
|
||||
use base64;
|
||||
use minidom::IntoAttributeValue;
|
||||
use std::num::ParseIntError;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::str::FromStr;
|
||||
|
||||
/// List of the algorithms we support, or Unknown.
|
||||
|
@ -121,6 +123,47 @@ impl Hash {
|
|||
}
|
||||
}
|
||||
|
||||
/// Helper for parsing and serialising a SHA-1 attribute.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Sha1HexAttribute(Hash);
|
||||
|
||||
impl FromStr for Sha1HexAttribute {
|
||||
type Err = ParseIntError;
|
||||
|
||||
fn from_str(hex: &str) -> Result<Self, Self::Err> {
|
||||
let mut bytes = vec![];
|
||||
for i in 0..hex.len() / 2 {
|
||||
let byte = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16);
|
||||
bytes.push(byte?);
|
||||
}
|
||||
Ok(Sha1HexAttribute(Hash::new(Algo::Sha_1, bytes)))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttributeValue for Sha1HexAttribute {
|
||||
fn into_attribute_value(self) -> Option<String> {
|
||||
let mut bytes = vec![];
|
||||
for byte in self.0.hash {
|
||||
bytes.push(format!("{:02x}", byte));
|
||||
}
|
||||
Some(bytes.join(""))
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Sha1HexAttribute {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Sha1HexAttribute {
|
||||
type Target = Hash;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in a new issue