diff --git a/lang/python/examples/howto/add-userid.py b/lang/python/examples/howto/add-userid.py index 85b90de5..945a8096 100755 --- a/lang/python/examples/howto/add-userid.py +++ b/lang/python/examples/howto/add-userid.py @@ -1,62 +1,62 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . import gpg import os.path print(""" This script adds a new user ID to an existing key. The gpg-agent and pinentry are invoked to enter the passphrase. """) c = gpg.Context() homedir = input("Enter the GPG configuration directory path (optional): ") fpr0 = input("Enter the fingerprint of the key to modify: ") uid_name = input("Enter the name of the user ID: ") uid_email = input("Enter the email address of the user ID: ") uid_cmnt = input("Enter a comment to include (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass fpr = "".join(fpr0.split()) -if len(uid_cmnt) > 0: +if uid_cmnt: userid = "{0} ({1}) <{2}>".format(uid_name, uid_cmnt, uid_email) else: userid = "{0} <{2}>".format(uid_name, uid_email) key = c.get_key(fpr, secret=True) c.key_add_uid(key, userid) diff --git a/lang/python/examples/howto/create-key.py b/lang/python/examples/howto/create-key.py index f7da2069..9058c194 100755 --- a/lang/python/examples/howto/create-key.py +++ b/lang/python/examples/howto/create-key.py @@ -1,95 +1,95 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . import gpg import os.path print(""" This script generates a new key which does not expire. The gpg-agent and pinentry are invoked to set the passphrase. """) c = gpg.Context() homedir = input("Enter the GPG configuration directory path (optional): ") uid_name = input("Enter the name of the user ID: ") uid_email = input("Enter the email address of the user ID: ") uid_cmnt = input("Enter a comment to include (optional): ") key_algo = input("Enter the key algorithm, RSA or DSA (default is RSA): ") key_size = input("Enter the key size (2048-4096, default is 2048): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass -if len(uid_cmnt) > 0: +if uid_cmnt: userid = "{0} ({1}) <{2}>".format(uid_name, uid_cmnt, uid_email) else: userid = "{0} <{2}>".format(uid_name, uid_email) if key_algo.lower() == "dsa": ka = "dsa" else: ka = "rsa" if len(key_size) == 4: try: ks0 = int(key_size) except ValueError: ks0 = None if ks0 is None: ks = "2048" else: if ks0 < 2048: ks = "2048" elif ka == "dsa" and ks0 > 3072: ks = "3072" elif ka == "rsa" and ks0 > 4096: ks = "4096" else: ks = key_size else: ks = "2048" keyalgo = "{0}{1}".format(ka, ks) newkey = c.create_key(userid, algorithm=keyalgo, expires=False, passphrase=True, certify=True) key = c.get_key(newkey.fpr, secret=True) if ka == "rsa": newsub = c.create_subkey(key, algorithm=keyalgo, expires=False, passphrase=True, encrypt=True) else: newsub = c.create_subkey(key, expires=False, passphrase=True, encrypt=True) diff --git a/lang/python/examples/howto/revoke-userid.py b/lang/python/examples/howto/revoke-userid.py index ac5f6495..da84ad17 100755 --- a/lang/python/examples/howto/revoke-userid.py +++ b/lang/python/examples/howto/revoke-userid.py @@ -1,62 +1,62 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . import gpg import os.path print(""" This script revokes a user ID on an existing key. The gpg-agent and pinentry are invoked to enter the passphrase. """) c = gpg.Context() homedir = input("Enter the GPG configuration directory path (optional): ") fpr0 = input("Enter the fingerprint of the key to modify: ") uid_name = input("Enter the name of the user ID: ") uid_email = input("Enter the email address of the user ID: ") uid_cmnt = input("Enter a comment to include (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass fpr = "".join(fpr0.split()) -if len(uid_cmnt) > 0: +if uid_cmnt: userid = "{0} ({1}) <{2}>".format(uid_name, uid_cmnt, uid_email) else: userid = "{0} <{2}>".format(uid_name, uid_email) key = c.get_key(fpr, secret=True) c.key_revoke_uid(key, userid) diff --git a/lang/python/examples/howto/send-key-to-keyserver.py b/lang/python/examples/howto/send-key-to-keyserver.py index d72ca9f1..cb17e804 100755 --- a/lang/python/examples/howto/send-key-to-keyserver.py +++ b/lang/python/examples/howto/send-key-to-keyserver.py @@ -1,98 +1,98 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals import gpg import hkp4py import os.path import sys # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . print(""" This script sends one or more public keys to the SKS keyservers and is essentially a slight variation on the export-key.py script. The default is to send all keys if there is no pattern or search term. """) c = gpg.Context(armor=True) server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net") if len(sys.argv) >= 3: logrus = sys.argv[1] homedir = sys.argv[2] elif len(sys.argv) == 2: logrus = sys.argv[1] homedir = input("Enter the GPG configuration directory path (optional): ") else: logrus = input("Enter the UID matching the key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") -if len(homedir) == 0: +if not homedir: homedir = None elif homedir.startswith("~"): userdir = os.path.expanduser(homedir) if os.path.exists(userdir) is True: homedir = os.path.realpath(userdir) else: homedir = None else: homedir = os.path.realpath(homedir) if homedir is not None and os.path.exists(homedir) is False: homedir = None elif homedir is not None and os.path.exists(homedir) is True: if os.path.isdir(homedir) is False: homedir = None else: pass if homedir is not None: c.home_dir = homedir else: pass -if len(logrus) > 0: +if logrus: try: export_result = c.key_export(pattern=logrus) except Exception as e: print(e) export_result = None else: export_result = c.key_export(pattern=None) if export_result is not None: try: try: send_result = server.add(export_result) except: send_result = server.add(export_result.decode()) if send_result is not None: print(send_result) else: pass except Exception as e: print(e) else: pass diff --git a/lang/python/examples/howto/sign-key.py b/lang/python/examples/howto/sign-key.py index 55d517b9..f9c90d76 100755 --- a/lang/python/examples/howto/sign-key.py +++ b/lang/python/examples/howto/sign-key.py @@ -1,63 +1,63 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . import gpg import os.path print(""" This script signs or certifies a key. The gpg-agent and pinentry are invoked to enter the passphrase. """) c = gpg.Context() homedir = input("Enter the GPG configuration directory path (optional): ") fpr0 = input("Enter the fingerprint of the key to sign: ") userid = input("Enter the UID to sign (case sensitive, optional): ") sig_type = input("Enter the certification type (local or normal): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass fpr = "".join(fpr0.split()) key = c.get_key(fpr, secret=False) -if len(userid) > 0 and sig_type.lower() == "local": +if userid and sig_type.lower() == "local": c.key_sign(key, uids=userid, local=True) -elif len(userid) > 0 and sig_type.lower() != "local": +elif userid and sig_type.lower() != "local": c.key_sign(key, uids=userid) -elif len(userid) == 0 and sig_type.lower() == "local": +elif not userid and sig_type.lower() == "local": c.key_sign(key, local=True) else: c.key_sign(key)