Method: BCrypt::Engine.calibrate

Defined in:
lib/bcrypt/engine.rb

.calibrate(upper_time_limit_in_ms) ⇒ Object

Returns the cost factor which will result in computation times less than upper_time_limit_in_ms.

Example:

BCrypt::Engine.calibrate(200)  #=> 10
BCrypt::Engine.calibrate(1000) #=> 12

# should take less than 200ms
BCrypt::Password.create("woo", :cost => 10)

# should take less than 1000ms
BCrypt::Password.create("woo", :cost => 12)


119
120
121
122
123
124
125
126
# File 'lib/bcrypt/engine.rb', line 119

def self.calibrate(upper_time_limit_in_ms)
  (BCrypt::Engine::MIN_COST..BCrypt::Engine::MAX_COST-1).each do |i|
    start_time = Time.now
    Password.create("testing testing", :cost => i+1)
    end_time = Time.now - start_time
    return i if end_time * 1_000 > upper_time_limit_in_ms
  end
end