b1afe13c0a803da04729d7ce691570aa0bc86d0f
[platform/upstream/gpgme.git] / lang / python / examples / howto / sign-key.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from __future__ import absolute_import, division, unicode_literals
5
6 # Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
7 #
8 # This program is free software; you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # This program is free software; you can redistribute it and/or modify it under
14 # the terms of the GNU Lesser General Public License as published by the Free
15 # Software Foundation; either version 2.1 of the License, or (at your option)
16 # any later version.
17 #
18 # This program is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
21 # Lesser General Public Licensefor more details.
22 #
23 # You should have received a copy of the GNU General Public License and the GNU
24 # Lesser General Public along with this program; if not, see
25 # <http://www.gnu.org/licenses/>.
26
27 import gpg
28 import os.path
29
30 print("""
31 This script signs or certifies a key.
32
33 The gpg-agent and pinentry are invoked to enter the passphrase.
34 """)
35
36 c = gpg.Context()
37
38 homedir = input("Enter the GPG configuration directory path (optional): ")
39 fpr0 = input("Enter the fingerprint of the key to sign: ")
40 userid = input("Enter the UID to sign (case sensitive, optional): ")
41 sig_type = input("Enter the certification type (local or normal): ")
42
43 if homedir.startswith("~"):
44     if os.path.exists(os.path.expanduser(homedir)) is True:
45         c.home_dir = os.path.expanduser(homedir)
46     else:
47         pass
48 elif os.path.exists(homedir) is True:
49     c.home_dir = homedir
50 else:
51     pass
52
53 fpr = "".join(fpr0.split())
54 key = c.get_key(fpr, secret=False)
55
56 if len(userid) > 0 and sig_type.lower() == "local":
57     c.key_sign(key, uids=userid, local=True)
58 elif len(userid) > 0 and sig_type.lower() != "local":
59     c.key_sign(key, uids=userid)
60 elif len(userid) == 0 and sig_type.lower() == "local":
61     c.key_sign(key, local=True)
62 else:
63     c.key_sign(key)