Using passphrase callback in ruby ​​gpgme

I am using ruby ​​gpgme gem (1.0.8). My passphrase callback is not being called:

def passfunc(*args)
  fd = args.last
  io = IO.for_fd(fd, 'w')
  io.puts "mypassphrase"
  io.flush
end

opts = {
  :passphrase_callback => method(:passfunc)
}
GPGME.decrypt(input,output, opts)

Does anyone have a working example of a passphrase callback?

+3
source share
3 answers

The sample callback you can find in the following working example. It signs the file in a separate mode, i.e. The signature file is separate from the original file. It uses the default keyword in ~ / .gnupg or something like that. To use a different directory for your keyword, before calling GPGME :: sign () set the ENV environment variable ["GNUPGHOME"] = "".

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

puts "Signing #{ARGV[0]}" 
input = File.open(ARGV[0],'r')

PASSWD = "abc"

def passfunc(hook, uid_hint, passphrase_info, prev_was_bad, fd)
    puts("Passphrase for #{uid_hint}: ")
    io = IO.for_fd(fd, 'w')
    io.write(PASSWD+"\n")
    io.flush
end

output = File.open(ARGV[0]+'.asc','w')

sign = GPGME::sign(input, {
        :passphrase_callback => method(:passfunc), 
        :mode => GPGME::SIG_MODE_DETACH
    })
output.write(sign)
output.close
input.close
+3

, . , "user@host.name" : GPG.decrypt(GPG.encrypt( " ",: armor = > true))

require 'gpgme'
require 'highline/import'

module GPG
  ENCRYPT_KEY = 'user@host.com'
  @gpg = GPGME::Crypto.new

  class << self

    def decrypt(encrypted_data, options = {})
      options = { :passphrase_callback => self.method(:passfunc) }.merge(options)
      @gpg.decrypt(encrypted_data, options).read 
    end

    def encrypt(data_to_encrypt, options = {})
      options = { :passphrase_callback => self.method(:passfunc), :armor => true }.merge(options)
      @gpg.encrypt(data_to_encrypt, options).read
    end

    private
      def get_passphrase
        ask("Enter passphrase for #{ENCRYPT_KEY}: ") { |q| q.echo = '*' }
      end

      def passfunc(hook, uid_hint, passphrase_info, prev_was_bad, fd)
        begin
          system('stty -echo')
          io = IO.for_fd(fd, 'w')
          io.puts(get_passphrase)
          io.flush
        ensure
          (0 ... $_.length).each do |i| $_[i] = ?0 end if $_
          system('stty echo')
        end
        $stderr.puts
      end
  end
end

!

-

+3

It is important to note that with GnuPG 2.0 (and in 1.4, when the option is used use-agent) it is pinentryused to collect frames. This means that the gpgme passphrase callback will not be called . This is described here , and a usage example can be found in the gpgme-tool example .

+2
source

Source: https://habr.com/ru/post/1525239/


All Articles