Class: Sym::App::PrivateKey::Decryptor

Inherits:
Object
  • Object
show all
Includes:
Sym
Defined in:
lib/sym/app/private_key/decryptor.rb

Constant Summary

Constants included from Sym

DESCRIPTION, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sym

config, default_key, default_key?, default_key_file

Constructor Details

#initialize(encrypted_key, input_handler, password_cache) ⇒ Decryptor

Returns a new instance of Decryptor.



12
13
14
15
16
17
# File 'lib/sym/app/private_key/decryptor.rb', line 12

def initialize(encrypted_key, input_handler, password_cache)
  self.encrypted_key  = encrypted_key
  self.input_handler  = input_handler
  self.password_cache = password_cache
  @cache_checked      = false
end

Instance Attribute Details

#encrypted_keyObject

Returns the value of attribute encrypted_key.



10
11
12
# File 'lib/sym/app/private_key/decryptor.rb', line 10

def encrypted_key
  @encrypted_key
end

#input_handlerObject

Returns the value of attribute input_handler.



10
11
12
# File 'lib/sym/app/private_key/decryptor.rb', line 10

def input_handler
  @input_handler
end

#password_cacheObject

Returns the value of attribute password_cache.



10
11
12
# File 'lib/sym/app/private_key/decryptor.rb', line 10

def password_cache
  @password_cache
end

Instance Method Details

#keyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sym/app/private_key/decryptor.rb', line 19

def key
  return nil if encrypted_key.nil?
  decrypted_key = nil
  if should_decrypt?
    begin
      retries                                   ||= 0
      p                                         = determine_key_password
      decrypted_key                             = decrypt(p)

      # if the password is valid, let's add it to the cache.
      password_cache[encrypted_key] = p

    rescue ::OpenSSL::Cipher::CipherError => e
      input_handler.puts 'Invalid password. Please try again.'

      if (retries += 1) < 3
        retry
      else
        raise(Sym::Errors::WrongPasswordForKey.new('Invalid password.'))
      end
    end
  else
    decrypted_key = encrypted_key
  end
  decrypted_key
end