use crate::error::Result;
use crate::keys::{KeyPair, PublicKeyBundle};
use crate::multi::MultiRecipientEnvelope;
use crate::types::{Envelope, HybridSignature};
use alloc::vec::Vec;
use zeroize::Zeroizing;
#[derive(Debug)]
pub struct HybridCrypto {
keypair: KeyPair,
}
impl HybridCrypto {
pub fn generate() -> Result<Self> {
Ok(Self {
keypair: KeyPair::generate()?,
})
}
pub fn from_secret_bytes(bytes: &[u8]) -> Result<Self> {
Ok(Self {
keypair: KeyPair::from_secret_bytes(bytes)?,
})
}
pub fn to_secret_bytes(&self) -> Zeroizing<Vec<u8>> {
self.keypair.to_secret_bytes()
}
pub fn public_keys(&self) -> &PublicKeyBundle {
self.keypair.public_keys()
}
pub fn seal_for(&self, plaintext: &[u8], recipient: &PublicKeyBundle) -> Result<Envelope> {
crate::seal(plaintext, recipient)
}
pub fn open(&self, envelope: &Envelope) -> Result<Vec<u8>> {
crate::seal::open(&self.keypair, envelope)
}
pub fn open_multi(&self, envelope: &MultiRecipientEnvelope) -> Result<Vec<u8>> {
crate::multi::open_multi(&self.keypair, envelope)
}
pub fn stream_opener(&self, header: &[u8]) -> Result<crate::StreamOpener> {
crate::StreamOpener::new(&self.keypair, header)
}
pub fn attest_rotation(
&self,
new_public: &PublicKeyBundle,
epoch: u64,
) -> Result<crate::RotationAttestation> {
crate::rotate::attest_rotation(&self.keypair, new_public, epoch)
}
pub fn sign(&self, message: &[u8], context: &[u8]) -> Result<HybridSignature> {
crate::sign::sign(&self.keypair, message, context)
}
}
impl From<KeyPair> for HybridCrypto {
fn from(keypair: KeyPair) -> Self {
Self { keypair }
}
}