class Veil::Hasher::BCrypt

Attributes

salt[R]
secret[R]

Public Class Methods

new(opts = {}) click to toggle source

Create a new BCrypt

@param [Hash] opts

a hash of options to pass to the constructor
# File lib/veil/hasher/bcrypt.rb, line 14
def initialize(opts = {})
  if opts[:secret] && opts[:salt]
    if ::BCrypt::Engine.valid_secret?(opts[:secret]) && ::BCrypt::Engine.valid_salt?(opts[:salt])
      @secret = opts.delete(:secret)
      @salt = opts.delete(:salt)
    elsif ::BCrypt::Engine.valid_secret?(opts[:secret])
      raise Veil::InvalidSalt.new("#{opts[:salt]} is not valid salt")
    else
      raise Veil::InvalidSecret.new("#{opts[:secret]} is not valid secret")
    end
  else
    @secret = SecureRandom.hex(512)
    @salt = ::BCrypt::Engine.generate_salt(opts[:cost] || 10)
  end
end

Public Instance Methods

encrypt(group, name, version) click to toggle source

Hash the credential group, name and version with the stored secret and salt

@param [String] group

The service group name, eg: postgresql

@param [String] name

The credential name, eg: sql_password

@param [Integer] version

The Credential version, eg: 1

@return [String] SHA512 hex digest of hashed data

# File lib/veil/hasher/bcrypt.rb, line 42
def encrypt(group, name, version)
  hex_digest(::BCrypt::Engine.hash_secret(hex_digest([secret, group, name, version].join), salt))
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

Return the instance as a Hash

@return [Hash<Symbol,String>]

# File lib/veil/hasher/bcrypt.rb, line 49
def to_hash
  {
    type: self.class.name,
    secret: secret,
    salt: salt,
  }
end
Also aliased as: to_h