Class: Shhh::App::PrivateKey::Decryptor

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

Constant Summary

Constants included from Shhh

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encrypted_key, input_handler, password_cache) ⇒ Decryptor

Returns a new instance of Decryptor.



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

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.



9
10
11
# File 'lib/shhh/app/private_key/decryptor.rb', line 9

def encrypted_key
  @encrypted_key
end

#input_handlerObject

Returns the value of attribute input_handler.



9
10
11
# File 'lib/shhh/app/private_key/decryptor.rb', line 9

def input_handler
  @input_handler
end

#password_cacheObject

Returns the value of attribute password_cache.



9
10
11
# File 'lib/shhh/app/private_key/decryptor.rb', line 9

def password_cache
  @password_cache
end

Instance Method Details

#keyObject



18
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
# File 'lib/shhh/app/private_key/decryptor.rb', line 18

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(Shhh::Errors::InvalidPasswordPrivateKey.new('Invalid password.'))
      end
    end
  else
    decrypted_key = encrypted_key
  end
  decrypted_key
end