Class: BCrypt::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/bcrypt.rb,
ext/bcrypt_ext.c

Overview

A Ruby wrapper for the bcrypt() extension calls.

Constant Summary collapse

DEFAULT_COST =

The default computational expense parameter.

10
MAX_SALT_LENGTH =

Maximum possible size of bcrypt() salts.

16

Class Method Summary collapse

Class Method Details

.__bc_crypt(key, salt) ⇒ Object

Given a secret and a salt, generates a salted hash (which you can then store safely).



18
19
20
# File 'ext/bcrypt_ext.c', line 18

static VALUE bc_crypt(VALUE self, VALUE key, VALUE salt) {
  return rb_str_new2((char *)bcrypt(RSTRING_PTR(key), (char *)RSTRING_PTR(salt)));
}

.__bc_salt(cost, seed) ⇒ Object

Given a logarithmic cost parameter, generates a salt for use with bc_crypt.



12
13
14
# File 'ext/bcrypt_ext.c', line 12

static VALUE bc_salt(VALUE self, VALUE cost, VALUE seed) {
  return rb_str_new2((char *)bcrypt_gensalt(NUM2INT(cost), (u_int8_t *)RSTRING_PTR(seed)));
}

.calibrate(upper_time_limit_in_ms) ⇒ Object

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)


74
75
76
77
78
79
80
81
# File 'lib/bcrypt.rb', line 74

def self.calibrate(upper_time_limit_in_ms)
  40.times do |i|
    start_time = Time.now
    Password.create("testing testing", :cost => i+1)
    end_time = Time.now - start_time
    return i if end_time * 1_000 > upper_time_limit_in_ms
  end
end

.generate_salt(cost = DEFAULT_COST) ⇒ Object

Generates a random salt with a given computational cost.



44
45
46
47
48
49
50
# File 'lib/bcrypt.rb', line 44

def self.generate_salt(cost = DEFAULT_COST)
  if cost.to_i > 0
    __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH))
  else
    raise Errors::InvalidCost.new("cost must be numeric and > 0")
  end
end

.hash_secret(secret, salt) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bcrypt.rb', line 31

def self.hash_secret(secret, salt)
  if valid_secret?(secret)
    if valid_salt?(salt)
      __bc_crypt(secret.to_s, salt)
    else
      raise Errors::InvalidSalt.new("invalid salt")
    end
  else
    raise Errors::InvalidSecret.new("invalid secret")
  end
end

.valid_salt?(salt) ⇒ Boolean

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

Returns:



53
54
55
# File 'lib/bcrypt.rb', line 53

def self.valid_salt?(salt)
  salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/
end

.valid_secret?(secret) ⇒ Boolean

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

Returns:



58
59
60
# File 'lib/bcrypt.rb', line 58

def self.valid_secret?(secret)
  secret.respond_to?(:to_s)
end