ueno/ruby-gpgme

How to encrypt a file?

Closed this issue · 3 comments

From README, I see only the way how to encrypt a plaintext.
What do I have to do to encrypt a file itself as argument?

ueno commented

Can't you simply pass a File object? If you are using the GPGME::Crypto API, it should be wrapped internally into a GPGME::Data object.

okay, thx

ueno commented

I meant something like:

#!/usr/bin/env ruby
require 'gpgme'

crypto = GPGME::Crypto.new(:armor => true)
File.open('plain.txt') do |file|
  cipher = crypto.encrypt(file, {:symmetric => true})
  str = cipher.read
  puts("Ciphertext:\n#{str}")

  cipher = GPGME::Data.new(str)
  plain = crypto.decrypt(cipher)
  puts("Plaintext:\n#{plain.read}")
end

This actually works on my system.