我使用的是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)有没有密码短语回调的工作示例?
发布于 2011-03-22 02:22:26
回调示例可以在以下工作示例中找到。它以分离模式对文件进行签名,即签名文件与原始文件分离。它使用~/.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发布于 2011-10-11 02:58:52
这是另一个不使用分离签名的工作示例。要测试这一点,只需将'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干杯!,
--卡尔
发布于 2015-01-05 01:54:32
需要注意的是,从GnuPG 2.0开始(在1.4中使用use-agent选项时),pinentry用于密码收集。这意味着gpgme密码短语回调将not be invoked。这里对此进行了描述,在gpgme-tool example中可以找到用法示例。
https://stackoverflow.com/questions/1867757
复制相似问题