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