diff options
| author | mo <mo.khan@gmail.com> | 2018-02-11 13:38:02 -0700 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2018-02-11 13:38:02 -0700 |
| commit | 74c4021f389593d2d195052b2710d9393843630f (patch) | |
| tree | 078c1c8350a9030923c5023046fa82d94d8482de | |
| parent | d0db2beb86ca58b852c60955a107f5ec8b687abb (diff) | |
check if a file is encrypted before decrypting.
| -rwxr-xr-x | exe/tfa | 8 | ||||
| -rw-r--r-- | lib/tfa/secure_proxy.rb | 15 |
2 files changed, 17 insertions, 6 deletions
@@ -1,5 +1,9 @@ #!/usr/bin/env ruby require 'tfa' -result = TFA::CLI.start(ARGV) -puts result unless result.is_a?(IO) +begin + result = TFA::CLI.start(ARGV) + puts result unless result.is_a?(IO) +rescue OpenSSL::Cipher::CipherError => error + puts error.message +end diff --git a/lib/tfa/secure_proxy.rb b/lib/tfa/secure_proxy.rb index 96481e5..fcfa55e 100644 --- a/lib/tfa/secure_proxy.rb +++ b/lib/tfa/secure_proxy.rb @@ -20,8 +20,6 @@ module TFA end def decrypt! - return unless File.exist?(@original.path) - data = JSON.parse(IO.read(@original.path), symbolize_names: true) decipher = OpenSSL::Cipher.new(data[:algorithm]) decipher.decrypt @@ -35,10 +33,19 @@ module TFA def method_missing(name, *args, &block) super unless @original.respond_to?(name) - decrypt! + was_encrypted = encrypted? + decrypt! if was_encrypted result = @original.public_send(name, *args, &block) - encrypt! + encrypt! if was_encrypted result end + + def encrypted? + return false unless File.exist?(@original.path) + JSON.parse(IO.read(@original.path)) + true + rescue JSON::ParseError + false + end end end |
