本文整理汇总了Python中gnupg.GPG.verify方法的典型用法代码示例。如果您正苦于以下问题:Python GPG.verify方法的具体用法?Python GPG.verify怎么用?Python GPG.verify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gnupg.GPG
的用法示例。
在下文中一共展示了GPG.verify方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify_wordlist
# 需要导入模块: from gnupg import GPG [as 别名]
# 或者: from gnupg.GPG import verify [as 别名]
def verify_wordlist(wordlist):
JOECHRISELLIS_KEY_ID = "02C6372546042BC2"
try:
from gnupg import GPG
except ImportError:
print "python-gnupg is not installed."
print "Please verify the wordlist manually."
print "$ gpg --verify", wordlist.name
exit(1)
gpg = GPG()
verified = gpg.verify(wordlist.read())
if verified.key_id == JOECHRISELLIS_KEY_ID:
print "Correct signature from", verified.username
print "Signature OK"
elif verified.key_id:
print "Signature was verified, however, the signing key used " \
"is not owned by the author of this program."
print "The wordlist was signed by '%s'" % (verified.key_id)
print "Please continue with caution!"
else:
print "Invalid/no signature!"
示例2: GPGMail
# 需要导入模块: from gnupg import GPG [as 别名]
# 或者: from gnupg.GPG import verify [as 别名]
class GPGMail(object):
def __init__(self, gpg=None):
if gpg:
self.gpg = gpg
else:
self.gpg = GPG(gpgbinary="gpg2", use_agent=True)
GPGLogger.setLevel(logging.DEBUG)
self.logger = logging.getLogger('GPGMail')
def _armor(self, container, message, signature):
"""
Make the armor signed message
"""
if container.get_param('protocol') == 'application/pgp-signature':
m = re.match(r'^pgp-(.*)$', container.get_param('micalg'))
if m:
TEMPLATE = '-----BEGIN PGP SIGNED MESSAGE-----\n' \
'Hash: %s\n\n' \
'%s\n%s\n'
s = StringIO()
text = re.sub(r'(?m)^(-.*)$', r'- \1', self._flatten(message))
s.write(TEMPLATE % (m.group(1).upper(),
text,
signature.get_payload()))
return s.getvalue()
return None
def _filter_parts(sefl, m, f):
"""Iterate over messages that satisfy predicate."""
for x in m.walk():
if f(x):
yield x
def _flatten(self, message):
"""Return raw string representation of message."""
try:
s = StringIO()
g = Generator(s, mangle_from_=False, maxheaderlen=0)
g.flatten(message)
return s.getvalue()
finally:
s.close()
def _signed_parts(self, message):
"""Iterate over signed parts of message yielding
GPG verification status and signed contents."""
f = lambda m: \
m.is_multipart() and m.get_content_type() == 'multipart/signed' \
or not m.is_multipart() and m.get_content_maintype() == 'text'
for part in self._filter_parts(message, f):
if part.is_multipart():
try:
signed_part, signature = part.get_payload()
s = None
sign_type = signature.get_content_type()
if sign_type == 'application/pgp-signature':
s = self._armor(part, signed_part, signature)
yield self.gpg.verify(s), True, signature.get_filename()
except ValueError:
pass
else:
payload = part.get_payload(decode=True)
yield self.gpg.verify(payload), False, None
def verify(self, message):
"""Verify signature of a email message and returns the GPG info"""
result = {}
for verified, sign_attached, filename in self._signed_parts(message):
if verified is not None:
result = verified.__dict__
break
if 'status' in result and result['status'] is None:
return None
if 'gpg' in result:
del(result['gpg'])
if sign_attached:
result['filename'] = filename
return result
def _get_digest_algo(self, signature):
"""
Returns a string representation of the digest algo used in signature.
Raises a TypeError if signature.hash_algo does not exists.
Acceptable values for signature.hash_algo are:
MD5 1
SHA1 2
RMD160 3
SHA256 8
#.........这里部分代码省略.........