use alloc::{collections::BTreeMap, string::String};
use serde::Serialize;
use crate::sealed::Sealed;
pub trait Thumbprint: Sealed {
fn thumbprint_prehashed(&self) -> String;
fn thumbprint_sha256(&self) -> [u8; 32] {
let msg = self.thumbprint_prehashed();
crate::crypto::sha256(msg.as_bytes())
}
fn thumbprint_sha384(&self) -> [u8; 48] {
let msg = self.thumbprint_prehashed();
crate::crypto::sha384(msg.as_bytes())
}
fn thumbprint_sha512(&self) -> [u8; 64] {
let msg = self.thumbprint_prehashed();
crate::crypto::sha512(msg.as_bytes())
}
}
pub(crate) fn serialize_key_thumbprint<T: Serialize>(key: &T) -> String {
let obj = serde_json::to_value(key).expect("serialization of OctetSequence can not fail");
let map = match obj {
serde_json::Value::Object(map) => BTreeMap::from_iter(map),
_ => unreachable!("all keytypes must serialize to structs"),
};
serde_json::to_string(&map).expect("BTreeMap serialization can not fail")
}