1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::vec::Vec; |
4 | |
5 | use libc::size_t; |
6 | |
7 | use crate::{translate::*, Checksum}; |
8 | |
9 | impl Checksum { |
10 | #[doc (alias = "g_checksum_get_digest" )] |
11 | #[doc (alias = "get_digest" )] |
12 | pub fn digest(self) -> Vec<u8> { |
13 | unsafe { |
14 | //Don't forget update when `ChecksumType` contains type bigger that Sha512. |
15 | let mut digest_len: size_t = 512 / 8; |
16 | let mut vec = Vec::with_capacity(digest_len as _); |
17 | |
18 | ffi::g_checksum_get_digest( |
19 | mut_override(self.to_glib_none().0), |
20 | vec.as_mut_ptr(), |
21 | &mut digest_len, |
22 | ); |
23 | |
24 | vec.set_len(digest_len); |
25 | vec |
26 | } |
27 | } |
28 | |
29 | #[doc (alias = "g_checksum_get_string" )] |
30 | #[doc (alias = "get_string" )] |
31 | pub fn string(self) -> Option<String> { |
32 | unsafe { |
33 | from_glib_none(ffi::g_checksum_get_string(mut_override( |
34 | self.to_glib_none().0, |
35 | ))) |
36 | } |
37 | } |
38 | } |
39 | |
40 | #[cfg (test)] |
41 | mod tests { |
42 | use crate::{Checksum, ChecksumType}; |
43 | |
44 | const CS_TYPE: ChecksumType = ChecksumType::Md5; |
45 | const CS_VALUE: &str = "fc3ff98e8c6a0d3087d515c0473f8677" ; |
46 | const CS_SLICE: &[u8] = &[ |
47 | 0xfc, 0x3f, 0xf9, 0x8e, 0x8c, 0x6a, 0x0d, 0x30, 0x87, 0xd5, 0x15, 0xc0, 0x47, 0x3f, 0x86, |
48 | 0x77, |
49 | ]; |
50 | |
51 | #[test ] |
52 | fn update() { |
53 | let mut cs = Checksum::new(CS_TYPE).unwrap(); |
54 | cs.update(b"hello world!" ); |
55 | assert_eq!(cs.string().unwrap(), CS_VALUE); |
56 | } |
57 | |
58 | #[test ] |
59 | fn update_multi_call() { |
60 | let mut cs = Checksum::new(CS_TYPE).unwrap(); |
61 | cs.update(b"hello " ); |
62 | cs.update(b"world!" ); |
63 | assert_eq!(cs.string().unwrap(), CS_VALUE); |
64 | } |
65 | |
66 | #[test ] |
67 | #[doc (alias = "get_digest" )] |
68 | fn digest() { |
69 | let mut cs = Checksum::new(CS_TYPE).unwrap(); |
70 | cs.update(b"hello world!" ); |
71 | let vec = cs.digest(); |
72 | assert_eq!(vec, CS_SLICE); |
73 | } |
74 | } |
75 | |