summaryrefslogtreecommitdiffstats
path: root/src/util.rs
blob: c6739b6543d94ce8abc82b910ed0941d0f82605b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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> {
//         
//     }
//     

use sha1_smol::{Digest, Sha1};

pub fn verify_sha1(expect: Digest, s: &str) -> Result<(), Digest> {
    let dig = Sha1::from(s).digest();

    if dig == expect {
        return Ok(());
    }

    Err(dig)
}

//     
//     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)
//     }
// }