lib/symmetric_encryption/cli.rb in symmetric-encryption-4.1.0.beta1 vs lib/symmetric_encryption/cli.rb in symmetric-encryption-4.1.0
- old
+ new
@@ -68,11 +68,11 @@
puts parser
end
end
def parser
- @parser ||= OptionParser.new do |opts|
+ @parser ||= OptionParser.new do |opts|
opts.banner = <<~BANNER
Symmetric Encryption v#{VERSION}
For more information, see: https://rocketjob.github.io/symmetric-encryption/
@@ -97,14 +97,18 @@
opts.on '-P', '--prompt', 'When encrypting or decrypting, prompt for a string encrypt or decrypt.' do
@prompt = true
end
- opts.on '-z', '--compress', 'Compress encrypted output file.' do
+ opts.on '-z', '--compress', 'Compress encrypted output file. [Default for encrypting files]' do
@compress = true
end
+ opts.on '-Z', '--no-compress', 'Does not compress the output file. [Default for encrypting strings]' do
+ @compress = false
+ end
+
opts.on '-E', '--env ENVIRONMENT', "Environment to use in the config file. Default: SYMMETRIC_ENCRYPTION_ENV || RACK_ENV || RAILS_ENV || 'development'" do |environment|
@environment = environment
end
opts.on '-c', '--config CONFIG_FILE_PATH', 'File name & path to the Symmetric Encryption configuration file. Default: config/symmetric-encryption.yml or Env var: `SYMMETRIC_ENCRYPTION_CONFIG`' do |path|
@@ -206,11 +210,11 @@
exit(-3)
end
config_file_does_not_exist!
self.environments ||= %i[development test release production]
- args = {
+ args = {
app_name: app_name,
environments: environments,
cipher_name: cipher_name
}
args[:key_path] = key_path if key_path
@@ -248,11 +252,12 @@
def run_cleanup_keys
config = Config.read_file(config_file_path)
config.each_pair do |env, cfg|
next if environments && !environments.include?(env.to_sym)
next unless ciphers = cfg[:ciphers]
- highest = ciphers.max_by { |i| i[:version] }
+
+ highest = ciphers.max_by { |i| i[:version] }
ciphers.clear
ciphers << highest
end
Config.write_file(config_file_path, config)
@@ -262,11 +267,12 @@
def run_activate_key
config = Config.read_file(config_file_path)
config.each_pair do |env, cfg|
next if environments && !environments.include?(env.to_sym)
next unless ciphers = cfg[:ciphers]
- highest = ciphers.max_by { |i| i[:version] }
+
+ highest = ciphers.max_by { |i| i[:version] }
ciphers.delete(highest)
ciphers.unshift(highest)
end
Config.write_file(config_file_path, config)
@@ -310,11 +316,11 @@
value1 = HighLine.new.ask('Enter the value to encrypt:') { |q| q.echo = '*' }
value2 = HighLine.new.ask('Re-enter the value to encrypt:') { |q| q.echo = '*' }
puts('Values do not match, please try again') if value1 != value2
end
-
+ compress = false if compress.nil?
encrypted = SymmetricEncryption.cipher(version).encrypt(value1, compress: compress)
output_file_name ? File.open(output_file_name, 'wb') { |f| f << encrypted } : puts("\n\nEncrypted: #{encrypted}\n\n")
end
def gen_random_password(size)
@@ -332,9 +338,10 @@
end
# Ensure that the config file does not already exist before generating a new one.
def config_file_does_not_exist!
return unless File.exist?(config_file_path)
+
puts "\nConfiguration file already exists, please move or rename: #{config_file_path}\n\n"
exit(-1)
end
end
end