Sha256: ee09cff10e5dd7222298942670939481fc60549d5aac09ab24ba858edad32ddf

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require_relative 'decryptor'
module Shhh
  module App
    module PrivateKey
      class Decryptor
        include Shhh

        attr_accessor :encrypted_key, :input_handler

        def initialize(encrypted_key, input_handler = Shhh::App::Input::Handler)
          self.encrypted_key = encrypted_key
          self.input_handler = input_handler
        end

        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
              STDERR.puts 'Invalid password. Please try again.'
              ((retries += 1) < 3) ? retry : raise(Shhh::Errors::InvalidPasswordPrivateKey.new(e))
            end
          else
            decrypted_key = encrypted_key
          end
          decrypted_key
        end

        private

        def should_decrypt?
          encrypted_key && (encrypted_key.length > 32)
        end


        def decrypt(password)
          decr_password(encrypted_key, password)
        end

        def password
          input_handler.ask
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shhh-1.3.0 lib/shhh/app/private_key/decryptor.rb