Commit 8f30bb84ac2a9e46b386cc2febf8852531e7828d
Merge pull request #53 from trevorturk/master
Password cost should be set to DEFAULT_COST if nilAman Gupta committed on 3/31/2013, 3:18:01 AM
Parent: 925671db0567373b7e01246b2ba0f82527871940
Parent: aa9e2bcc54b663950d1ce5addb7fb46e62643946
Files changed
lib/bcrypt.rb | changed |
spec/bcrypt/password_spec.rb | changed |
lib/bcrypt.rb | ||
---|---|---|
@@ -116,9 +116,9 @@ | ||
116 | 116 | # Example usage: |
117 | 117 | # |
118 | 118 | # include BCrypt |
119 | 119 | # |
120 | - # # hash a user's password | |
120 | + # # hash a user's password | |
121 | 121 | # @password = Password.create("my grand secret") |
122 | 122 | # @password #=> "$2a$10$GtKs1Kbsig8ULHZzO1h2TetZfhO4Fmlxphp8bVKnUlZCBYYClPohG" |
123 | 123 | # |
124 | 124 | # # store it safely |
@@ -151,11 +151,12 @@ | ||
151 | 151 | # |
152 | 152 | # Example: |
153 | 153 | # |
154 | 154 | # @password = BCrypt::Password.create("my secret", :cost => 13) |
155 | - def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST }) | |
156 | - raise ArgumentError if options[:cost] > 31 | |
157 | - Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost]), options[:cost])) | |
155 | + def create(secret, options = {}) | |
156 | + cost = options[:cost] || BCrypt::Engine::DEFAULT_COST | |
157 | + raise ArgumentError if cost > 31 | |
158 | + Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost), cost)) | |
158 | 159 | end |
159 | 160 | |
160 | 161 | def valid_hash?(h) |
161 | 162 | h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/ |
spec/bcrypt/password_spec.rb | ||
---|---|---|
@@ -39,8 +39,16 @@ | ||
39 | 39 | BCrypt::Password.create("hello", :cost => 32) |
40 | 40 | }.should raise_error(ArgumentError) |
41 | 41 | end |
42 | 42 | |
43 | + specify "the cost should be set to the default if nil" do | |
44 | + BCrypt::Password.create("hello", :cost => nil).cost.should equal(BCrypt::Engine::DEFAULT_COST) | |
45 | + end | |
46 | + | |
47 | + specify "the cost should be set to the default if empty hash" do | |
48 | + BCrypt::Password.create("hello", {}).cost.should equal(BCrypt::Engine::DEFAULT_COST) | |
49 | + end | |
50 | + | |
43 | 51 | specify "should read the version, cost, salt, and hash" do |
44 | 52 | password = BCrypt::Password.new(@hash) |
45 | 53 | password.version.should eql("2a") |
46 | 54 | password.cost.should equal(5) |
Built with git-ssb-web