@@ -63,6 +63,7 @@ impl Params {
6363 } ;
6464
6565 /// Recommended PBKDF2 parameters for the selected algorithm.
66+ #[ must_use]
6667 pub const fn recommended_for ( algorithm : Algorithm ) -> Self {
6768 let rounds = match algorithm {
6869 Algorithm :: Pbkdf2Sha256 => Self :: RECOMMENDED_ROUNDS ,
@@ -76,12 +77,20 @@ impl Params {
7677 }
7778
7879 /// Create new params with the given number of rounds.
80+ ///
81+ /// # Errors
82+ /// If the number of rounds is less than [`Params::MIN_ROUNDS`].
7983 #[ cfg( feature = "password-hash" ) ]
8084 pub const fn new ( rounds : u32 ) -> Result < Self > {
8185 Self :: new_with_output_len ( rounds, Self :: RECOMMENDED_OUTPUT_LENGTH )
8286 }
8387
8488 /// Create new params with a customized output length.
89+ ///
90+ /// # Errors
91+ /// - If the number of rounds is less than [`Params::MIN_ROUNDS`].
92+ /// - If `output_len` is shorter than [`Params::MIN_OUTPUT_LENGTH`].
93+ /// - If `output_len` is longer than [`Params::MAX_OUTPUT_LENGTH`].
8594 #[ cfg( feature = "password-hash" ) ]
8695 pub const fn new_with_output_len ( rounds : u32 , output_len : usize ) -> Result < Self > {
8796 if rounds < Self :: MIN_ROUNDS
@@ -95,11 +104,13 @@ impl Params {
95104 }
96105
97106 /// Get the number of rounds.
107+ #[ must_use]
98108 pub const fn rounds ( self ) -> u32 {
99109 self . rounds
100110 }
101111
102112 /// Get the output length.
113+ #[ must_use]
103114 pub const fn output_len ( self ) -> usize {
104115 self . output_len
105116 }
@@ -141,7 +152,7 @@ impl TryFrom<u32> for Params {
141152impl TryFrom < & ParamsString > for Params {
142153 type Error = Error ;
143154
144- fn try_from ( params_string : & ParamsString ) -> password_hash :: Result < Self > {
155+ fn try_from ( params_string : & ParamsString ) -> Result < Self > {
145156 let mut rounds = Params :: RECOMMENDED_ROUNDS ;
146157 let mut output_len = Params :: RECOMMENDED_OUTPUT_LENGTH ;
147158
@@ -179,7 +190,7 @@ impl TryFrom<&ParamsString> for Params {
179190impl TryFrom < & phc:: PasswordHash > for Params {
180191 type Error = Error ;
181192
182- fn try_from ( hash : & phc:: PasswordHash ) -> password_hash :: Result < Self > {
193+ fn try_from ( hash : & phc:: PasswordHash ) -> Result < Self > {
183194 if hash. version . is_some ( ) {
184195 return Err ( Error :: Version ) ;
185196 }
@@ -200,7 +211,7 @@ impl TryFrom<&phc::PasswordHash> for Params {
200211impl TryFrom < Params > for ParamsString {
201212 type Error = Error ;
202213
203- fn try_from ( params : Params ) -> password_hash :: Result < ParamsString > {
214+ fn try_from ( params : Params ) -> Result < ParamsString > {
204215 Self :: try_from ( & params)
205216 }
206217}
@@ -209,10 +220,11 @@ impl TryFrom<Params> for ParamsString {
209220impl TryFrom < & Params > for ParamsString {
210221 type Error = Error ;
211222
212- fn try_from ( input : & Params ) -> password_hash :: Result < ParamsString > {
223+ fn try_from ( input : & Params ) -> Result < ParamsString > {
213224 let mut output = ParamsString :: new ( ) ;
225+ let output_len = Decimal :: try_from ( input. output_len ) . map_err ( |_| Error :: OutputSize ) ?;
214226
215- for ( name, value) in [ ( "i" , input. rounds ) , ( "l" , input . output_len as Decimal ) ] {
227+ for ( name, value) in [ ( "i" , input. rounds ) , ( "l" , output_len) ] {
216228 output
217229 . add_decimal ( name, value)
218230 . map_err ( |_| Error :: ParamInvalid { name } ) ?;
0 commit comments