glib/
checksum.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use libc::size_t;
4
5use crate::{ffi, translate::*, Checksum};
6
7impl Checksum {
8    /// Gets the digest from @self as a raw binary vector and places it
9    /// into @buffer. The size of the digest depends on the type of checksum.
10    ///
11    /// Once this function has been called, the #GChecksum is closed and can
12    /// no longer be updated with g_checksum_update().
13    /// ## `buffer`
14    /// output buffer
15    #[doc(alias = "g_checksum_get_digest")]
16    #[doc(alias = "get_digest")]
17    pub fn digest(self) -> Vec<u8> {
18        unsafe {
19            //Don't forget update when `ChecksumType` contains type bigger that Sha512.
20            let mut digest_len: size_t = 512 / 8;
21            let mut vec = Vec::with_capacity(digest_len as _);
22
23            ffi::g_checksum_get_digest(
24                mut_override(self.to_glib_none().0),
25                vec.as_mut_ptr(),
26                &mut digest_len,
27            );
28
29            vec.set_len(digest_len);
30            vec
31        }
32    }
33
34    /// Gets the digest as a hexadecimal string.
35    ///
36    /// Once this function has been called the #GChecksum can no longer be
37    /// updated with g_checksum_update().
38    ///
39    /// The hexadecimal characters will be lower case.
40    ///
41    /// # Returns
42    ///
43    /// the hexadecimal representation of the checksum. The
44    ///   returned string is owned by the checksum and should not be modified
45    ///   or freed.
46    #[doc(alias = "g_checksum_get_string")]
47    #[doc(alias = "get_string")]
48    pub fn string(self) -> Option<String> {
49        unsafe {
50            from_glib_none(ffi::g_checksum_get_string(mut_override(
51                self.to_glib_none().0,
52            )))
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::{Checksum, ChecksumType};
60
61    const CS_TYPE: ChecksumType = ChecksumType::Md5;
62    const CS_VALUE: &str = "fc3ff98e8c6a0d3087d515c0473f8677";
63    const CS_SLICE: &[u8] = &[
64        0xfc, 0x3f, 0xf9, 0x8e, 0x8c, 0x6a, 0x0d, 0x30, 0x87, 0xd5, 0x15, 0xc0, 0x47, 0x3f, 0x86,
65        0x77,
66    ];
67
68    #[test]
69    fn update() {
70        let mut cs = Checksum::new(CS_TYPE).unwrap();
71        cs.update(b"hello world!");
72        assert_eq!(cs.string().unwrap(), CS_VALUE);
73    }
74
75    #[test]
76    fn update_multi_call() {
77        let mut cs = Checksum::new(CS_TYPE).unwrap();
78        cs.update(b"hello ");
79        cs.update(b"world!");
80        assert_eq!(cs.string().unwrap(), CS_VALUE);
81    }
82
83    #[test]
84    #[doc(alias = "get_digest")]
85    fn digest() {
86        let mut cs = Checksum::new(CS_TYPE).unwrap();
87        cs.update(b"hello world!");
88        let vec = cs.digest();
89        assert_eq!(vec, CS_SLICE);
90    }
91}