Method: BCrypt::Engine.generate_salt

Defined in:
lib/bcrypt/engine.rb

.generate_salt(cost = self.cost) ⇒ Object

Generates a random salt with a given computational cost.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bcrypt/engine.rb', line 62

def self.generate_salt(cost = self.cost)
  cost = cost.to_i
  if cost > 0
    if cost < MIN_COST
      cost = MIN_COST
    end
    if RUBY_PLATFORM == "java"
      Java.bcrypt_jruby.BCrypt.gensalt(cost)
    else
      prefix = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
      __bc_salt(prefix, cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH))
    end
  else
    raise Errors::InvalidCost.new("cost must be numeric and > 0")
  end
end