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
pub use base64ct::Error as B64Error;
use core::cmp::Ordering;
use core::fmt;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
Algorithm,
B64Encoding(B64Error),
Crypto,
OutputSize {
provided: Ordering,
expected: usize,
},
ParamNameDuplicated,
ParamNameInvalid,
ParamValueInvalid(InvalidValue),
ParamsMaxExceeded,
Password,
PhcStringField,
PhcStringTrailingData,
SaltInvalid(InvalidValue),
Version,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
match self {
Self::Algorithm => write!(f, "unsupported algorithm"),
Self::B64Encoding(err) => write!(f, "{}", err),
Self::Crypto => write!(f, "cryptographic error"),
Self::OutputSize { provided, expected } => match provided {
Ordering::Less => write!(
f,
"output size too short, expected at least {} bytes",
expected
),
Ordering::Equal => write!(f, "output size unexpected, expected {} bytes", expected),
Ordering::Greater => write!(
f,
"output size too long, expected at most {} bytes",
expected
),
},
Self::ParamNameDuplicated => f.write_str("duplicate parameter"),
Self::ParamNameInvalid => f.write_str("invalid parameter name"),
Self::ParamValueInvalid(val_err) => write!(f, "invalid parameter value: {}", val_err),
Self::ParamsMaxExceeded => f.write_str("maximum number of parameters reached"),
Self::Password => write!(f, "invalid password"),
Self::PhcStringField => write!(f, "password hash string missing field"),
Self::PhcStringTrailingData => {
write!(f, "password hash string contains trailing characters")
}
Self::SaltInvalid(val_err) => write!(f, "salt invalid: {}", val_err),
Self::Version => write!(f, "invalid algorithm version"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::B64Encoding(err) => Some(err),
Self::ParamValueInvalid(err) => Some(err),
Self::SaltInvalid(err) => Some(err),
_ => None,
}
}
}
impl From<B64Error> for Error {
fn from(err: B64Error) -> Error {
Error::B64Encoding(err)
}
}
impl From<base64ct::InvalidLengthError> for Error {
fn from(_: base64ct::InvalidLengthError) -> Error {
Error::B64Encoding(B64Error::InvalidLength)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum InvalidValue {
InvalidChar(char),
InvalidFormat,
Malformed,
TooLong,
TooShort,
}
impl InvalidValue {
pub fn param_error(self) -> Error {
Error::ParamValueInvalid(self)
}
pub fn salt_error(self) -> Error {
Error::SaltInvalid(self)
}
}
impl fmt::Display for InvalidValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
match self {
Self::InvalidChar(c) => write!(f, "contains invalid character: '{}'", c),
Self::InvalidFormat => f.write_str("value format is invalid"),
Self::Malformed => f.write_str("value malformed"),
Self::TooLong => f.write_str("value to long"),
Self::TooShort => f.write_str("value to short"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidValue {}