kanidmd_core/https/
javascript.rs

1use std::path::PathBuf;
2
3/// Generates the integrity hash for a file based on a filename
4pub fn generate_integrity_hash(filename: String) -> Result<String, String> {
5    let filepath = PathBuf::from(filename);
6    match filepath.exists() {
7        false => Err(format!("Can't find {:?} to generate file hash", &filepath)),
8        true => {
9            let filecontents = match std::fs::read(&filepath) {
10                Ok(value) => value,
11                Err(error) => {
12                    return Err(format!("Failed to read {filepath:?}, skipping: {error:?}"));
13                }
14            };
15            let shasum = openssl::hash::hash(openssl::hash::MessageDigest::sha384(), &filecontents)
16                .map_err(|_| format!("Failed to generate SHA384 hash for file at {filepath:?}"))?;
17            Ok(openssl::base64::encode_block(&shasum))
18        }
19    }
20}
21
22#[derive(Clone)]
23pub struct JavaScriptFile {
24    // SHA384 hash of the file
25    pub hash: String,
26}