Class: Veil::Hasher::PBKDF2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PBKDF2

Create a new PBKDF2

Parameters:

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

    a hash of options to pass to the constructor



13
14
15
16
17
18
# 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

Instance Attribute Details

#hash_functionObject (readonly)

Returns the value of attribute hash_function.



7
8
9
# File 'lib/veil/hasher/pbkdf2.rb', line 7

def hash_function
  @hash_function
end

#iterationsObject (readonly)

Returns the value of attribute iterations.



7
8
9
# File 'lib/veil/hasher/pbkdf2.rb', line 7

def iterations
  @iterations
end

#saltObject (readonly)

Returns the value of attribute salt.



7
8
9
# File 'lib/veil/hasher/pbkdf2.rb', line 7

def salt
  @salt
end

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/veil/hasher/pbkdf2.rb', line 7

def secret
  @secret
end

Instance Method Details

#encrypt(group, name, version) ⇒ String

Hash data with the stored secret and salt

Parameters:

  • data (String)

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

  • opts (Hash)

    Optional parameter overrides

Returns:

  • (String)

    SHA512 hex digest of hashed data



29
30
31
32
33
34
35
36
37
# 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_hashHash Also known as: to_h

Return the instance as a Hash

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
# 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