AirLibrary/Updates/
ChecksumUtil.rs1#![allow(unused_variables, dead_code, unused_imports)]
2
3use sha2::{Digest, Sha256, Sha512};
11
12pub fn sha256_hex(data:&[u8]) -> String {
14 let mut h = Sha256::new();
15 h.update(data);
16 hex::encode(h.finalize())
17}
18
19pub fn sha512_hex(data:&[u8]) -> String {
21 let mut h = Sha512::new();
22 h.update(data);
23 hex::encode(h.finalize())
24}
25
26pub fn md5_hex(data:&[u8]) -> String {
28 let digest = md5::compute(data);
29 format!("{:x}", digest)
30}
31
32pub fn crc32_hex(data:&[u8]) -> String {
34 let crc = crc32fast::hash(data);
35 format!("{:08x}", crc)
36}
37
38pub async fn sha256_file(path:&std::path::Path) -> Result<String, std::io::Error> {
40 let content = tokio::fs::read(path).await?;
41 Ok(sha256_hex(&content))
42}
43
44pub fn verify(data:&[u8], algorithm:&str, expected:&str) -> bool {
48 let actual = match algorithm.to_lowercase().as_str() {
49 "sha256" => sha256_hex(data),
50 "sha512" => sha512_hex(data),
51 "md5" => md5_hex(data),
52 "crc32" => crc32_hex(data),
53 _ => return false,
54 };
55 actual == expected
56}