Commit 649682ef658d3dfcdecdb116a0a36fffc4678542
Moved calibration into BCrypt::Engine.
git-svn-id: http://bcrypt-ruby.rubyforge.org/svn/trunk@9 b1e0f299-433e-4bb3-9895-84128a6cfb6acodahale committed on 2/28/2007, 3:43:10 AM
Parent: 03c07302c3f65146490bf60e9a39bd16fe1dcf67
Files changed
lib/bcrypt.rb | changed |
spec/bcrypt/engine_spec.rb | changed |
spec/bcrypt/bcrypt_spec.rb | deleted |
lib/bcrypt.rb | ||
---|---|---|
@@ -48,28 +48,28 @@ | ||
48 | 48 … | # Returns true if +salt+ is a valid bcrypt() salt, false if not. |
49 | 49 … | def self.valid_salt?(salt) |
50 | 50 … | salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/ |
51 | 51 … | 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 | |
72 | 72 … | end |
73 | 73 … | end |
74 | 74 … | |
75 | 75 … | # A password management class which allows you to safely store users' passwords and compare them. |
@@ -127,14 +127,13 @@ | ||
127 | 127 … | raise Errors::InvalidHash.new("invalid hash") |
128 | 128 … | end |
129 | 129 … | end |
130 | 130 … | |
131 | - alias_method :exactly_equals, :== | |
132 | - | |
133 | 131 … | # Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise. |
134 | 132 … | def ==(secret) |
135 | - self.exactly_equals(BCrypt::Engine.hash(secret, @salt)) | |
133 … | + super(BCrypt::Engine.hash(secret, @salt)) | |
136 | 134 … | end |
135 … | + alias_method :is_password?, :== | |
137 | 136 … | |
138 | 137 … | private |
139 | 138 … | # Returns true if +h+ is a valid hash. |
140 | 139 … | def valid_hash?(h) |
spec/bcrypt/engine_spec.rb | ||
---|---|---|
@@ -1,6 +1,14 @@ | ||
1 | 1 … | require File.join(File.dirname(__FILE__), "..", "spec_helper") |
2 | 2 … | |
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 … | + | |
3 | 11 … | context "Generating BCrypt salts" do |
4 | 12 … | |
5 | 13 … | specify "should produce strings" do |
6 | 14 … | BCrypt::Engine.generate_salt.should be_an_instance_of(String) |
spec/bcrypt/bcrypt_spec.rb | ||
---|---|---|
@@ -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