Skip to main content

paysec_crypto_rustcrypto/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct RustCryptoError {
6    message: String,
7}
8
9impl RustCryptoError {
10    pub(crate) fn new(message: impl Into<String>) -> Self {
11        Self {
12            message: message.into(),
13        }
14    }
15
16    pub(crate) fn invalid_key_length(length: usize) -> Self {
17        Self::new(format!("unsupported AES key length: {length} bytes"))
18    }
19
20    pub(crate) fn random_generation_failed() -> Self {
21        Self::new("random byte generation failed")
22    }
23
24    pub(crate) fn rsa_oaep_encryption_failed() -> Self {
25        Self::new("RSA-OAEP encryption failed")
26    }
27
28    pub(crate) fn rsa_signing_failed() -> Self {
29        Self::new("RSA PKCS#1 v1.5 signing failed")
30    }
31
32    pub(crate) fn rsa_verification_failed() -> Self {
33        Self::new("RSA PKCS#1 v1.5 signature verification failed")
34    }
35}
36
37impl Display for RustCryptoError {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        f.write_str(&self.message)
40    }
41}
42
43impl Error for RustCryptoError {}