3

我正在使用 ruby​​ gpgme gem (1.0.8)。我的密码回调没有被调用:

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)

有人有密码回调的工作示例吗?

4

3 回答 3

3

您可以在以下工作示例中找到回调示例。它以分离模式对文件进行签名,即签名文件与原始文件分离。它使用 ~/.gnupg 或类似的默认密钥环。要为您的密钥环使用不同的目录,请在调用 GPGME::sign() 之前设置环境变量 ENV["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
于 2011-03-21T18:22:26.227 回答
3

这是另一个不使用分离签名的工作示例。要对此进行测试,只需将 'user@host.name' 更改为密钥的标识符并执行以下操作: GPG.decrypt(GPG.encrypt('some text', :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

干杯!,

——卡尔

于 2011-10-10T18:58:52.180 回答
2

需要注意的是,从 GnuPG 2.0 开始(以及在 1.4 中使用该use-agent选项时)pinentry用于密码收集。这意味着不会调用gpgme 密码回调。这在此处进行了描述,并且可以在示例中找到使用gpgme-tool 示例

于 2015-01-04T17:54:31.603 回答