1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! Error types.

pub use base64ct::Error as B64Error;

use core::fmt;

#[cfg(docsrs)]
use crate::PasswordHasher;

/// Password hash errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum HashError {
    /// Hash output error.
    Hash(OutputError),

    /// Params error.
    Params(ParamsError),

    /// Parse error.
    Parse(ParseError),
}

impl fmt::Display for HashError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Hash(err) => write!(f, "invalid password hash: {}", err),
            Self::Params(err) => write!(f, "invalid params: {}", err),
            Self::Parse(err) => write!(f, "parse error: {}", err),
        }
    }
}

impl From<OutputError> for HashError {
    fn from(err: OutputError) -> HashError {
        HashError::Hash(err)
    }
}

impl From<ParamsError> for HashError {
    fn from(err: ParamsError) -> HashError {
        match err {
            ParamsError::Parse(e) => e.into(),
            _ => HashError::Params(err),
        }
    }
}

impl From<ParseError> for HashError {
    fn from(err: ParseError) -> HashError {
        HashError::Parse(err)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for HashError {}

/// Errors generating password hashes using a [`PasswordHasher`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum HasherError {
    /// Unsupported algorithm.
    Algorithm,

    /// "B64" encoding error.
    B64(B64Error),

    /// Cryptographic error.
    Crypto,

    /// Error generating output.
    Output(OutputError),

    /// Invalid parameter.
    Params(ParamsError),

    /// Parse error.
    Parse(ParseError),

    /// Invalid password.
    Password,

    /// Invalid algorithm version.
    Version,
}

impl fmt::Display for HasherError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Algorithm => write!(f, "unsupported algorithm"),
            Self::B64(err) => write!(f, "{}", err),
            Self::Crypto => write!(f, "cryptographic error"),
            Self::Output(err) => write!(f, "PHF output error: {}", err),
            Self::Params(err) => write!(f, "{}", err),
            Self::Parse(err) => write!(f, "{}", err),
            Self::Password => write!(f, "invalid password"),
            Self::Version => write!(f, "invalid algorithm version"),
        }
    }
}

impl From<B64Error> for HasherError {
    fn from(err: B64Error) -> HasherError {
        HasherError::B64(err)
    }
}

impl From<base64ct::InvalidLengthError> for HasherError {
    fn from(_: base64ct::InvalidLengthError) -> HasherError {
        HasherError::B64(B64Error::InvalidLength)
    }
}

impl From<OutputError> for HasherError {
    fn from(err: OutputError) -> HasherError {
        HasherError::Output(err)
    }
}

impl From<ParamsError> for HasherError {
    fn from(err: ParamsError) -> HasherError {
        HasherError::Params(err)
    }
}

impl From<ParseError> for HasherError {
    fn from(err: ParseError) -> HasherError {
        HasherError::Parse(err)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for HasherError {}

/// Parameter-related errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ParamsError {
    /// Duplicate parameter name encountered.
    DuplicateName,

    /// Invalid parameter name.
    InvalidName,

    /// Invalid parameter value.
    InvalidValue,

    /// Maximum number of parameters exceeded.
    MaxExceeded,

    /// Parse errors.
    Parse(ParseError),
}

impl fmt::Display for ParamsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::DuplicateName => f.write_str("duplicate parameter"),
            Self::InvalidName => f.write_str("invalid parameter name"),
            Self::InvalidValue => f.write_str("invalid parameter value"),
            Self::MaxExceeded => f.write_str("maximum number of parameters reached"),
            Self::Parse(err) => write!(f, "{}", err),
        }
    }
}

impl From<ParseError> for ParamsError {
    fn from(err: ParseError) -> ParamsError {
        Self::Parse(err)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParamsError {}

/// Parse errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// Invalid empty input.
    Empty,

    /// Input contains invalid character.
    InvalidChar(char),

    /// Input too short.
    TooShort,

    /// Input too long.
    TooLong,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Empty => f.write_str("invalid empty input"),
            Self::InvalidChar(char) => write!(f, "invalid character '{}'", char),
            Self::TooShort => f.write_str("too short"),
            Self::TooLong => f.write_str("too long"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseError {}

/// Password hash function output (i.e. hash/digest) errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum OutputError {
    /// "B64" encoding error.
    B64(B64Error),

    /// Output too short (min 10-bytes).
    TooShort,

    /// Output too long (max 64-bytes).
    TooLong,
}

impl fmt::Display for OutputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::B64(err) => write!(f, "{}", err),
            Self::TooShort => f.write_str("PHF output too short (min 10-bytes)"),
            Self::TooLong => f.write_str("PHF output too long (max 64-bytes)"),
        }
    }
}

impl From<B64Error> for OutputError {
    fn from(err: B64Error) -> OutputError {
        OutputError::B64(err)
    }
}

impl From<base64ct::InvalidLengthError> for OutputError {
    fn from(_: base64ct::InvalidLengthError) -> OutputError {
        OutputError::B64(B64Error::InvalidLength)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for OutputError {}

/// Password verification errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct VerifyError;

impl fmt::Display for VerifyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        f.write_str("password verification error")
    }
}

impl From<HasherError> for VerifyError {
    fn from(_: HasherError) -> VerifyError {
        VerifyError
    }
}

#[cfg(feature = "std")]
impl std::error::Error for VerifyError {}