class MacSetup::Secrets

Constants

CIPHER
CIPHERTEXT_EXT
CRYPTO_LIB
PLAINTEXT_EXT

Attributes

files[R]
password[R]

Public Class Methods

decrypt(dir_or_files) click to toggle source
# File lib/mac_setup/secrets.rb, line 14
def self.decrypt(dir_or_files)
  new(filter_files(dir_or_files, CIPHERTEXT_EXT)).decrypt
end
encrypt(dir_or_files) click to toggle source
# File lib/mac_setup/secrets.rb, line 10
def self.encrypt(dir_or_files)
  new(filter_files(dir_or_files, PLAINTEXT_EXT)).encrypt
end
encrypted?(file) click to toggle source
# File lib/mac_setup/secrets.rb, line 18
def self.encrypted?(file)
  file.to_s.end_with?(CIPHERTEXT_EXT)
end
filter_files(dir_or_files, extension) click to toggle source
# File lib/mac_setup/secrets.rb, line 28
def self.filter_files(dir_or_files, extension)
  if dir_or_files.is_a?(Array)
    dir_or_files.select { |file| file.to_s.end_with?(extension) }
  else
    Dir.glob("#{File.expand_path(dir_or_files)}/**/*.#{extension}")
  end
end
new(files) click to toggle source
# File lib/mac_setup/secrets.rb, line 36
def initialize(files)
  @files = files
end
strip_extension(file) click to toggle source
# File lib/mac_setup/secrets.rb, line 22
def self.strip_extension(file)
  return file unless file.to_s.end_with?(PLAINTEXT_EXT)

  file.sub(/\.#{PLAINTEXT_EXT}$/, "")
end

Public Instance Methods

decrypt() click to toggle source
# File lib/mac_setup/secrets.rb, line 44
def decrypt
  if files.any?
    MacSetup.log "Decrypting files:"
  else
    MacSetup.log "No files to decrypt. Skipping"
  end

  list_files
  get_password
  decrypt_files
end
encrypt() click to toggle source
# File lib/mac_setup/secrets.rb, line 40
def encrypt
  do_crypt("encrypt") { encrypt_files }
end

Private Instance Methods

base_command(password) click to toggle source
# File lib/mac_setup/secrets.rb, line 132
def base_command(password)
  %W(#{CRYPTO_LIB} enc -#{CIPHER} -k #{password})
end
decrypt_command(file, target_path) click to toggle source
# File lib/mac_setup/secrets.rb, line 128
def decrypt_command(file, target_path)
  %W(openssl enc -aes-256-cbc -k testme -d -in #{file} -out #{target_path})
end
decrypt_file(file, log: true) click to toggle source
# File lib/mac_setup/secrets.rb, line 107
def decrypt_file(file, log: true)
  target_path = file.sub(/#{CIPHERTEXT_EXT}$/, PLAINTEXT_EXT)
  command = -> { Shell.run(decrypt_command(file, target_path)) }

  if log
    MacSetup.log "Decrypting #{raw_file_path(file)}", &command
  else
    command.call
  end
end
decrypt_files() click to toggle source
# File lib/mac_setup/secrets.rb, line 89
def decrypt_files
  if password_correct?
    do_decrypt
  else
    puts "Wrong Password!"
    get_password
    decrypt_files
  end
end
do_crypt(type) { || ... } click to toggle source
# File lib/mac_setup/secrets.rb, line 58
def do_crypt(type)
  if files.any?
    MacSetup.log "#{titleized(type)}ing files:"
    list_files
    get_password
    yield
  else
    MacSetup.log "No files to #{type}. Skipping"
  end
end
do_decrypt() click to toggle source
# File lib/mac_setup/secrets.rb, line 103
def do_decrypt
  files.each { |file| decrypt_file(file) }
end
do_encrypt(file, target_path) click to toggle source
# File lib/mac_setup/secrets.rb, line 118
def do_encrypt(file, target_path)
  MacSetup.log "Encrypting #{raw_file_path(file)}" do
    Shell.run(encrypt_command(file, target_path))
  end
end
encrypt_command(file, target_path) click to toggle source
# File lib/mac_setup/secrets.rb, line 124
def encrypt_command(file, target_path)
  %W(openssl enc -aes-256-cbc -k testme -in #{file} -out #{target_path})
end
encrypt_files() click to toggle source
# File lib/mac_setup/secrets.rb, line 82
def encrypt_files
  files.each do |file|
    target_path = file.sub(/#{PLAINTEXT_EXT}$/, CIPHERTEXT_EXT)
    do_encrypt(file, target_path)
  end
end
get_password() click to toggle source
# File lib/mac_setup/secrets.rb, line 78
def get_password
  @password = Shell.password
end
list_files() click to toggle source
# File lib/mac_setup/secrets.rb, line 74
def list_files
  files.each { |file| puts "  #{MacSetup.shorten_path(file)}" }
end
password_correct?() click to toggle source
# File lib/mac_setup/secrets.rb, line 99
def password_correct?
  Shell.success?(decrypt_command(files.first, files.first.sub(/#{CIPHERTEXT_EXT}$/, PLAINTEXT_EXT)))
end
raw_file_path(file) click to toggle source
# File lib/mac_setup/secrets.rb, line 136
def raw_file_path(file)
  raw_file = file.sub(/\.#{CIPHERTEXT_EXT}|#{PLAINTEXT_EXT}$/, "")
  MacSetup.shorten_path(raw_file)
end
titleized(str) click to toggle source
# File lib/mac_setup/secrets.rb, line 69
def titleized(str)
  char, rest = str.split("", 2)
  char.upcase + rest
end