use crate::error::Error;
#[cfg(feature = "digest")]
use crate::digest::Digest;
#[cfg(feature = "rand_core")]
use crate::rand_core::CryptoRngCore;
pub trait Signer<S> {
fn sign(&self, msg: &[u8]) -> S {
self.try_sign(msg).expect("signature operation failed")
}
fn try_sign(&self, msg: &[u8]) -> Result<S, Error>;
}
pub trait SignerMut<S> {
fn sign(&mut self, msg: &[u8]) -> S {
self.try_sign(msg).expect("signature operation failed")
}
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>;
}
impl<S, T: Signer<S>> SignerMut<S> for T {
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error> {
T::try_sign(self, msg)
}
}
#[cfg(feature = "digest")]
pub trait DigestSigner<D: Digest, S> {
fn sign_digest(&self, digest: D) -> S {
self.try_sign_digest(digest)
.expect("signature operation failed")
}
fn try_sign_digest(&self, digest: D) -> Result<S, Error>;
}
#[cfg(feature = "rand_core")]
pub trait RandomizedSigner<S> {
fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
fn try_sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<S, Error>;
}
#[cfg(all(feature = "digest", feature = "rand_core"))]
pub trait RandomizedDigestSigner<D: Digest, S> {
fn sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D) -> S {
self.try_sign_digest_with_rng(rng, digest)
.expect("signature operation failed")
}
fn try_sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D)
-> Result<S, Error>;
}
#[cfg(feature = "rand_core")]
pub trait RandomizedSignerMut<S> {
fn sign_with_rng(&mut self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
fn try_sign_with_rng(&mut self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<S, Error>;
}
#[cfg(feature = "rand_core")]
impl<S, T: RandomizedSigner<S>> RandomizedSignerMut<S> for T {
fn try_sign_with_rng(&mut self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<S, Error> {
T::try_sign_with_rng(self, rng, msg)
}
}