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


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

def initialize(encrypted_key, input_handler)
  self.encrypted_key = encrypted_key
  self.input_handler = input_handler
end

Instance Attribute Details

#encrypted_keyObject

Returns the value of attribute encrypted_key


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

def encrypted_key
  @encrypted_key
end

#input_handlerObject

Returns the value of attribute input_handler


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

def input_handler
  @input_handler
end

Instance Method Details

#keyObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shhh/app/private_key/decryptor.rb', line 15

def key
  return nil if encrypted_key.nil?
  decrypted_key = nil
  if should_decrypt?
    begin
      retries ||= 0
      decrypted_key = decrypt(password)
    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