Class: BCrypt::Engine

Class | BCrypt::Engine
In: | lib/bcrypt.rb lib/bcrypt.rb
Parent: | Object

A Ruby wrapper for the bcrypt() extension calls.

### Methods

calibrate calibrate generate_salt generate_salt hash_secret hash_secret valid_salt? valid_salt? valid_secret? valid_secret?

### Constants

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.

### Public Class methods

calibrate(upper_time_limit_in_ms)

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)

[Source]

        # 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

calibrate(upper_time_limit_in_ms)

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)

[Source]

        # 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

generate_salt(cost = DEFAULT_COST)

Generates a random salt with a given computational cost.

[Source]

        # 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

generate_salt(cost = DEFAULT_COST)

Generates a random salt with a given computational cost.

[Source]

        # 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

hash_secret(secret, salt)

Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.

[Source]

        # 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

hash_secret(secret, salt)

Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash.

[Source]

        # 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

valid_salt?(salt)

Returns true if `salt` is a valid bcrypt() salt, false if not.

[Source]

        # 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

valid_salt?(salt)

Returns true if `salt` is a valid bcrypt() salt, false if not.

[Source]

        # 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

valid_secret?(secret)

Returns true if `secret` is a valid bcrypt() secret, false if not.

[Source]

        # File lib/bcrypt.rb, line 58
    58:     def self.valid_secret?(secret)
    59:       secret.respond_to?(:to_s)
    60:     end

valid_secret?(secret)

Returns true if `secret` is a valid bcrypt() secret, false if not.

[Source]

        # File lib/bcrypt.rb, line 58
    58:     def self.valid_secret?(secret)
    59:       secret.respond_to?(:to_s)
    60:     end

[Validate]