git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Commit 8f30bb84ac2a9e46b386cc2febf8852531e7828d

Merge pull request #53 from trevorturk/master

Password cost should be set to DEFAULT_COST if nil
Aman Gupta committed on 3/31/2013, 3:18:01 AM
Parent: 925671db0567373b7e01246b2ba0f82527871940
Parent: aa9e2bcc54b663950d1ce5addb7fb46e62643946

Files changed

lib/bcrypt.rbchanged
spec/bcrypt/password_spec.rbchanged
lib/bcrypt.rbView
@@ -116,9 +116,9 @@
116116 # Example usage:
117117 #
118118 # include BCrypt
119119 #
120- # # hash a user's password
120+ # # hash a user's password
121121 # @password = Password.create("my grand secret")
122122 # @password #=> "$2a$10$GtKs1Kbsig8ULHZzO1h2TetZfhO4Fmlxphp8bVKnUlZCBYYClPohG"
123123 #
124124 # # store it safely
@@ -151,11 +151,12 @@
151151 #
152152 # Example:
153153 #
154154 # @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))
158159 end
159160
160161 def valid_hash?(h)
161162 h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/
spec/bcrypt/password_spec.rbView
@@ -39,8 +39,16 @@
3939 BCrypt::Password.create("hello", :cost => 32)
4040 }.should raise_error(ArgumentError)
4141 end
4242
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+
4351 specify "should read the version, cost, salt, and hash" do
4452 password = BCrypt::Password.new(@hash)
4553 password.version.should eql("2a")
4654 password.cost.should equal(5)

Built with git-ssb-web