Class: Veil::Hasher::BCrypt

Inherits:
Base
  • Object
show all
Defined in:
lib/veil/hasher/bcrypt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BCrypt

Create a new BCrypt

Parameters:

  • opts (Hash) (defaults to: {})

    a hash of options to pass to the constructor



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#saltObject (readonly)

Returns the value of attribute salt.



8
9
10
# File 'lib/veil/hasher/bcrypt.rb', line 8

def salt
  @salt
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/veil/hasher/bcrypt.rb', line 8

def secret
  @secret
end

Instance Method Details

#encrypt(group, name, version) ⇒ String

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

Parameters:

  • group (String)

    The service group name, eg: postgresql

  • name (String)

    The credential name, eg: sql_password

  • version (Integer)

    The Credential version, eg: 1

Returns:

  • (String)

    SHA512 hex digest of hashed data



42
43
44
# 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_hashHash<Symbol,String> Also known as: to_h

Return the instance as a Hash

Returns:

  • (Hash<Symbol,String>)


49
50
51
52
53
54
55
# File 'lib/veil/hasher/bcrypt.rb', line 49

def to_hash
  {
    type: self.class.name,
    secret: secret,
    salt: salt,
  }
end