use std::collections::HashMap;
use crate::{
crypto::{aead::AeadDecrypt, key_derivation::KeyDerivation},
header::KeyId,
};
use super::crypto_key::DecryptionKey;
pub trait KeyStore<A, D>
where
A: AeadDecrypt<Secret = D::Secret>,
D: KeyDerivation,
{
fn get_key<K>(&self, key_id: K) -> Option<&DecryptionKey<A, D>>
where
K: Into<KeyId>;
}
impl<A, D> KeyStore<A, D> for DecryptionKey<A, D>
where
A: AeadDecrypt<Secret = D::Secret>,
D: KeyDerivation,
{
fn get_key<K>(&self, key_id: K) -> Option<&DecryptionKey<A, D>>
where
K: Into<KeyId>,
{
let key_id = key_id.into();
if self.key_id() == key_id {
Some(self)
} else {
None
}
}
}
impl<A, D> KeyStore<A, D> for HashMap<KeyId, DecryptionKey<A, D>>
where
A: AeadDecrypt<Secret = D::Secret>,
D: KeyDerivation,
{
fn get_key<K>(&self, key_id: K) -> Option<&DecryptionKey<A, D>>
where
K: Into<KeyId>,
{
let key_id = key_id.into();
self.get(&key_id)
}
}