Class: KmsTools::CLI::Encrypt
- Inherits:
-
Object
- Object
- KmsTools::CLI::Encrypt
- Defined in:
- lib/kms-tools/cli/encrypt.rb
Overview
Class for handling encrypt operations from the CLI
Instance Method Summary collapse
-
#encrypt(source) ⇒ String
Handle encrypt command.
-
#encrypt_file(source) ⇒ String
Encrypt a file from source path.
-
#initialize(global_options, options) ⇒ Encrypt
constructor
Set up encryption handler.
Constructor Details
#initialize(global_options, options) ⇒ Encrypt
Set up encryption handler
14 15 16 17 18 19 |
# File 'lib/kms-tools/cli/encrypt.rb', line 14 def initialize(, ) @enc = KmsTools::Encrypter.new() @output = [:output] abort "You must specify a key alias as a flag when encrypting stdin!" unless STDIN.tty? || [:k] @enc.use_key_alias = Helpers.select_key(@enc) if @enc.master_key.nil? end |
Instance Method Details
#encrypt(source) ⇒ String
Handle encrypt command
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/kms-tools/cli/encrypt.rb', line 26 def encrypt(source) if File.file?(source) save_path = encrypt_file(source) Output.say("Encrypted file saved to: #{save_path}") elsif source.is_a?(String) encrypted_response = @enc.encrypt_string(source) if STDIN.tty? Output.say("Encrypted ciphertext (copy all text without surrounding whitespace):\n\n#{encrypted_response}\n\n") else print encrypted_response end else raise "Unknown input to encrypt..." end # clear source from memory for good measure source = nil end |
#encrypt_file(source) ⇒ String
Encrypt a file from source path
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/kms-tools/cli/encrypt.rb', line 49 def encrypt_file(source) ef = KmsTools::EncryptedFile.new(encrypter: @enc) ef.create_from_file(source) save_path = Helpers::get_save_path({ :prompt => "Save encrytped file to", :suggested_path => File.absolute_path(source.sub File.extname(source), ".kms") }) ef.save_encrypted(save_path) save_path end |