Class | BCrypt::Engine |
In: |
lib/bcrypt.rb
lib/bcrypt.rb |
Parent: | Object |
A Ruby wrapper for the bcrypt() C extension calls and the Java calls.
DEFAULT_COST | = | 10 | The default computational expense parameter. | |
MIN_COST | = | 4 | The minimum cost supported by the algorithm. | |
MAX_SALT_LENGTH | = | 16 | Maximum possible size of bcrypt() salts. | |
DEFAULT_COST | = | 10 | The default computational expense parameter. | |
MIN_COST | = | 4 | The minimum cost supported by the algorithm. | |
MAX_SALT_LENGTH | = | 16 | Maximum possible size of bcrypt() salts. |
Autodetects the cost from the salt string.
# File lib/bcrypt.rb, line 108 108: def self.autodetect_cost(salt) 109: salt[4..5].to_i 110: end
Autodetects the cost from the salt string.
# File lib/bcrypt.rb, line 108 108: def self.autodetect_cost(salt) 109: salt[4..5].to_i 110: 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 98 98: def self.calibrate(upper_time_limit_in_ms) 99: 40.times do |i| 100: start_time = Time.now 101: Password.create("testing testing", :cost => i+1) 102: end_time = Time.now - start_time 103: return i if end_time * 1_000 > upper_time_limit_in_ms 104: end 105: 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 98 98: def self.calibrate(upper_time_limit_in_ms) 99: 40.times do |i| 100: start_time = Time.now 101: Password.create("testing testing", :cost => i+1) 102: end_time = Time.now - start_time 103: return i if end_time * 1_000 > upper_time_limit_in_ms 104: end 105: end
Generates a random salt with a given computational cost.
# File lib/bcrypt.rb, line 60 60: def self.generate_salt(cost = DEFAULT_COST) 61: cost = cost.to_i 62: if cost > 0 63: if cost < MIN_COST 64: cost = MIN_COST 65: end 66: if RUBY_PLATFORM == "java" 67: Java.bcrypt_jruby.BCrypt.gensalt(cost) 68: else 69: __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH)) 70: end 71: else 72: raise Errors::InvalidCost.new("cost must be numeric and > 0") 73: end 74: end
Generates a random salt with a given computational cost.
# File lib/bcrypt.rb, line 60 60: def self.generate_salt(cost = DEFAULT_COST) 61: cost = cost.to_i 62: if cost > 0 63: if cost < MIN_COST 64: cost = MIN_COST 65: end 66: if RUBY_PLATFORM == "java" 67: Java.bcrypt_jruby.BCrypt.gensalt(cost) 68: else 69: __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH)) 70: end 71: else 72: raise Errors::InvalidCost.new("cost must be numeric and > 0") 73: end 74: end
Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.
# File lib/bcrypt.rb, line 39 39: def self.hash_secret(secret, salt, cost = nil) 40: if valid_secret?(secret) 41: if valid_salt?(salt) 42: if cost.nil? 43: cost = autodetect_cost(salt) 44: end 45: 46: if RUBY_PLATFORM == "java" 47: Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s, salt.to_s) 48: else 49: __bc_crypt(secret.to_s, salt, cost) 50: end 51: else 52: raise Errors::InvalidSalt.new("invalid salt") 53: end 54: else 55: raise Errors::InvalidSecret.new("invalid secret") 56: end 57: end
Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.
# File lib/bcrypt.rb, line 39 39: def self.hash_secret(secret, salt, cost = nil) 40: if valid_secret?(secret) 41: if valid_salt?(salt) 42: if cost.nil? 43: cost = autodetect_cost(salt) 44: end 45: 46: if RUBY_PLATFORM == "java" 47: Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s, salt.to_s) 48: else 49: __bc_crypt(secret.to_s, salt, cost) 50: end 51: else 52: raise Errors::InvalidSalt.new("invalid salt") 53: end 54: else 55: raise Errors::InvalidSecret.new("invalid secret") 56: end 57: end
Returns true if salt is a valid bcrypt() salt, false if not.
# File lib/bcrypt.rb, line 77 77: def self.valid_salt?(salt) 78: salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/ 79: end
Returns true if salt is a valid bcrypt() salt, false if not.
# File lib/bcrypt.rb, line 77 77: def self.valid_salt?(salt) 78: salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/ 79: end
Returns true if secret is a valid bcrypt() secret, false if not.
# File lib/bcrypt.rb, line 82 82: def self.valid_secret?(secret) 83: secret.respond_to?(:to_s) 84: end