Skip to main content

paysec_crypto_rustcrypto/
provider.rs

1use paysec_crypto::CryptoProvider;
2
3use crate::RustCryptoError;
4
5#[derive(Debug, Default, Clone, Copy)]
6pub struct RustCryptoProvider;
7
8impl RustCryptoProvider {
9    pub const fn new() -> Self {
10        Self
11    }
12
13    pub const fn with_rng<R>(rng: R) -> RustCryptoProviderWithRng<R> {
14        RustCryptoProviderWithRng::new(rng)
15    }
16}
17
18impl CryptoProvider for RustCryptoProvider {
19    type Error = RustCryptoError;
20}
21
22/// RustCrypto provider using a caller-supplied cryptographically secure
23/// random number generator.
24///
25/// This variant is useful when randomness must be controlled externally,
26/// including deterministic conformance and test-vector generation.
27#[derive(Debug)]
28pub struct RustCryptoProviderWithRng<R> {
29    rng: R,
30}
31
32impl<R> RustCryptoProviderWithRng<R> {
33    pub const fn new(rng: R) -> Self {
34        Self { rng }
35    }
36
37    pub fn rng_mut(&mut self) -> &mut R {
38        &mut self.rng
39    }
40
41    pub fn into_rng(self) -> R {
42        self.rng
43    }
44}
45
46impl<R> CryptoProvider for RustCryptoProviderWithRng<R> {
47    type Error = RustCryptoError;
48}