summaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs131
1 files changed, 70 insertions, 61 deletions
diff --git a/src/util.rs b/src/util.rs
index 428990c..c6739b6 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,68 +1,77 @@
-use std::fmt::{Debug, Display, Formatter, Pointer};
-use std::ops::Deref;
-use crypto::digest::Digest;
-use crypto::sha1::Sha1;
-use serde::{Deserialize, Deserializer};
-use serde::de::{Error, Visitor};
-use hex::{FromHex, ToHex};
+// use std::fmt::{Debug, Display, Formatter};
+// use serde::{Deserialize, Deserializer};
+// use serde::de::{Error, Visitor};
+// use hex::{FromHex, FromHexError, ToHex};
+// use sha1_smol::Sha1;
+//
+// // sha1 digests are 20 bytes long
+// pub use sha1_smol::DIGEST_LENGTH;
+// pub type Sha1DigestBytes = [u8; DIGEST_LENGTH];
+//
+// #[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
+// pub struct Sha1Digest(pub Sha1DigestBytes);
+//
+// impl Debug for Sha1Digest {
+// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+// write!(f, "Sha1Digest {{{}}}", self.0.encode_hex::<String>())
+// }
+// }
+//
+// impl Display for Sha1Digest {
+// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+// f.write_str(&self.0.encode_hex::<String>())
+// }
+// }
+//
+// impl Sha1Digest {
+// pub fn from_hex(s: &str) -> Result<Sha1Digest, FromHexError> {
+//
+// }
+//
-// sha1 digests are 20 bytes long
-pub const SHA1_DIGEST_BYTES: usize = 20;
-pub type Sha1DigestBytes = [u8; SHA1_DIGEST_BYTES];
+use sha1_smol::{Digest, Sha1};
-#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
-pub struct Sha1Digest(pub Sha1DigestBytes);
+pub fn verify_sha1(expect: Digest, s: &str) -> Result<(), Digest> {
+ let dig = Sha1::from(s).digest();
-impl Debug for Sha1Digest {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- write!(f, "Sha1Digest {{{}}}", self.0.encode_hex::<String>())
+ if dig == expect {
+ return Ok(());
}
-}
-
-impl Display for Sha1Digest {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- f.write_str(&self.0.encode_hex::<String>())
- }
-}
-
-impl Sha1Digest {
- pub fn verify(&self, s: &str) -> Result<(), Sha1Digest> {
- let mut st = Sha1::new();
- let mut dig = [0u8; SHA1_DIGEST_BYTES];
-
- st.input_str(s);
- st.result(&mut dig);
-
- if self.0 == dig {
- return Ok(());
- }
-
- Err(Sha1Digest(dig))
- }
-}
-
-struct Sha1DigestVisitor;
-
-impl <'a> Visitor<'a> for Sha1DigestVisitor {
- type Value = Sha1Digest;
- fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
- write!(formatter, "a valid SHA-1 digest (40-character hex string)")
- }
-
- fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
- where
- E: Error,
- {
- Sha1DigestBytes::from_hex(v).map_err(|e| E::custom(e)).map(Sha1Digest)
- }
+ Err(dig)
}
-impl<'a> Deserialize<'a> for Sha1Digest {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'a>,
- {
- deserializer.deserialize_any(Sha1DigestVisitor)
- }
-} \ No newline at end of file
+//
+// pub fn as_hex(&self) -> String {
+// // allocate the string with capacity first so we only do one heap alloc
+// let mut s: String = String::with_capacity(2 * self.0.len());
+// self.0.iter().for_each(|b| s.push_str(&format!("{:02x}", b)));
+// s
+// }
+// }
+//
+// struct Sha1DigestVisitor;
+//
+// impl <'a> Visitor<'a> for Sha1DigestVisitor {
+// type Value = Sha1Digest;
+//
+// fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
+// write!(formatter, "a valid SHA-1 digest (40-character hex string)")
+// }
+//
+// fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+// where
+// E: Error,
+// {
+// Sha1DigestBytes::from_hex(v).map_err(|e| E::custom(e)).map(Sha1Digest)
+// }
+// }
+//
+// impl<'a> Deserialize<'a> for Sha1Digest {
+// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+// where
+// D: Deserializer<'a>,
+// {
+// deserializer.deserialize_any(Sha1DigestVisitor)
+// }
+// } \ No newline at end of file