class Veil::Hasher::PBKDF2

Attributes

hash_function[R]
iterations[R]
salt[R]
secret[R]

Public Class Methods

new(opts = {}) click to toggle source

Create a new PBKDF2

@param [Hash] opts

a hash of options to pass to the constructor
# File lib/veil/hasher/pbkdf2.rb, line 13
def initialize(opts = {})
  @secret = opts[:secret] || SecureRandom.hex(512)
  @salt = opts[:salt] || SecureRandom.hex(128)
  @iterations = opts[:iterations] || 100_000
  @hash_function = OpenSSL::Digest.const_get((opts[:hash_function] || "SHA512")).new
end

Public Instance Methods

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

Hash data with the stored secret and salt

@param [String] data

The service name and version to be encrypted with the shared key

@param [Hash] opts

Optional parameter overrides

@return [String] SHA512 hex digest of hashed data

# File lib/veil/hasher/pbkdf2.rb, line 29
def encrypt(group, name, version)
  hex_digest(OpenSSL::PKCS5.pbkdf2_hmac(
    [secret, group, name, version].join,
    salt,
    iterations,
    hash_function.length,
    hash_function
  ))
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

Return the instance as a Hash

@return [Hash]

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