use aws_lc_rs::digest::{digest, SHA256, SHA256_OUTPUT_LEN};
use janus_messages::{ReportId, ReportIdChecksum};
pub trait ReportIdChecksumExt {
fn for_report_id(report_id: &ReportId) -> Self;
fn updated_with(self, report_id: &ReportId) -> Self;
fn combined_with(self, other: &Self) -> Self;
}
impl ReportIdChecksumExt for ReportIdChecksum {
fn for_report_id(report_id: &ReportId) -> Self {
Self::from(report_id_digest(report_id))
}
fn updated_with(self, report_id: &ReportId) -> Self {
self.combined_with(&Self::for_report_id(report_id))
}
fn combined_with(mut self, other: &Self) -> Self {
self.as_mut()
.iter_mut()
.zip(other.as_ref())
.for_each(|(x, y)| *x ^= y);
self
}
}
fn report_id_digest(report_id: &ReportId) -> [u8; SHA256_OUTPUT_LEN] {
digest(&SHA256, report_id.as_ref())
.as_ref()
.try_into()
.unwrap()
}