Skip to content

Instantly share code, notes, and snippets.

@tadast
Created July 14, 2011 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadast/1082225 to your computer and use it in GitHub Desktop.
Save tadast/1082225 to your computer and use it in GitHub Desktop.
class GpgWrapper
class GpgNotInstalled < StandardError; end
class CanNotFindPgpKey < StandardError; end
attr_reader :file_to_encrypt, :tmp_dir, :pgp_key, :email
def initialize(file_content, for_what = "customer")
check_gpg
load_config(for_what)
random_filename = "#{String.random}_#{Time.now.to_i}.xml"
@file_to_encrypt = path_for(random_filename)
File.open(file_to_encrypt, "w") do |f|
f.write(file_content)
end
end
def encrypt
import_key
output_file = path_for("#{String.random}_#{Time.now.to_i}.encrypted")
`#{GpgWrapper.command} -r #{email} -q --no-verbose --always-trust -a -o #{output_file} --yes -e #{file_to_encrypt}`
File.read(output_file)
end
def import_key
raise CanNotFindPgpKey unless pgp_key
`#{GpgWrapper.command} --import #{pgp_key}`
end
def self.command
'/usr/bin/env gpg'
end
private
def path_for(filename)
@tmp_dir ||= Dir.tmpdir
File.join(tmp_dir, filename)
end
def check_gpg
raise GpgNotInstalled.new("Please install gpg ('brew install gnupg')") unless `#{GpgWrapper.command} --version`.present?
end
def load_config(for_what)
@pgp_key = File.join(Rails.root, PistachioConf.config.third_party_keys.send(:[], for_what.to_s)["key_path"])
@email = PistachioConf.config.third_party_keys.send(:[], for_what.to_s)["email"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment