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