Class: MultiPassword::Strategies::SCrypt

Inherits:
Object
  • Object
show all
Includes:
MultiPassword::Strategy
Defined in:
lib/multi_password/strategies/scrypt.rb

Instance Method Summary collapse

Methods included from MultiPassword::Strategy

included

Instance Method Details

#create(password, options = {}) ⇒ Object



11
12
13
# File 'lib/multi_password/strategies/scrypt.rb', line 11

def create(password, options = {})
  ::SCrypt::Password.create(password, validate_options(options)).to_s
end

#validate_options(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/multi_password/strategies/scrypt.rb', line 19

def validate_options(options)
  return options if options.empty?

  key_len = options[:key_len]
  max_time = options[:max_time]
  max_mem = options[:max_mem]

  if key_len && (!key_len.is_a?(Integer) || key_len < 16 || key_len > 512)
    raise InvalidOptions.new('scrypt', 'key_len must be an integer between 16 and 512')
  end

  if max_time && (!max_time.is_a?(Integer) || max_time < 0 || max_time > 2)
    raise InvalidOptions.new('scrypt', 'max_time must be an integer between 0 and 2')
  end

  if max_mem && (!max_mem.is_a?(Numeric) || max_mem < 0 || max_mem > 256)
    raise InvalidOptions.new('scrypt', 'max_mem must be a number between 0 and 256')
  end

  options
end

#verify(password, encrypted_password) ⇒ Object



15
16
17
# File 'lib/multi_password/strategies/scrypt.rb', line 15

def verify(password, encrypted_password)
  ::SCrypt::Password.new(encrypted_password) == password
end