在Ruby gpgme中使用密码短语回调 [英] Using passphrase callback in ruby gpgme

查看:96
本文介绍了在Ruby gpgme中使用密码短语回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ruby gpgme gem(1.0.8).我的密码短语回调未调用:

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 "mypassphrase"
  io.flush
end

opts = {
  :passphrase_callback => method(:passfunc)
}
GPGME.decrypt(input,output, opts)

有人有密码短语回调的有效示例吗?

Does someone have working example of passphrase callback?

推荐答案

您可以在以下工作示例中找到回调的示例.它以分离模式对文件进行签名,即签名文件与原始文件分离.它使用默认的钥匙圈〜/.gnupg或类似的名称.要为密钥环使用其他目录,请在调用GPGME :: sign()之前设置环境变量ENV ["GNUPGHOME"] =".

Sample of callback you can find in the following working example. It signs a file in detached mode, i.e., the signature file is separated from the original file. It uses the default keyring at ~/.gnupg or something like that. To use a different directory for your keyring, set the environment variable ENV["GNUPGHOME"]="" before call GPGME::sign().

#!/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

这篇关于在Ruby gpgme中使用密码短语回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆