use std::fmt::Write;
use crate::
{
consts,
crypto,
};
pub enum TofuCode {
Valid, Unknown(String, String), Mismatch, }
pub fn hash(pubkey: &[u8]) -> String {
let pubkey_hash = crypto::sha256(pubkey);
let mut pubkey_string = String::with_capacity(64);
for byte in pubkey_hash
{
write!(pubkey_string, "{:02x}", byte).unwrap();
}
pubkey_string
}
pub fn check(host: &str, pubkey: &[u8]) -> TofuCode {
let pubkey_string = hash(pubkey);
if super::get_data(&super::config_path(consts::SERVER_KEYS_CONFIG)).get(host).is_some()
{
return if super::config_read::<String>(consts::SERVER_KEYS_CONFIG, host) == pubkey_string
{
TofuCode::Valid
} else
{
TofuCode::Mismatch
}
}
TofuCode::Unknown(pubkey_string, host.to_string())
}
pub fn pinned(host: &str) -> Option<String> {
if super::get_data(&super::config_path(consts::SERVER_KEYS_CONFIG)).get(host).is_none() { return None; }
Some(super::config_read::<String>(consts::SERVER_KEYS_CONFIG, host))
}
pub fn save(host: &str, pubkey_hash: &str) {
super::config_write(consts::SERVER_KEYS_CONFIG, host, pubkey_hash);
}