Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@
- Update C and Java implementations to latest versions [GH #182 by @fonica]
- Bump default cost to 12 [GH #181 by @bdewater]
- Remove explicit support for Rubies 1.8 and 1.9
- Start calibration from the minimum cost supported by the algorithm [GH #206 by @sergey-alekseev]
4 changes: 3 additions & 1 deletion lib/bcrypt/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Engine
DEFAULT_COST = 12
# The minimum cost supported by the algorithm.
MIN_COST = 4
# The maximum cost supported by the algorithm.
MAX_COST = 31
# Maximum possible size of bcrypt() salts.
MAX_SALT_LENGTH = 16

Expand Down Expand Up @@ -99,7 +101,7 @@ def self.valid_secret?(secret)
# # should take less than 1000ms
# BCrypt::Password.create("woo", :cost => 12)
def self.calibrate(upper_time_limit_in_ms)
40.times do |i|
(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
Expand Down
2 changes: 1 addition & 1 deletion lib/bcrypt/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class << self
# @password = BCrypt::Password.create("my secret", :cost => 13)
def create(secret, options = {})
cost = options[:cost] || BCrypt::Engine.cost
raise ArgumentError if cost > 31
raise ArgumentError if cost > BCrypt::Engine::MAX_COST
Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost)))
end

Expand Down
10 changes: 10 additions & 0 deletions spec/bcrypt/engine_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))

describe 'BCrypt::Engine' do
describe '.calibrate(upper_time_limit_in_ms)' do
context 'a tiny upper time limit provided' do
it 'returns a minimum cost supported by the algorithm' do
expect(BCrypt::Engine.calibrate(0.001)).to eq(4)
end
end
end
end

describe "The BCrypt engine" do
specify "should calculate the optimal cost factor to fit in a specific time" do
first = BCrypt::Engine.calibrate(100)
Expand Down