RustCrypto / password-hashes

Password hashing functions / KDFs
673 stars 82 forks source link

scrypt: Provide read access to the parameter values #123

Closed demurgos closed 3 years ago

demurgos commented 3 years ago

Hi, I am currently migrating a Node application to Rust. This application uses scrypt-kdf which is itself based on node-scrypt. The kdf function in these libraries uses a different format than the one provided by this lib. I plan to eventually move to the PHC string format, but my first step is to perform a simple port.

When porting the Node algorithm, I need to write the current parameters into the output buffer. I planned to store my parameters directly as a scrypt::Param instance but this type does not allow to read the current values, preventing me from using it to write the Node format. My current solution is to store these parameters in my own struct, but it creates duplication. This is especially bad when using Params::recommended as I now have to copy the recommended values to my own crate.

Would it be possible to provide read access to the current values of the parameters?

These values are pub(crate) currently. I understand that making them public might not be desirable as it completely breaks encapsulation (a mut scrypt::Params would allow anyone to change its values at any time). Would it then be possible to add getter methods? Something like:

impl Params {
  pub fn r(&self) -> u32 {
    self.r
  }
  // ...
}

pub fn main() {
   let params = Params::recommended();
   let r = params.r();
   // ...
}
tarcieri commented 3 years ago

Sounds good to me.

@newpavlov you okay with the getter method approach?

newpavlov commented 3 years ago

Yes, sounds good.

tarcieri commented 3 years ago

I added accessors in 1bb21b1, but accidentally pushed master instead of opening a PR. Whoops.

Let me know if that looks good.

demurgos commented 3 years ago

Thank you very much :slightly_smiling_face: