bininfo/pe/
hash.rs

1use crypto::{digest::Digest, md5::Md5, sha1::Sha1, sha2::Sha256};
2use serde::{Deserialize, Serialize};
3
4pub trait HashData {
5    /// Produce an MD5 hash.
6    fn md5(&self) -> Vec<u8>;
7    /// Produce a SHA1 hash.
8    fn sha1(&self) -> Vec<u8>;
9    /// Produce a SHA256 hash.
10    fn sha256(&self) -> Vec<u8>;
11}
12impl HashData for [u8] {
13    fn md5(&self) -> Vec<u8> {
14        let mut hash = Md5::new();
15        hash.input(self); // Update the hash input with the byte slice
16        hash.result_str().as_bytes().to_vec() // Convert to Vec<u8>
17    }
18
19    fn sha1(&self) -> Vec<u8> {
20        let mut hash = Sha1::new();
21        hash.input(self); // Update the hash input with the byte slice
22        hash.result_str().as_bytes().to_vec() // Convert to Vec<u8>
23    }
24
25    fn sha256(&self) -> Vec<u8> {
26        let mut hash = Sha256::new();
27        hash.input(self); // Update the hash input with the byte slice
28        hash.result_str().as_bytes().to_vec() // Convert to Vec<u8>
29    }
30}
31
32#[derive(Clone, Debug, Default, Serialize, Deserialize)]
33pub struct Hash {
34    pub name: String,
35    pub value: Vec<u8>,
36}
37
38#[derive(Clone, Debug, Default, Serialize, Deserialize)]
39pub struct Hashes {
40    pub hashes: Vec<Hash>,
41}
42
43impl Hashes {
44    pub fn parse(pe: &[u8]) -> Hashes {
45        Hashes {
46            hashes: vec![
47                Hash {
48                    name: "MD5".to_string(),
49                    value: pe.md5(),
50                },
51                Hash {
52                    name: "SHA1".to_string(),
53                    value: pe.sha1(),
54                },
55                Hash {
56                    name: "SHA256".to_string(),
57                    value: pe.sha256(),
58                },
59            ],
60        }
61    }
62}