Coverage Report

Created: 2025-02-21 07:11

/rust/registry/src/index.crates.io-6f17d22bba15001f/argon2-0.5.3/src/error.rs
Line
Count
Source (jump to first uncovered line)
1
//! Error type
2
3
use core::fmt;
4
5
#[cfg(feature = "password-hash")]
6
use {crate::Params, core::cmp::Ordering, password_hash::errors::InvalidValue};
7
8
/// Result with argon2's [`Error`] type.
9
pub type Result<T> = core::result::Result<T, Error>;
10
11
/// Error type.
12
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13
pub enum Error {
14
    /// Associated data is too long.
15
    AdTooLong,
16
17
    /// Algorithm identifier invalid.
18
    AlgorithmInvalid,
19
20
    /// "B64" encoding is invalid.
21
    B64Encoding(base64ct::Error),
22
23
    /// Key ID is too long.
24
    KeyIdTooLong,
25
26
    /// Memory cost is too small.
27
    MemoryTooLittle,
28
29
    /// Memory cost is too large.
30
    MemoryTooMuch,
31
32
    /// Output is too short.
33
    OutputTooShort,
34
35
    /// Output is too long.
36
    OutputTooLong,
37
38
    /// Password is too long.
39
    PwdTooLong,
40
41
    /// Salt is too short.
42
    SaltTooShort,
43
44
    /// Salt is too long.
45
    SaltTooLong,
46
47
    /// Secret is too long.
48
    SecretTooLong,
49
50
    /// Not enough threads.
51
    ThreadsTooFew,
52
53
    /// Too many threads.
54
    ThreadsTooMany,
55
56
    /// Time cost is too small.
57
    TimeTooSmall,
58
59
    /// Invalid version
60
    VersionInvalid,
61
}
62
63
impl fmt::Display for Error {
64
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65
0
        f.write_str(match self {
66
0
            Error::AdTooLong => "associated data is too long",
67
0
            Error::AlgorithmInvalid => "algorithm identifier invalid",
68
0
            Error::B64Encoding(inner) => return write!(f, "B64 encoding invalid: {inner}"),
69
0
            Error::KeyIdTooLong => "key ID is too long",
70
0
            Error::MemoryTooLittle => "memory cost is too small",
71
0
            Error::MemoryTooMuch => "memory cost is too large",
72
0
            Error::OutputTooShort => "output is too short",
73
0
            Error::OutputTooLong => "output is too long",
74
0
            Error::PwdTooLong => "password is too long",
75
0
            Error::SaltTooShort => "salt is too short",
76
0
            Error::SaltTooLong => "salt is too long",
77
0
            Error::SecretTooLong => "secret is too long",
78
0
            Error::ThreadsTooFew => "not enough threads",
79
0
            Error::ThreadsTooMany => "too many threads",
80
0
            Error::TimeTooSmall => "time cost is too small",
81
0
            Error::VersionInvalid => "invalid version",
82
        })
83
0
    }
84
}
85
86
impl From<base64ct::Error> for Error {
87
0
    fn from(err: base64ct::Error) -> Error {
88
0
        Error::B64Encoding(err)
89
0
    }
90
}
91
92
#[cfg(feature = "password-hash")]
93
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
94
impl From<Error> for password_hash::Error {
95
0
    fn from(err: Error) -> password_hash::Error {
96
0
        match err {
97
0
            Error::AdTooLong => InvalidValue::TooLong.param_error(),
98
0
            Error::AlgorithmInvalid => password_hash::Error::Algorithm,
99
0
            Error::B64Encoding(inner) => password_hash::Error::B64Encoding(inner),
100
0
            Error::KeyIdTooLong => InvalidValue::TooLong.param_error(),
101
0
            Error::MemoryTooLittle => InvalidValue::TooShort.param_error(),
102
0
            Error::MemoryTooMuch => InvalidValue::TooLong.param_error(),
103
0
            Error::PwdTooLong => password_hash::Error::Password,
104
0
            Error::OutputTooShort => password_hash::Error::OutputSize {
105
0
                provided: Ordering::Less,
106
0
                expected: Params::MIN_OUTPUT_LEN,
107
0
            },
108
0
            Error::OutputTooLong => password_hash::Error::OutputSize {
109
0
                provided: Ordering::Greater,
110
0
                expected: Params::MAX_OUTPUT_LEN,
111
0
            },
112
0
            Error::SaltTooShort => InvalidValue::TooShort.salt_error(),
113
0
            Error::SaltTooLong => InvalidValue::TooLong.salt_error(),
114
0
            Error::SecretTooLong => InvalidValue::TooLong.param_error(),
115
0
            Error::ThreadsTooFew => InvalidValue::TooShort.param_error(),
116
0
            Error::ThreadsTooMany => InvalidValue::TooLong.param_error(),
117
0
            Error::TimeTooSmall => InvalidValue::TooShort.param_error(),
118
0
            Error::VersionInvalid => password_hash::Error::Version,
119
        }
120
0
    }
121
}
122
123
#[cfg(feature = "std")]
124
impl std::error::Error for Error {
125
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126
        match self {
127
            Self::B64Encoding(err) => Some(err),
128
            _ => None,
129
        }
130
    }
131
}