Class: PBKDF2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ PBKDF2

Returns a new instance of PBKDF2.

Yields:

  • (_self)

Yield Parameters:

  • _self (PBKDF2)

    the object that the method was called on

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pbkdf2.rb', line 4

def initialize(opts={})
  @hash_function = OpenSSL::Digest::Digest.new("sha256")
  
  # override with options
  opts.each_key do |k|
    if self.respond_to?("#{k}=")
      self.send("#{k}=", opts[k])
    else
      raise ArgumentError, "Argument '#{k}' is not allowed"
    end
  end
  
  yield self if block_given?

  # set this to the default if nothing was given
  @key_length ||= @hash_function.size
  
  # make sure the relevant things got set
  raise ArgumentError, "password not set" if @password.nil?
  raise ArgumentError, "salt not set" if @salt.nil?
  raise ArgumentError, "iterations not set" if @iterations.nil?
end

Instance Attribute Details

#hash_functionObject

Returns the value of attribute hash_function.



26
27
28
# File 'lib/pbkdf2.rb', line 26

def hash_function
  @hash_function
end

#iterationsObject

Returns the value of attribute iterations.



26
27
28
# File 'lib/pbkdf2.rb', line 26

def iterations
  @iterations
end

#key_lengthObject

Returns the value of attribute key_length.



26
27
28
# File 'lib/pbkdf2.rb', line 26

def key_length
  @key_length
end

#passwordObject

Returns the value of attribute password.



26
27
28
# File 'lib/pbkdf2.rb', line 26

def password
  @password
end

#saltObject

Returns the value of attribute salt.



26
27
28
# File 'lib/pbkdf2.rb', line 26

def salt
  @salt
end

Instance Method Details

#benchmark(iters = 400000) ⇒ Object

return number of milliseconds it takes to complete one iteration



68
69
70
71
72
73
74
75
76
# File 'lib/pbkdf2.rb', line 68

def benchmark(iters = 400000)
  iter_orig = @iterations
  @iterations=iters
  start = Time.now
  calculate!
  time = Time.now - start
  @iterations = iter_orig
  return (time/iters)
end

#hex_stringObject



63
64
65
# File 'lib/pbkdf2.rb', line 63

def hex_string
  bin_string.unpack("H*").first
end

#valueObject Also known as: bin_string



56
57
58
59
# File 'lib/pbkdf2.rb', line 56

def value
  calculate! if @value.nil?
  @value
end