git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Commit 649682ef658d3dfcdecdb116a0a36fffc4678542

Moved calibration into BCrypt::Engine.

git-svn-id: http://bcrypt-ruby.rubyforge.org/svn/trunk@9 b1e0f299-433e-4bb3-9895-84128a6cfb6a
codahale committed on 2/28/2007, 3:43:10 AM
Parent: 03c07302c3f65146490bf60e9a39bd16fe1dcf67

Files changed

lib/bcrypt.rbchanged
spec/bcrypt/engine_spec.rbchanged
spec/bcrypt/bcrypt_spec.rbdeleted
lib/bcrypt.rbView
@@ -48,28 +48,28 @@
4848 # Returns true if +salt+ is a valid bcrypt() salt, false if not.
4949 def self.valid_salt?(salt)
5050 salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/
5151 end
52- end
53-
54- # Returns the cost factor which will result in computation times less than +upper_time_limit_in_ms+.
55- #
56- # Example:
57- #
58- # BCrypt.calibrate(200) #=> 10
59- # BCrypt.calibrate(1000) #=> 12
60- #
61- # # should take less than 200ms
62- # BCrypt::Password.create("woo", :cost => 10)
63- #
64- # # should take less than 1000ms
65- # BCrypt::Password.create("woo", :cost => 12)
66- def self.calibrate(upper_time_limit_in_ms)
67- 40.times do |i|
68- start_time = Time.now
69- Password.create("testing testing", :cost => i+1)
70- end_time = Time.now - start_time
71- return i if end_time * 1_000 > upper_time_limit_in_ms
52 +
53 + # Returns the cost factor which will result in computation times less than +upper_time_limit_in_ms+.
54 + #
55 + # Example:
56 + #
57 + # BCrypt.calibrate(200) #=> 10
58 + # BCrypt.calibrate(1000) #=> 12
59 + #
60 + # # should take less than 200ms
61 + # BCrypt::Password.create("woo", :cost => 10)
62 + #
63 + # # should take less than 1000ms
64 + # BCrypt::Password.create("woo", :cost => 12)
65 + def self.calibrate(upper_time_limit_in_ms)
66 + 40.times do |i|
67 + start_time = Time.now
68 + Password.create("testing testing", :cost => i+1)
69 + end_time = Time.now - start_time
70 + return i if end_time * 1_000 > upper_time_limit_in_ms
71 + end
7272 end
7373 end
7474
7575 # A password management class which allows you to safely store users' passwords and compare them.
@@ -127,14 +127,13 @@
127127 raise Errors::InvalidHash.new("invalid hash")
128128 end
129129 end
130130
131- alias_method :exactly_equals, :==
132-
133131 # Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
134132 def ==(secret)
135- self.exactly_equals(BCrypt::Engine.hash(secret, @salt))
133 + super(BCrypt::Engine.hash(secret, @salt))
136134 end
135 + alias_method :is_password?, :==
137136
138137 private
139138 # Returns true if +h+ is a valid hash.
140139 def valid_hash?(h)
spec/bcrypt/engine_spec.rbView
@@ -1,6 +1,14 @@
11 require File.join(File.dirname(__FILE__), "..", "spec_helper")
22
3 +context "The BCrypt engine" do
4 + specify "should calculate the optimal cost factor to fit in a specific time" do
5 + first = BCrypt::Engine.calibrate(100)
6 + second = BCrypt::Engine.calibrate(300)
7 + second.should >(first + 1)
8 + end
9 +end
10 +
311 context "Generating BCrypt salts" do
412
513 specify "should produce strings" do
614 BCrypt::Engine.generate_salt.should be_an_instance_of(String)
spec/bcrypt/bcrypt_spec.rbView
@@ -1,9 +1,0 @@
1-require File.join(File.dirname(__FILE__), "..", "spec_helper")
2-
3-context "BCrypt" do
4- specify "should calculate the optimal cost factor to fit in a specific time" do
5- first = BCrypt.calibrate(100)
6- second = BCrypt.calibrate(300)
7- second.should >(first + 1)
8- end
9-end

Built with git-ssb-web