Blame lang/python/examples/howto/verify-signed-file.py

Packit Service 30b792
#!/usr/bin/env python3
Packit Service 30b792
# -*- coding: utf-8 -*-
Packit Service 30b792
Packit Service 30b792
from __future__ import absolute_import, division, unicode_literals
Packit Service 30b792
Packit Service 30b792
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU General Public License as published by the Free Software
Packit Service 30b792
# Foundation; either version 2 of the License, or (at your option) any later
Packit Service 30b792
# version.
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU Lesser General Public License as published by the Free
Packit Service 30b792
# Software Foundation; either version 2.1 of the License, or (at your option)
Packit Service 30b792
# any later version.
Packit Service 30b792
#
Packit Service 30b792
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 30b792
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Packit Service 30b792
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public License for more details.
Packit Service 30b792
#
Packit Service 30b792
# You should have received a copy of the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public along with this program; if not, see
Packit Service 30b792
# <https://www.gnu.org/licenses/>.
Packit Service 30b792
Packit Service 30b792
import gpg
Packit Service 30b792
import sys
Packit Service 30b792
import time
Packit Service 30b792
Packit Service 30b792
"""
Packit Service 30b792
Verifies a signed file which has been signed with either NORMAL or CLEAR modes.
Packit Service 30b792
"""
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) > 2:
Packit Service 30b792
    filename = " ".join(sys.argv[1:])
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    filename = sys.argv[1]
Packit Service 30b792
else:
Packit Service 30b792
    filename = input("Enter the path and filename to sign: ")
Packit Service 30b792
Packit Service 30b792
c = gpg.Context()
Packit Service 30b792
Packit Service 30b792
try:
Packit Service 30b792
    data, result = c.verify(open(filename))
Packit Service 30b792
    verified = True
Packit Service 30b792
except gpg.errors.BadSignatures as e:
Packit Service 30b792
    verified = False
Packit Service 30b792
    print(e)
Packit Service 30b792
Packit Service 30b792
if verified is True:
Packit Service 30b792
    for i in range(len(result.signatures)):
Packit Service 30b792
        sign = result.signatures[i]
Packit Service 30b792
        print("""Good signature from:
Packit Service 30b792
{0}
Packit Service 30b792
with key {1}
Packit Service 30b792
made at {2}
Packit Service 30b792
""".format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
Packit Service 30b792
           time.ctime(sign.timestamp)))
Packit Service 30b792
else:
Packit Service 30b792
    pass