cross_authenticode/
hex_ex.rs

1/// Extension trait to convert byte slices and byte vectors to hex strings.
2pub trait ToHex {
3    /// Converts the byte slice or vector to a hex string in lowercase.
4    fn to_hex(&self) -> String;
5    /// Converts the byte slice or vector to a hex string in lowercase.
6    fn to_lower_hex(&self) -> String;
7    /// Converts the byte slice or vector to a hex string in uppercase.
8    fn to_upper_hex(&self) -> String;
9}
10
11impl ToHex for &[u8] {
12    fn to_hex(&self) -> String {
13        self.to_lower_hex()
14    }
15    fn to_lower_hex(&self) -> String {
16        use std::fmt::Write;
17        self.iter().fold(String::new(), |mut output, b| {
18            let _ = write!(output, "{b:02x}");
19            output
20        })
21    }
22    fn to_upper_hex(&self) -> String {
23        use std::fmt::Write;
24        self.iter().fold(String::new(), |mut output, b| {
25            let _ = write!(output, "{b:02X}");
26            output
27        })
28    }
29}
30
31impl ToHex for Vec<u8> {
32    fn to_hex(&self) -> String {
33        self.to_lower_hex()
34    }
35
36    fn to_lower_hex(&self) -> String {
37        self.as_slice().to_lower_hex()
38    }
39
40    fn to_upper_hex(&self) -> String {
41        self.as_slice().to_upper_hex()
42    }
43}