@@ -86,12 +86,23 @@ pub trait PasswordHasher<H> {
8686 ///
8787 /// The salt should be unique per password. When in doubt, use [`PasswordHasher::hash_password`]
8888 /// which will choose the salt for you.
89+ ///
90+ /// # Errors
91+ /// These will vary by algorithm/implementation of this trait, but may be due to:
92+ /// - length restrictions on the password and/or salt
93+ /// - algorithm-specific internal error
8994 fn hash_password_with_salt ( & self , password : & [ u8 ] , salt : & [ u8 ] ) -> Result < H > ;
9095
9196 /// Compute the hash `H` from the given password, potentially using configuration stored in
9297 /// `&self` for the parameters, or otherwise the recommended defaults.
9398 ///
9499 /// A large random salt will be generated automatically.
100+ ///
101+ /// # Errors
102+ /// These will vary by algorithm/implementation of this trait, but may be due to:
103+ /// - length restrictions on the password
104+ /// - algorithm-specific internal error
105+ /// - RNG internal error
95106 #[ cfg( feature = "getrandom" ) ]
96107 fn hash_password ( & self , password : & [ u8 ] ) -> Result < H > {
97108 let salt = try_generate_salt ( ) ?;
@@ -102,6 +113,12 @@ pub trait PasswordHasher<H> {
102113 /// `&self` for the parameters, or otherwise the recommended defaults.
103114 ///
104115 /// A large random salt will be generated automatically from the provided RNG.
116+ ///
117+ /// # Errors
118+ /// These will vary by algorithm/implementation of this trait, but may be due to:
119+ /// - length restrictions on the password
120+ /// - algorithm-specific internal error
121+ /// - RNG internal error
105122 #[ cfg( feature = "rand_core" ) ]
106123 fn hash_password_with_rng < R : TryCryptoRng + ?Sized > (
107124 & self ,
@@ -121,11 +138,15 @@ pub trait CustomizedPasswordHasher<H> {
121138 /// Algorithm-specific parameters.
122139 type Params : Clone + Debug + Default ;
123140
124- /// Compute a [`PasswordHash`] from the provided password using an
125- /// explicit set of customized algorithm parameters as opposed to the
126- /// defaults.
141+ /// Compute a [`PasswordHash`] from the provided password using an explicit set of customized
142+ /// algorithm parameters as opposed to the defaults.
127143 ///
128144 /// When in doubt, use [`PasswordHasher::hash_password`] instead.
145+ ///
146+ /// # Errors
147+ /// These will vary by algorithm/implementation of this trait, but may be due to:
148+ /// - length restrictions on the password and/or salt
149+ /// - algorithm-specific params or internal error
129150 fn hash_password_customized (
130151 & self ,
131152 password : & [ u8 ] ,
@@ -137,6 +158,11 @@ pub trait CustomizedPasswordHasher<H> {
137158
138159 /// Compute a [`PasswordHash`] using customized parameters only, using the default
139160 /// algorithm and version.
161+ ///
162+ /// # Errors
163+ /// These will vary by algorithm/implementation of this trait, but may be due to:
164+ /// - length restrictions on the password and/or salt
165+ /// - algorithm-specific params or internal error
140166 fn hash_password_with_params (
141167 & self ,
142168 password : & [ u8 ] ,
@@ -156,9 +182,14 @@ pub trait CustomizedPasswordHasher<H> {
156182/// This trait is object safe and can be used to implement abstractions over
157183/// multiple password hashing algorithms.
158184pub trait PasswordVerifier < H : ?Sized > {
159- /// Compute this password hashing function against the provided password
160- /// using the parameters from the provided password hash and see if the
161- /// computed output matches.
185+ /// Compute this password hashing function against the provided password using the parameters
186+ /// from the provided password hash and see if the computed output matches.
187+ ///
188+ /// # Errors
189+ /// - Returns `Error::Algorithm` if the algorithm being requested by the hash `H` is unsupported
190+ /// - Returns `Error::PasswordInvalid` if the hash for the supplied password does not match
191+ /// the provided hash
192+ /// - May return other algorithm-specific errors
162193 fn verify_password ( & self , password : & [ u8 ] , hash : & H ) -> Result < ( ) > ;
163194}
164195
@@ -207,17 +238,27 @@ pub trait McfHasher {
207238 ///
208239 /// MCF hashes are otherwise largely unstructured and parsed according to
209240 /// algorithm-specific rules so hashers must parse a raw string themselves.
241+ ///
242+ /// # Errors
243+ /// Returns errors if the the hash couldn't be converted
210244 fn upgrade_mcf_hash ( & self , hash : & str ) -> Result < phc:: PasswordHash > ;
211245}
212246
213247/// Generate a random salt value of the recommended length using the system's secure RNG.
248+ ///
249+ /// # Panics
250+ /// If the system's secure RNG experiences an internal failure.
214251#[ cfg( feature = "getrandom" ) ]
252+ #[ must_use]
215253pub fn generate_salt ( ) -> [ u8 ; RECOMMENDED_SALT_LEN ] {
216254 try_generate_salt ( ) . expect ( "RNG failure" )
217255}
218256
219257/// Try generating a random salt value of the recommended length using the system's secure RNG,
220258/// returning errors if they occur.
259+ ///
260+ /// # Errors
261+ /// If the system's secure RNG experiences an internal failure.
221262#[ cfg( feature = "getrandom" ) ]
222263pub fn try_generate_salt ( ) -> core:: result:: Result < [ u8 ; RECOMMENDED_SALT_LEN ] , getrandom:: Error > {
223264 let mut salt = [ 0u8 ; RECOMMENDED_SALT_LEN ] ;
0 commit comments