module HandsomeFencer::Crypto

Constants

VERSION

Public Class Methods

build_cipher(environment) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 28
def build_cipher(environment)
  @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
  @salt = '8 octets'
  @pass_phrase = get_key(environment)
  @cipher.encrypt.pkcs5_keyivgen @pass_phrase, @salt
end
decrypt(file, environment=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 42
def decrypt(file, environment=nil)
  environment ||= file.split('.')[-3].split('/').last
  build_cipher(environment)
  encrypted = Base64.decode64 File.read(file)
  @cipher.decrypt.pkcs5_keyivgen @pass_phrase, @salt
  decrypted = @cipher.update(encrypted) + @cipher.final
  decrypted_file = file.split('.enc').first
  write_to_file decrypted, decrypted_file
end
encrypt(file, environment=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 35
def encrypt(file, environment=nil)
  environment ||= file.split('.')[-2].split('/').last
  build_cipher(environment)
  encrypted = @cipher.update(File.read file) + @cipher.final
  write_to_file(Base64.encode64(encrypted), file + '.enc')
end
expose(env=nil, dir=nil, ext=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 57
def expose(env=nil, dir=nil, ext=nil)
  ext = ext || "#{env}.env.enc"
  source_files(dir, ext).each { |file| decrypt(file, env) }
end
generate_key() click to toggle source
# File lib/handsome_fencer/crypto.rb, line 9
def generate_key
  @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
  @salt = '8 octets'
  @new_key = @cipher.random_key
  Base64.encode64(@new_key)
end
generate_key_file(directory, environment) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 20
def generate_key_file(directory, environment)
  write_to_file(generate_key, directory + "/" + environment + ".key")
end
get_key(environment, directory=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 62
def get_key(environment, directory=nil)
  env_key = environment.upcase + '_KEY'
  key_file = source_files('./.', "#{directory}/#{environment}.key").first
  case
  when ENV[env_key].nil? && key_file.nil?
    raise DeployKeyError, "No #{env_key} set."
  when ENV[env_key]
    ENV[env_key]
  when File.exist?(key_file)
    File.read(key_file).strip
  end
end
obfuscate(env=nil, dir=nil, ext=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 52
def obfuscate(env=nil, dir=nil, ext=nil)
  ext = ext || "#{env}.env"
  source_files(dir, ext).each { |file| encrypt(file, env) }
end
source_files(directory=nil, extension=nil) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 24
def source_files(directory=nil, extension=nil)
  Dir.glob(directory + "/**/*#{extension}")
end
write_to_file(data, filename) click to toggle source
# File lib/handsome_fencer/crypto.rb, line 16
def write_to_file(data, filename)
  File.open(filename, "w") { |io| io.write data }
end