我不确定为什么它不起作用(Python 2.7.5)-在可以验证的temp目录中创建的文件,但是python无法验证。帮助吗?
我不确定我是否使用了gpg.verify_file函数错误,或者不确定我是否知道告诉python的正确方法我相信要导入的密钥,或者是什么。当我打开外壳并进入temp目录时,我没有遇到根据sig验证文件的问题,因此我知道它应该进行验证。
不幸的是,其中有99%的Python GnuPG示例都具有加密功能,而事实并非如此。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import gnupg
import tempfile
import shutil
from pprint import pprint
def getStartTlsPolicy():
policyURL = 'https://dl.eff.org/starttls-everywhere/policy.json'
policyASC = 'https://dl.eff.org/starttls-everywhere/policy.json.asc'
publicKEY = 'https://dl.eff.org/starttls-everywhere/public-key.txt'
try:
response = urllib2.urlopen(policyURL, timeout=5)
except:
return '{}'
try:
signature = urllib2.urlopen(policyASC, timeout=5)
except:
return '{}'
try:
keyblock = urllib2.urlopen(publicKEY, timeout=5)
except:
return '{}'
jsondata = response.read()
sigdata = signature.read()
keyfile = keyblock.read()
# GnuPG stuff here
dirpath = tempfile.mkdtemp()
gpg = gnupg.GPG(gnupghome=dirpath)
gpg.encoding = 'utf-8'
sigfile = open(dirpath + '/policy.json.asc', 'w+')
sigfile.write(sigdata)
jsonfile = open(dirpath + '/policy.json', 'w+')
jsonfile.write(jsondata)
jsonfile.close()
fingerlist = []
fingerlist.append('B693F33372E965D76D55368616EEA65D03326C9D')
gpg.import_keys(keyfile)
gpg.trust_keys(fingerlist, 'TRUST_FULLY')
verified = gpg.verify_file(sigfile, dirpath + '/policy.json', 'key_id=842AEA40C5BCD6E1')
if not verified:
print "signature verify failed"
public_keys = gpg.list_keys()
pprint(public_keys)
sigfile.close()
#shutil.rmtree(dirpath)
print dirpath
return '{}'
sigfile.close()
shutil.rmtree(dirpath)
return jsondata
jsondata = getStartTlsPolicy()
print jsondata
答案 0 :(得分:1)
最后弄清楚了。将签名写入文件还不够,您必须关闭然后以只读模式重新打开它。为什么我不知道,但是行得通。