6

Whenever I put config.stretches = 20 in config/initializers/devise.rb, the server times out on encryption requests.

The reposnse time is bearable at 15 stretches, then rapidly increases as I raise stretches value, and becomes totally unusable at 19. I don't know much about the performance impact this value may deal, but it certainly shouldn't be exponential, which is what I experience. Apparently I don't need to raise this value for anything except authlogic compatibility, but it seems wrong anyway.

I verified this with totally fresh install of https://github.com/plataformatec/devise_example/.

This behavior is observed on Ubuntu 11.04, any 3.0.x rails version, 3.1.0.beta1, devise 1.3.1 and 1.3.4, mysql, pg, sqlite drivers. This holds true for brcypt as well as for sha1 encryptors.

punund
  • 4,321
  • 3
  • 34
  • 45

2 Answers2

6

This is the expected behavior (especially for bcrypt which is arguably better). The only purpose of this value is to degrade performance to increase security.

You don't want speed when hashing as this allows an attacker to try more things in a given time span. This article explains this: http://codahale.com/how-to-safely-store-a-password/ .

In devise, stretches is used to adjust the work factor higher so that passwords take a configurably long time to hash. The configurable nature is necessary for 2 reasons: 1) different applications have different acceptable performance characteristics and 2) as computers get faster you should be able to increase the work factor to keep the same performance.

The idea is that you should configure this value to be as high as you can while maintaining acceptable performance. The goal isn't to make log on take 60 seconds, it is to make it take longer than a microsecond or two. If you can find a value for stretches that slows requests down to around 200 milliseconds or so, that's probably where you want to be.

Ben Hughes
  • 14,075
  • 1
  • 41
  • 34
  • I understand the idea, but I still believe something is wrong. With authlogic, whose behavior Devise is supposedly mimicking by setting encryptor to :authlogic_sha512 and stretches to 20, there was no noticable delay with either login or signup. – punund May 08 '11 at 21:55
  • Shouldn't be an issue: the code is the same. https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/crypto_providers/sha512.rb vs. https://github.com/plataformatec/devise/blob/master/lib/devise/encryptors/authlogic_sha512.rb – Ben Hughes May 08 '11 at 22:33
  • Also, bcrypt is different, with the work factor being much slower. – Ben Hughes May 08 '11 at 22:33
6

It turned out that I didn't specify the :encryptable option in my model, and Devise was ignoring config.encryptor setting silently, and was, indeed, using bcrypt, which is really that slow at 20 stretches.

punund
  • 4,321
  • 3
  • 34
  • 45