glib/auto/
checksum.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*, ChecksumType};
6
7crate::wrapper! {
8    /// GLib provides a generic API for computing checksums (or ‘digests’)
9    /// for a sequence of arbitrary bytes, using various hashing algorithms
10    /// like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
11    /// environments and specifications.
12    ///
13    /// To create a new `GChecksum`, use `GLib::Checksum::new()`. To free
14    /// a `GChecksum`, use `GLib::Checksum::free()`.
15    ///
16    /// GLib supports incremental checksums using the `GChecksum` data
17    /// structure, by calling [`update()`][Self::update()] as long as there’s data
18    /// available and then using [`string()`][Self::string()] or
19    /// [`digest()`][Self::digest()] to compute the checksum and return it
20    /// either as a string in hexadecimal form, or as a raw sequence of bytes. To
21    /// compute the checksum for binary blobs and nul-terminated strings in
22    /// one go, use the convenience functions [`compute_checksum_for_data()`][crate::compute_checksum_for_data()]
23    /// and `compute_checksum_for_string()`, respectively.
24    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
25    pub struct Checksum(Boxed<ffi::GChecksum>);
26
27    match fn {
28        copy => |ptr| ffi::g_checksum_copy(ptr),
29        free => |ptr| ffi::g_checksum_free(ptr),
30        type_ => || ffi::g_checksum_get_type(),
31    }
32}
33
34impl Checksum {
35    /// Creates a new #GChecksum, using the checksum algorithm @checksum_type.
36    /// If the @checksum_type is not known, [`None`] is returned.
37    /// A #GChecksum can be used to compute the checksum, or digest, of an
38    /// arbitrary binary blob, using different hashing algorithms.
39    ///
40    /// A #GChecksum works by feeding a binary blob through g_checksum_update()
41    /// until there is data to be checked; the digest can then be extracted
42    /// using g_checksum_get_string(), which will return the checksum as a
43    /// hexadecimal string; or g_checksum_get_digest(), which will return a
44    /// vector of raw bytes. Once either g_checksum_get_string() or
45    /// g_checksum_get_digest() have been called on a #GChecksum, the checksum
46    /// will be closed and it won't be possible to call g_checksum_update()
47    /// on it anymore.
48    /// ## `checksum_type`
49    /// the desired type of checksum
50    ///
51    /// # Returns
52    ///
53    /// the newly created #GChecksum, or [`None`].
54    ///   Use g_checksum_free() to free the memory allocated by it.
55    #[doc(alias = "g_checksum_new")]
56    pub fn new(checksum_type: ChecksumType) -> Option<Checksum> {
57        unsafe { from_glib_full(ffi::g_checksum_new(checksum_type.into_glib())) }
58    }
59
60    /// Resets the state of the @self back to its initial state.
61    #[doc(alias = "g_checksum_reset")]
62    pub fn reset(&mut self) {
63        unsafe {
64            ffi::g_checksum_reset(self.to_glib_none_mut().0);
65        }
66    }
67
68    /// Feeds @data into an existing #GChecksum. The checksum must still be
69    /// open, that is g_checksum_get_string() or g_checksum_get_digest() must
70    /// not have been called on @self.
71    /// ## `data`
72    /// buffer used to compute the checksum
73    #[doc(alias = "g_checksum_update")]
74    pub fn update(&mut self, data: &[u8]) {
75        let length = data.len() as _;
76        unsafe {
77            ffi::g_checksum_update(self.to_glib_none_mut().0, data.to_glib_none().0, length);
78        }
79    }
80
81    /// Gets the length in bytes of digests of type @checksum_type
82    /// ## `checksum_type`
83    /// a #GChecksumType
84    ///
85    /// # Returns
86    ///
87    /// the checksum length, or -1 if @checksum_type is
88    /// not supported.
89    #[doc(alias = "g_checksum_type_get_length")]
90    pub fn type_get_length(checksum_type: ChecksumType) -> isize {
91        unsafe { ffi::g_checksum_type_get_length(checksum_type.into_glib()) }
92    }
93}
94
95unsafe impl Send for Checksum {}
96unsafe impl Sync for Checksum {}