aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/self_update/keys.rs
blob: 3fa29f7d75355e3a0be2c5eec1c2ce5109d88103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//! ref: composer/src/Composer/SelfUpdate/Keys.php

use regex::Regex;
use shirabe_php_shim::hash;

pub struct Keys;

impl Keys {
    pub fn fingerprint(path: &str) -> anyhow::Result<String> {
        let content = std::fs::read_to_string(path)?;
        let re = Regex::new(r"\s").unwrap();
        let cleaned = re.replace_all(&content, "");
        let hash = hash("sha256", &cleaned).to_uppercase();

        Ok([
            &hash[0..8],
            &hash[8..16],
            &hash[16..24],
            &hash[24..32],
            "",
            &hash[32..40],
            &hash[40..48],
            &hash[48..56],
            &hash[56..64],
        ]
        .join(" "))
    }
}