Sha256: bd294c0c0d92aa6c9b2f256ae4538e552de2b0f548eebf389f6d92d0a856e809
Contents?: true
Size: 1.98 KB
Versions: 2
Compression:
Stored size: 1.98 KB
Contents
require 'open3' module RubyGpg extend self Config = Struct.new(:executable, :homedir) def config @config ||= Config.new("gpg", "~/.gnupg") end def gpg_command "#{config.executable} --homedir #{config.homedir} --quiet" + " --no-secmem-warning --no-permission-warning --no-tty --yes" end def encrypt(file, recipient, opts = {}) options = { :armor => false }.merge(opts) output = output_filename(file, options) ascii = options[:armor] == true ? "-a " : "" command = "#{gpg_command} #{ascii}--output #{output}" + " --recipient \"#{recipient}\" --encrypt #{file}" run_command(command) end # Encrypt a string from stdin def encrypt_string(string, recipient, opts = {}) command = gpg_command.dup command << " -a" if opts[:armor] command << " --encrypt" command << " --recipient \"#{recipient}\"" run_command(command, string) end def decrypt(file, passphrase = nil, opts = {}) outfile = opts[:output].nil? ? file.gsub(/\.gpg$|\.asc$/, '') : opts[:output] command = "#{gpg_command} --output #{outfile}" command << " --passphrase #{passphrase}" if passphrase command << " --decrypt #{file}" run_command(command) end def decrypt_string(string, passphrase = nil) command = gpg_command.dup command << " --passphrase #{passphrase}" if passphrase command << " --decrypt" run_command(command, string) end private def run_command(command, input = nil) output = "" Open3.popen3(command) do |stdin, stdout, stderr| stdin.write(input) if input stdin.close_write output << stdout.read error = stderr.read if error && !error.empty? raise "GPG command (#{command}) failed with: #{error}" end end output end # Return the output filename to use def output_filename(file, opts) extension = opts[:armor] ? "asc" : "gpg" opts[:output].nil? ? "#{file}.#{extension}" : opts[:output] end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
ruby_gpg-0.3.0 | lib/ruby_gpg.rb |
gitc-ruby_gpg-0.2.0 | lib/ruby_gpg.rb |