【问题标题】:Using passphrase callback in ruby gpgme在 ruby​​ gpgme 中使用密码回调
【发布时间】:2009-12-08 15:36:34
【问题描述】:

我正在使用 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)

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

【问题讨论】:

    标签: ruby gnupg gpgme


    【解决方案1】:

    您可以在以下工作示例中找到回调示例。它以分离模式对文件进行签名,即签名文件与原始文件分离。它使用 ~/.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
    

    【讨论】:

      【解决方案2】:

      这是另一个不使用分离签名的工作示例。要对此进行测试,只需将 '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
      

      干杯!,

      -- 卡尔

      【讨论】:

        【解决方案3】:

        请务必注意,从 GnuPG 2.0 开始(以及在 1.4 中使用 use-agent 选项时)pinentry 用于密码收集。这意味着 gpgme 密码回调将 not be invoked。这在here 中有描述,使用示例可以在gpgme-tool example 中找到。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多