def GPGME.encrypt(recipients, plain, *args_options)
raise ArgumentError, 'wrong number of arguments' if args_options.length > 3
args, options = split_args(args_options)
cipher = args[0]
recipient_keys = recipients ? resolve_keys(recipients, false, [:encrypt]) : nil
check_version(options)
GPGME::Ctx.new(options) do |ctx|
plain_data = input_data(plain)
cipher_data = output_data(cipher)
begin
flags = 0
if options[:always_trust]
flags |= GPGME::ENCRYPT_ALWAYS_TRUST
end
if options[:sign]
if options[:signers]
ctx.add_signer(*resolve_keys(options[:signers], true, [:sign]))
end
ctx.encrypt_sign(recipient_keys, plain_data, cipher_data, flags)
else
ctx.encrypt(recipient_keys, plain_data, cipher_data, flags)
end
rescue GPGME::Error::UnusablePublicKey => exc
exc.keys = ctx.encrypt_result.invalid_recipients
raise exc
rescue GPGME::Error::UnusableSecretKey => exc
exc.keys = ctx.sign_result.invalid_signers
raise exc
end
unless cipher
cipher_data.seek(0, IO::SEEK_SET)
cipher_data.read
end
end
end