代码之家  ›  专栏  ›  技术社区  ›  edbond

在RubyGPGME中使用密码回调

  •  3
  • edbond  · 技术社区  · 15 年前

    我使用的是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)
    

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

    3 回复  |  直到 10 年前
        1
  •  3
  •   Fernando G. Testa    14 年前

    在下面的工作示例中可以找到回调的示例。它以分离模式对文件进行签名,即签名文件与原始文件分离。它使用~/.gnupg或类似的默认键环。要为您的keyring使用不同的目录,请在调用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
  •  3
  •   rabbitt    13 年前

    这是另一个不使用分离签名的工作示例。要测试它,只需将'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
  •  2
  •   bgamari    10 年前

    重要的是要注意,从GNUPG 2.0开始(并且在1.4中,当 use-agent 使用选项 pinentry 用于密码短语集合。这意味着gpgme密码短语回调将 not be invoked . 这是描述 here 使用示例可以在 gpgme-tool example .