Using passphrase callback in ruby gpgme

后端 未结 3 1314
梦毁少年i
梦毁少年i 2021-01-06 14:04

I am using ruby gpgme gem (1.0.8). My passphrase callback isn\'t called:

def passfunc(*args)
  fd = args.last
  io = IO.for_fd(fd, \'w\')
  io.puts \"mypassp         


        
3条回答
  •  遥遥无期
    2021-01-06 14:24

    Here's another working example for you that doesn't use a detached signature. To test this, simply change 'user@host.name' to the identifier of your key and do this: 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
    

    Cheers!,

    -- Carl

提交回复
热议问题