Blame lang/python/examples/howto/sign-key.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 os.path
Packit Service 30b792
Packit Service 30b792
print("""
Packit Service 30b792
This script signs or certifies a key.
Packit Service 30b792
Packit Service 30b792
The gpg-agent and pinentry are invoked to enter the passphrase.
Packit Service 30b792
""")
Packit Service 30b792
Packit Service 30b792
c = gpg.Context()
Packit Service 30b792
Packit Service 30b792
homedir = input("Enter the GPG configuration directory path (optional): ")
Packit Service 30b792
fpr0 = input("Enter the fingerprint of the key to sign: ")
Packit Service 30b792
userid = input("Enter the UID to sign (case sensitive, optional): ")
Packit Service 30b792
sig_type = input("Enter the certification type (local or normal): ")
Packit Service 30b792
Packit Service 30b792
if homedir.startswith("~"):
Packit Service 30b792
    if os.path.exists(os.path.expanduser(homedir)) is True:
Packit Service 30b792
        c.home_dir = os.path.expanduser(homedir)
Packit Service 30b792
    else:
Packit Service 30b792
        pass
Packit Service 30b792
elif os.path.exists(homedir) is True:
Packit Service 30b792
    c.home_dir = homedir
Packit Service 30b792
else:
Packit Service 30b792
    pass
Packit Service 30b792
Packit Service 30b792
fpr = "".join(fpr0.split())
Packit Service 30b792
key = c.get_key(fpr, secret=False)
Packit Service 30b792
Packit Service 30b792
if userid and sig_type.lower() == "local":
Packit Service 30b792
    c.key_sign(key, uids=userid, local=True)
Packit Service 30b792
elif userid and sig_type.lower() != "local":
Packit Service 30b792
    c.key_sign(key, uids=userid)
Packit Service 30b792
elif not userid and sig_type.lower() == "local":
Packit Service 30b792
    c.key_sign(key, local=True)
Packit Service 30b792
else:
Packit Service 30b792
    c.key_sign(key)