Basic GPGME ruby gem usage
I just found this API ridiculously confusing to use, and maybe I just suck, but I don't want to figure it out again, so I'm writing it down here
Optional, but if you have an established gpg home dir that you want to use or you don't want it chosen for you
GPGME::Engine.home_dir = "/some/dir" # e.g. env['GNUPG_HOME']
Note If you already have keys imported in that home dir, you can skip this part:
Only need to do once for each key, but repeated imports are ok too.
A very basic explanation of keys in case you're not familiar:
### The keys are "saved" in your GPGME::Engine.home_dir which is why they don't really persist in ruby land
File.open("keyfile", 'rb') do |key_file|
GPGME::Key.import(
key_file,
{armor: true, keylist_mode: GPGME::KEYLIST_MODE_LOCAL}
)
end
It will inherit the keys assuming you don't change home_dir again) Note You can specify options now and/or when you call additional methods or both
additional ctx options available
crypto = GPGME::Crypto.new({always_trust: true})
Note Most of the GPGME methods return a GPGME::Data object which can be read just like a typical IO object, but remember to seek(0) to return to the beginning if nec.
cipher_text = crypto.encrypt(
"test text",
{recipients: 'email@address.com'}
# or recipients: ['email1@address.com', 'email2@address.com']
)
# recipients: could have been specified w/the crypto object or when calling encrypt
# same goes for always_trust:
clear_text = crypto.decrypt(
cipher_text,
{keylist_mode: GPGME::KEYLIST_MODE_LOCAL, password: 'blah'}
)
Note If you previously called cipher_text.read etc you'll need to first call cipher_text.seek 0
clear_text.read
clear_text.see(0) # optional