2009-12-08 5 views
3

ruby ​​gpgme gem (1.0.8)을 사용하고 있습니다. 패스 프레이즈 콜백이 호출되지 않습니다.ruby ​​gpgme의 암호문 콜백 사용

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

다음 작업 예제에서 확인할 수있는 콜백 샘플입니다. 분리 모드에서 파일에 서명합니다. 즉, 서명 파일이 원래 파일과 분리됩니다. ~/.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 
3

은 여기에 분리 된 서명을 사용하지 않는 당신을위한 다른 작업 예입니다. (GPG.encrypt ('텍스트'갑옷) => TRUE) GPG.decrypt이를 테스트하려면 키의 식별자에 '[email protected]'을 변경하고이 작업을 수행

require 'gpgme' 
require 'highline/import' 

module GPG 
    ENCRYPT_KEY = '[email protected]' 
    @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 

건배!

- 칼

2

은 GnuPG는 2.0으로 (그리고 use-agent 옵션을 사용 1.4) pinentry하는 것은 암호 수집을 위해 사용되는 것이 중요합니다. 즉, gpgme 암호 문구 콜백은 not be invoked입니다. 이것은 here으로 기술되어 있으며 사용 예는 gpgme-toolexample에서 확인할 수 있습니다.

+0

정말 고마워요. – msanteler