Class | BCrypt::Engine |
In: |
lib/bcrypt.rb
lib/bcrypt.rb |
Parent: | Object |
A Ruby wrapper for the bcrypt() extension calls.
DEFAULT_COST | = | 10 | The default computational expense parameter. | |
MAX_SALT_LENGTH | = | 16 | Maximum possible size of bcrypt() salts. | |
DEFAULT_COST | = | 10 | The default computational expense parameter. | |
MAX_SALT_LENGTH | = | 16 | Maximum possible size of bcrypt() salts. |
Returns the cost factor which will result in computation times less than upper_time_limit_in_ms.
Example:
BCrypt.calibrate(200) #=> 10 BCrypt.calibrate(1000) #=> 12 # should take less than 200ms BCrypt::Password.create("woo", :cost => 10) # should take less than 1000ms BCrypt::Password.create("woo", :cost => 12)
# File lib/bcrypt.rb, line 74 74: def self.calibrate(upper_time_limit_in_ms) 75: 40.times do |i| 76: start_time = Time.now 77: Password.create("testing testing", :cost => i+1) 78: end_time = Time.now - start_time 79: return i if end_time * 1_000 > upper_time_limit_in_ms 80: end 81: end
Returns the cost factor which will result in computation times less than upper_time_limit_in_ms.
Example:
BCrypt.calibrate(200) #=> 10 BCrypt.calibrate(1000) #=> 12 # should take less than 200ms BCrypt::Password.create("woo", :cost => 10) # should take less than 1000ms BCrypt::Password.create("woo", :cost => 12)
# File lib/bcrypt.rb, line 74 74: def self.calibrate(upper_time_limit_in_ms) 75: 40.times do |i| 76: start_time = Time.now 77: Password.create("testing testing", :cost => i+1) 78: end_time = Time.now - start_time 79: return i if end_time * 1_000 > upper_time_limit_in_ms 80: end 81: end
Generates a random salt with a given computational cost.
# File lib/bcrypt.rb, line 44 44: def self.generate_salt(cost = DEFAULT_COST) 45: if cost.to_i > 0 46: __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH)) 47: else 48: raise Errors::InvalidCost.new("cost must be numeric and > 0") 49: end 50: end
Generates a random salt with a given computational cost.
# File lib/bcrypt.rb, line 44 44: def self.generate_salt(cost = DEFAULT_COST) 45: if cost.to_i > 0 46: __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH)) 47: else 48: raise Errors::InvalidCost.new("cost must be numeric and > 0") 49: end 50: end
Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.
# File lib/bcrypt.rb, line 31 31: def self.hash_secret(secret, salt) 32: if valid_secret?(secret) 33: if valid_salt?(salt) 34: __bc_crypt(secret.to_s, salt) 35: else 36: raise Errors::InvalidSalt.new("invalid salt") 37: end 38: else 39: raise Errors::InvalidSecret.new("invalid secret") 40: end 41: end
Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.
# File lib/bcrypt.rb, line 31 31: def self.hash_secret(secret, salt) 32: if valid_secret?(secret) 33: if valid_salt?(salt) 34: __bc_crypt(secret.to_s, salt) 35: else 36: raise Errors::InvalidSalt.new("invalid salt") 37: end 38: else 39: raise Errors::InvalidSecret.new("invalid secret") 40: end 41: end
Returns true if salt is a valid bcrypt() salt, false if not.
# File lib/bcrypt.rb, line 53 53: def self.valid_salt?(salt) 54: salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/ 55: end
Returns true if salt is a valid bcrypt() salt, false if not.
# File lib/bcrypt.rb, line 53 53: def self.valid_salt?(salt) 54: salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/ 55: end
Returns true if secret is a valid bcrypt() secret, false if not.
# File lib/bcrypt.rb, line 58 58: def self.valid_secret?(secret) 59: secret.respond_to?(:to_s) 60: end