Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing gpg Encryption in Ruby

Trying to convert some old shell/unix scripts into Ruby.

I have the following encryption of a file that's accomplished via the gpg tool in Unix. I can pass in the recipient key, the file I want to encrypt, and the outfile to pgp encrypt something.

gpg --recipient "$my_recipient_key" \
    --encrypt "$my_file" \
    --output "$my_outfile" \
    --always-trust \
    --compress-algo zip

What's the Ruby equivalent to do a simple encryption as above?

After doing some digging, I see:

  1. OpenPGP is popular, but there's no documentation on the RubyGems site and other places are sparse with good examples.
  2. gpgme seems popular and promising. However, I'll be honest, I'm new to Ruby and clicking on the documentation (see link) leaves me a little bit confused about where to start (i.e. a good example would be nice)
  3. I also see other smaller gems and libraries that users have designed, but I'm restricted to using certified gems from the rubyGems database as this is for official work use.

Thanks!

like image 711
user2490003 Avatar asked Jun 17 '13 04:06

user2490003


1 Answers

I've used gpgme recently. The code to encrypt a file looked like this

GPGME::Key.import(File.open(path_to_key)) #only needed if the key has not been imported previously
crypto = GPGME::Crypto.new :always_trust => true
File.open(path_to_encrypt) do |in_file|
  File.open(output_path, 'wb') do |out_file|
    crypto.encrypt in_file, :output => out_file, :recipients => "[email protected]"
  end
end

You might want to know that there's nothing "certified" about what's on rubygems - anyone can post a gem there (and it takes all of 3 minutes to do so)

like image 195
Frederick Cheung Avatar answered Oct 06 '22 00:10

Frederick Cheung