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) ⇒ Decryptor

Returns a new instance of Decryptor.



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

def initialize(encrypted_key, input_handler)
  self.encrypted_key = encrypted_key
  self.input_handler = input_handler
  @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

Instance Method Details

#keyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shhh/app/private_key/decryptor.rb', line 17

def key
  return nil if encrypted_key.nil?
  decrypted_key = nil
  if should_decrypt?
    begin
      retries ||= 0

      p = password
      decrypted_key = decrypt(p)
      # if the password is valid, let's add it to the cache.
      Shhh::App::Cache.instance[encrypted_key] = p

    rescue ::OpenSSL::Cipher::CipherError => e
      input_handler.puts 'Invalid password. Please try again.'
      ((retries += 1) < 3) ? retry : raise(Shhh::Errors::InvalidPasswordPrivateKey.new('Invalid password.'))
    end
  else
    decrypted_key = encrypted_key
  end
  decrypted_key
end