user2012677
user2012677

Reputation: 5745

GPGME Passphrase prompt issue (Ruby)

In the example below, I have tried password and passphrase. Neither seem to allow me to run my code without a openpgp box prompting for the passphrase, the following message:

Pinentry Mac "Please enter the passphrase to unlock the secret key for the OpenPGP certificate"

What do I need to change to allow my code to run without the prompt? I know my password is correct in the code.

I have tried :

ctx = GPGME::Ctx.new :password=> 'password'

and this:

ctx = GPGME::Ctx.new :passphrase_callback => method(:passfunc)

But both do not seem to work. Any advice is appreciated.

  def self.passfunc(obj, uid_hint, passphrase_info, prev_was_bad, fd)
    io = IO.for_fd(fd, 'w')
    io.puts 'password'
    io.flush
  end

  def self.decrypt_file(local_file, decrypted_file = nil)
    # Set decrypted file path if one is not provided
    decrypted_file = local_file.chomp(File.extname(local_file)) + ".csv" if decrypted_file == nil
    encrypted_data = GPGME::Data.new(File.open(local_file))

    # Set the password and GPG Key to decryption
    ctx = GPGME::Ctx.new :password=> 'password'

    # I have tried the passphrase call back
    #ctx = GPGME::Ctx.new :passphrase_callback => method(:passfunc)

    #KEY= GPGME::Data.new(File.open("key.gpg"))
    ctx.import_keys Rebal::Config::KEY

    # Decrypt the data
    decrypted = ctx.decrypt encrypted_data
    decrypted.seek(0)

    #Write the data to a file
    File.write(decrypted_file, decrypted.read)

    #return path
    decrypted_file
  end

The following link did not seem to help... Using passphrase callback in ruby gpgme and I'm not sure what this link is referring to or how it addresses my specific issue, but it might be the soltuion... how to bypass pinentry (passphrase screen) while decrypting a file using gpgme If someone can explain to me what changes to my code would help, I'm listening.

SOLUTION: I found out that the pinentry prompt is because of GPG2 vs GPG1.4. I downgraded to GPG1.4 and now it seems to work.

If someone knows how to get GPG2 to work, please comment

thank you

Upvotes: 4

Views: 1870

Answers (1)

Morozov
Morozov

Reputation: 3019

Providing pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK option worked for me on GPG2:

GPGME::Ctx.new(
  pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK,
  passphrase_callback: method(:passfunc)
)

Upvotes: 4

Related Questions