git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Commit 2a84f0e01db71967573b42cc2e730d867c1ce555

Merge pull request #63 from tjschuck/make_cost_globally_changeable_externally

Allow a default cost to be set externally
Aman Gupta committed on 5/3/2013, 11:56:00 PM
Parent: 46157399e458e54e7c4186d3dbb88ac29fd3299c
Parent: f07a154d197c91c995590fabb5ae6d5f3f48ae97

Files changed

lib/bcrypt.rbchanged
spec/bcrypt/password_spec.rbchanged
lib/bcrypt.rbView
@@ -35,8 +35,13 @@
3535 private_class_method :__bc_salt
3636 private_class_method :__bc_crypt
3737 end
3838
39+ class << self
40+ attr_accessor :cost
41+ end
42+ self.cost = DEFAULT_COST
43+
3944 # Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates
4045 # a bcrypt() password hash.
4146 def self.hash_secret(secret, salt, cost = nil)
4247 if valid_secret?(secret)
@@ -58,9 +63,9 @@
5863 end
5964 end
6065
6166 # Generates a random salt with a given computational cost.
62- def self.generate_salt(cost = DEFAULT_COST)
67+ def self.generate_salt(cost = self.cost)
6368 cost = cost.to_i
6469 if cost > 0
6570 if cost < MIN_COST
6671 cost = MIN_COST
@@ -154,9 +159,9 @@
154159 # Example:
155160 #
156161 # @password = BCrypt::Password.create("my secret", :cost => 13)
157162 def create(secret, options = {})
158- cost = options[:cost] || BCrypt::Engine::DEFAULT_COST
163+ cost = options[:cost] || BCrypt::Engine.cost
159164 raise ArgumentError if cost > 31
160165 Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost), cost))
161166 end
162167
spec/bcrypt/password_spec.rbView
@@ -47,8 +47,17 @@
4747 specify "the cost should be set to the default if empty hash" do
4848 BCrypt::Password.create("hello", {}).cost.should equal(BCrypt::Engine::DEFAULT_COST)
4949 end
5050
51+ specify "the cost should be set to the passed value if provided" do
52+ BCrypt::Password.create("hello", :cost => 5).cost.should equal(5)
53+ end
54+
55+ specify "the cost should be set to the global value if set" do
56+ BCrypt::Engine.cost = 5
57+ BCrypt::Password.create("hello").cost.should equal(5)
58+ end
59+
5160 specify "should read the version, cost, salt, and hash" do
5261 password = BCrypt::Password.new(@hash)
5362 password.version.should eql("2a")
5463 password.cost.should equal(5)

Built with git-ssb-web