diff options
Diffstat (limited to 'lang/python/examples/howto')
41 files changed, 0 insertions, 3421 deletions
diff --git a/lang/python/examples/howto/README.org b/lang/python/examples/howto/README.org deleted file mode 100644 index b74ae7e2..00000000 --- a/lang/python/examples/howto/README.org +++ /dev/null @@ -1,58 +0,0 @@ -#+TITLE: GPGME Python Bindings HOWTO Examples -#+LATEX_COMPILER: xelatex -#+LATEX_CLASS: article -#+LATEX_CLASS_OPTIONS: [12pt] -#+LATEX_HEADER: \usepackage{xltxtra} -#+LATEX_HEADER: \usepackage[margin=1in]{geometry} -#+LATEX_HEADER: \setmainfont[Ligatures={Common}]{Times New Roman} -#+LATEX_HEADER: \author{Ben McGinnes <[email protected]>} - - -* Examples - :PROPERTIES: - :CUSTOM_ID: gpgme-python3-examples - :END: - - The contents of this directory are the examples included in the /GNU - Privacy Guard (GnuPG) Made Easy Python Bindings HOWTO/ file. Each - script is explicitly for Python 3 and specifically for Python 3.4 or - later. - - Some of these scripts may work with Python 2.7, but there are no - guarantees. They will include the relevant imports from the - =__future__= module to facilitate that if possible. - - -* Copyright and Licensing - :PROPERTIES: - :CUSTOM_ID: copyright-and-license - :END: - - Unless otherwise stated, all the examples in this directory are - released under the same terms as GPGME itself; that is they are dual - licensed under the terms of both the GNU General Public License - version 2.0 (or any later version) *and* the GNU Lesser General - Public License version 2.1 (or any later version). - - -** Copyright (C) The GnuPG Project, 2018 - :PROPERTIES: - :CUSTOM_ID: copyright - :END: - - Copyright © The GnuPG Project, 2018. - - -** License GPL compatible - :PROPERTIES: - :CUSTOM_ID: license - :END: - - This file is free software; as a special exception the author gives - unlimited permission to copy and/or distribute it, with or without - modifications, as long as this notice is preserved. - - This file is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY, to the extent permitted by law; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. diff --git a/lang/python/examples/howto/add-userid.py b/lang/python/examples/howto/add-userid.py deleted file mode 100755 index fce4acea..00000000 --- a/lang/python/examples/howto/add-userid.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -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 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/advanced/cython/keycount.pyx b/lang/python/examples/howto/advanced/cython/keycount.pyx deleted file mode 100755 index 2aa636d5..00000000 --- a/lang/python/examples/howto/advanced/cython/keycount.pyx +++ /dev/null @@ -1,26 +0,0 @@ -from __future__ import absolute_import - -import cython -import gpg - -c = gpg.Context() -seckeys = c.keylist(pattern=None, secret=True) -pubkeys = c.keylist(pattern=None, secret=False) - -seclist = list(seckeys) -secnum = len(seclist) - -publist = list(pubkeys) -pubnum = len(publist) - -if cython.compiled is True: - cc = "Powered by Cython compiled C code." -else: - cc = "Powered by Python." - -print(""" - Number of secret keys: {0} - Number of public keys: {1} - - {2} -""".format(secnum, pubnum, cc)) diff --git a/lang/python/examples/howto/advanced/cython/requirements.txt b/lang/python/examples/howto/advanced/cython/requirements.txt deleted file mode 100644 index f6629e02..00000000 --- a/lang/python/examples/howto/advanced/cython/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -cython diff --git a/lang/python/examples/howto/advanced/cython/setup.py b/lang/python/examples/howto/advanced/cython/setup.py deleted file mode 100644 index 849639e2..00000000 --- a/lang/python/examples/howto/advanced/cython/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -from setuptools import setup -from Cython.Build import cythonize - -setup( - ext_modules = cythonize("keycount.pyx", annotate=True) -) diff --git a/lang/python/examples/howto/clear-sign-file.py b/lang/python/examples/howto/clear-sign-file.py deleted file mode 100755 index 718ba026..00000000 --- a/lang/python/examples/howto/clear-sign-file.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys - -""" -Clear-signs a file with a specified key. If entering both the key and the -filename on the command line, the key must be entered first. -""" - -if len(sys.argv) > 3: - logrus = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - logrus = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - logrus = sys.argv[1] - filename = input("Enter the path and filename to sign: ") -else: - logrus = input("Enter the fingerprint or key ID to sign with: ") - filename = input("Enter the path and filename to sign: ") - -with open(filename, "rb") as f: - text = f.read() - -key = list(gpg.Context().keylist(pattern=logrus)) - -with gpg.Context(armor=True, signers=key) as c: - signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.CLEAR) - with open("{0}.asc".format(filename), "wb") as f: - f.write(signed_data) diff --git a/lang/python/examples/howto/create-key.py b/lang/python/examples/howto/create-key.py deleted file mode 100755 index fe1ea7b5..00000000 --- a/lang/python/examples/howto/create-key.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -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 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/decrypt-file.py b/lang/python/examples/howto/decrypt-file.py deleted file mode 100755 index a9c6cb76..00000000 --- a/lang/python/examples/howto/decrypt-file.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys - -if len(sys.argv) == 3: - ciphertext = sys.argv[1] - newfile = sys.argv[2] -elif len(sys.argv) == 2: - ciphertext = sys.argv[1] - newfile = input("Enter path and filename to save decrypted data to: ") -else: - ciphertext = input("Enter path and filename of encrypted file: ") - newfile = input("Enter path and filename to save decrypted data to: ") - -with open(ciphertext, "rb") as cfile: - try: - plaintext, result, verify_result = gpg.Context().decrypt(cfile) - except gpg.errors.GPGMEError as e: - plaintext = None - print(e) - -if plaintext is not None: - with open(newfile, "wb") as nfile: - nfile.write(plaintext) -else: - pass diff --git a/lang/python/examples/howto/detach-sign-file.py b/lang/python/examples/howto/detach-sign-file.py deleted file mode 100755 index b9896ad1..00000000 --- a/lang/python/examples/howto/detach-sign-file.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys - -""" -Signs a file with a specified key. If entering both the key and the filename -on the command line, the key must be entered first. - -Will produce both an ASCII armoured and GPG binary format copy of the detached -signature file. -""" - -if len(sys.argv) > 3: - logrus = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - logrus = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - logrus = sys.argv[1] - filename = input("Enter the path and filename to sign: ") -else: - logrus = input("Enter the fingerprint or key ID to sign with: ") - filename = input("Enter the path and filename to sign: ") - -with open(filename, "rb") as f: - text = f.read() - -key = list(gpg.Context().keylist(pattern=logrus)) - -with gpg.Context(armor=True, signers=key) as ca: - signed_data, result = ca.sign(text, mode=gpg.constants.sig.mode.DETACH) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(signed_data) - -with gpg.Context(signers=key) as cb: - signed_data, result = cb.sign(text, mode=gpg.constants.sig.mode.DETACH) - with open("{0}.sig".format(filename), "wb") as fb: - fb.write(signed_data) diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py deleted file mode 100755 index 1e0c4712..00000000 --- a/lang/python/examples/howto/encrypt-file.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Encrypts a file to a specified key. If entering both the key and the filename -on the command line, the key must be entered first. - -Will produce both an ASCII armoured and GPG binary format copy of the encrypted -file. -""" - -if len(sys.argv) > 3: - a_key = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - a_key = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - a_key = sys.argv[1] - filename = input("Enter the path and filename to encrypt: ") -else: - a_key = input("Enter the fingerprint or key ID to encrypt to: ") - filename = input("Enter the path and filename to encrypt: ") - -rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) -with open(filename, "rb") as f: - text = f.read() - -with gpg.Context(armor=True) as ca: - try: - ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, - sign=False) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(ciphertext) - except gpg.errors.InvalidRecipients as e: - print(e) - -with gpg.Context() as cg: - try: - ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, - sign=False) - with open("{0}.gpg".format(filename), "wb") as fg: - fg.write(ciphertext) - except gpg.errors.InvalidRecipients as e: - print(e) diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py deleted file mode 100755 index f63644c3..00000000 --- a/lang/python/examples/howto/encrypt-sign-file.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Signs and encrypts a file to a specified key. If entering both the key and the -filename on the command line, the key must be entered first. - -Signs with and also encrypts to the default key of the user invoking the -script. Will treat all recipients as trusted to permit encryption. - -Will produce both an ASCII armoured and GPG binary format copy of the signed -and encrypted file. -""" - -if len(sys.argv) > 3: - a_key = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - a_key = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - a_key = sys.argv[1] - filename = input("Enter the path and filename to encrypt: ") -else: - a_key = input("Enter the fingerprint or key ID to encrypt to: ") - filename = input("Enter the path and filename to encrypt: ") - -rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) -with open(filename, "rb") as f: - text = f.read() - -with gpg.Context(armor=True) as ca: - ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, - always_trust=True, - add_encrypt_to=True) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(ciphertext) - -with gpg.Context() as cg: - ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, - always_trust=True, - add_encrypt_to=True) - with open("{0}.gpg".format(filename), "wb") as fg: - fg.write(ciphertext) diff --git a/lang/python/examples/howto/encrypt-to-group-gullible.py b/lang/python/examples/howto/encrypt-to-group-gullible.py deleted file mode 100755 index d954ed3f..00000000 --- a/lang/python/examples/howto/encrypt-to-group-gullible.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys -from groups import group_lists - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Uses the groups module to encrypt to multiple recipients. - -""" - -c = gpg.Context(armor=True) - -if len(sys.argv) > 3: - group_id = sys.argv[1] - filepath = sys.argv[2:] -elif len(sys.argv) == 3: - group_id = sys.argv[1] - filepath = sys.argv[2] -elif len(sys.argv) == 2: - group_id = sys.argv[1] - filepath = input("Enter the filename to encrypt: ") -else: - group_id = input("Enter the group name to encrypt to: ") - filepath = input("Enter the filename to encrypt: ") - -with open(filepath, "rb") as f: - text = f.read() - -for i in range(len(group_lists)): - if group_lists[i][0] == group_id: - klist = group_lists[i][1] - else: - klist = None - -logrus = [] - -if klist is not None: - for i in range(len(klist)): - apattern = list(c.keylist(pattern=klist[i], secret=False)) - if apattern[0].can_encrypt == 1: - logrus.append(apattern[0]) - else: - pass - try: - ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, - add_encrypt_to=True) - except: - ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, - add_encrypt_to=True, - always_trust=True) - with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) -else: - pass - -# EOF diff --git a/lang/python/examples/howto/encrypt-to-group-trustno1.py b/lang/python/examples/howto/encrypt-to-group-trustno1.py deleted file mode 100755 index 758e8de6..00000000 --- a/lang/python/examples/howto/encrypt-to-group-trustno1.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys -from groups import group_lists - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Uses the groups module to encrypt to multiple recipients. - -""" - -c = gpg.Context(armor=True) - -if len(sys.argv) > 3: - group_id = sys.argv[1] - filepath = sys.argv[2:] -elif len(sys.argv) == 3: - group_id = sys.argv[1] - filepath = sys.argv[2] -elif len(sys.argv) == 2: - group_id = sys.argv[1] - filepath = input("Enter the filename to encrypt: ") -else: - group_id = input("Enter the group name to encrypt to: ") - filepath = input("Enter the filename to encrypt: ") - -with open(filepath, "rb") as f: - text = f.read() - -for i in range(len(group_lists)): - if group_lists[i][0] == group_id: - klist = group_lists[i][1] - else: - klist = None - -logrus = [] - -if klist is not None: - for i in range(len(klist)): - apattern = list(c.keylist(pattern=klist[i], secret=False)) - if apattern[0].can_encrypt == 1: - logrus.append(apattern[0]) - else: - pass - try: - ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, - add_encrypt_to=True) - except gpg.errors.InvalidRecipients as e: - for i in range(len(e.recipients)): - for n in range(len(logrus)): - if logrus[n].fpr == e.recipients[i].fpr: - logrus.remove(logrus[n]) - else: - pass - try: - ciphertext, result, sign_result = c.encrypt(text, - recipients=logrus, - add_encrypt_to=True) - except: - pass - with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) -else: - pass - -# EOF diff --git a/lang/python/examples/howto/encrypt-to-group.py b/lang/python/examples/howto/encrypt-to-group.py deleted file mode 100755 index 514d6e0c..00000000 --- a/lang/python/examples/howto/encrypt-to-group.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys -from groups import group_lists - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Uses the groups module to encrypt to multiple recipients. - -""" - -c = gpg.Context(armor=True) - -if len(sys.argv) > 3: - group_id = sys.argv[1] - filepath = sys.argv[2:] -elif len(sys.argv) == 3: - group_id = sys.argv[1] - filepath = sys.argv[2] -elif len(sys.argv) == 2: - group_id = sys.argv[1] - filepath = input("Enter the filename to encrypt: ") -else: - group_id = input("Enter the group name to encrypt to: ") - filepath = input("Enter the filename to encrypt: ") - -with open(filepath, "rb") as f: - text = f.read() - -for i in range(len(group_lists)): - if group_lists[i][0] == group_id: - klist = group_lists[i][1] - else: - klist = None - -logrus = [] - -if klist is not None: - for i in range(len(klist)): - apattern = list(c.keylist(pattern=klist[i], secret=False)) - if apattern[0].can_encrypt == 1: - logrus.append(apattern[0]) - else: - pass - try: - ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, - add_encrypt_to=True) - except gpg.errors.InvalidRecipients as e: - for i in range(len(e.recipients)): - for n in range(len(logrus)): - if logrus[n].fpr == e.recipients[i].fpr: - logrus.remove(logrus[n]) - else: - pass - try: - ciphertext, result, sign_result = c.encrypt(text, - recipients=logrus, - add_encrypt_to=True, - always_trust=True) - except: - pass - with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) -else: - pass - -# EOF diff --git a/lang/python/examples/howto/export-key.py b/lang/python/examples/howto/export-key.py deleted file mode 100755 index 18718d94..00000000 --- a/lang/python/examples/howto/export-key.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script exports one or more public keys. -""") - -c = gpg.Context(armor=True) - -if len(sys.argv) >= 4: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = sys.argv[3] -elif len(sys.argv) == 3: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = input("Enter the GPG configuration directory path (optional): ") -elif len(sys.argv) == 2: - keyfile = sys.argv[1] - logrus = input("Enter the UID matching the key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyfile = input("Enter the path and filename to save the key(s) to: ") - logrus = input("Enter the UID matching the key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") - -if len(homedir) == 0: - 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 - -try: - result = c.key_export(pattern=logrus) -except: - result = c.key_export(pattern=None) - -if result is not None: - with open(keyfile, "wb") as f: - f.write(result) -else: - pass diff --git a/lang/python/examples/howto/export-minimised-key.py b/lang/python/examples/howto/export-minimised-key.py deleted file mode 100755 index 3bf10963..00000000 --- a/lang/python/examples/howto/export-minimised-key.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script exports one or more public keys in minimised form. -""") - -c = gpg.Context(armor=True) - -if len(sys.argv) >= 4: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = sys.argv[3] -elif len(sys.argv) == 3: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = input("Enter the GPG configuration directory path (optional): ") -elif len(sys.argv) == 2: - keyfile = sys.argv[1] - logrus = input("Enter the UID matching the key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyfile = input("Enter the path and filename to save the key(s) to: ") - logrus = input("Enter the UID matching the key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") - -if len(homedir) == 0: - 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 - -try: - result = c.key_export_minimal(pattern=logrus) -except: - result = c.key_export_minimal(pattern=None) - -if result is not None: - with open(keyfile, "wb") as f: - f.write(result) -else: - pass diff --git a/lang/python/examples/howto/export-secret-key.py b/lang/python/examples/howto/export-secret-key.py deleted file mode 100755 index f04f716c..00000000 --- a/lang/python/examples/howto/export-secret-key.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os -import os.path -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script exports one or more secret keys. - -The gpg-agent and pinentry are invoked to authorise the export. -""") - -def open_0o600(path, flags): - return os.open(path, flags, mode=0o600) - -c = gpg.Context(armor=True) - -if len(sys.argv) >= 4: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = sys.argv[3] -elif len(sys.argv) == 3: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = input("Enter the GPG configuration directory path (optional): ") -elif len(sys.argv) == 2: - keyfile = sys.argv[1] - logrus = input("Enter the UID matching the secret key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyfile = input("Enter the path and filename to save the secret key to: ") - logrus = input("Enter the UID matching the secret key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") - -if len(homedir) == 0: - 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 - -try: - result = c.key_export_secret(pattern=logrus) -except: - result = c.key_export_secret(pattern=None) - -if result is not None: - with open(keyfile, "wb", opener=open_0o600) as f: - f.write(result) -else: - pass diff --git a/lang/python/examples/howto/export-secret-keys.py b/lang/python/examples/howto/export-secret-keys.py deleted file mode 100755 index 08b165ed..00000000 --- a/lang/python/examples/howto/export-secret-keys.py +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os -import os.path -import subprocess -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script exports one or more secret keys as both ASCII armored and binary -file formats, saved in files within the user's GPG home directory. - -The gpg-agent and pinentry are invoked to authorise the export. -""") - -def open_0o600(path, flags): - return os.open(path, flags, mode=0o600) - -if sys.platform == "win32": - gpgconfcmd = "gpgconf.exe --list-dirs homedir" -else: - gpgconfcmd = "gpgconf --list-dirs homedir" - -a = gpg.Context(armor=True) -b = gpg.Context() -c = gpg.Context() - -if len(sys.argv) >= 4: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = sys.argv[3] -elif len(sys.argv) == 3: - keyfile = sys.argv[1] - logrus = sys.argv[2] - homedir = input("Enter the GPG configuration directory path (optional): ") -elif len(sys.argv) == 2: - keyfile = sys.argv[1] - logrus = input("Enter the UID matching the secret key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyfile = input("Enter the filename to save the secret key to: ") - logrus = input("Enter the UID matching the secret key(s) to export: ") - homedir = input("Enter the GPG configuration directory path (optional): ") - -if len(homedir) == 0: - 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 c.home_dir is not None: - if c.home_dir.endswith("/"): - gpgfile = "{0}{1}.gpg".format(c.home_dir, keyfile) - ascfile = "{0}{1}.asc".format(c.home_dir, keyfile) - else: - gpgfile = "{0}/{1}.gpg".format(c.home_dir, keyfile) - ascfile = "{0}/{1}.asc".format(c.home_dir, keyfile) -else: - if os.path.exists(os.environ["GNUPGHOME"]) is True: - hd = os.environ["GNUPGHOME"] - else: - try: - hd = subprocess.getoutput(gpgconfcmd) - except: - process = subprocess.Popen(gpgconfcmd.split(), - stdout=subprocess.PIPE) - procom = process.communicate() - if sys.version_info[0] == 2: - hd = procom[0].strip() - else: - hd = procom[0].decode().strip() - gpgfile = "{0}/{1}.gpg".format(hd, keyfile) - ascfile = "{0}/{1}.asc".format(hd, keyfile) - -try: - a_result = a.key_export_secret(pattern=logrus) - b_result = b.key_export_secret(pattern=logrus) -except: - a_result = a.key_export_secret(pattern=None) - b_result = b.key_export_secret(pattern=None) - -if a_result is not None: - with open(ascfile, "wb", opener=open_0o600) as f: - f.write(a_result) -else: - pass - -if b_result is not None: - with open(gpgfile, "wb", opener=open_0o600) as f: - f.write(b_result) -else: - pass diff --git a/lang/python/examples/howto/groups.py b/lang/python/examples/howto/groups.py deleted file mode 100644 index 17b7ab2f..00000000 --- a/lang/python/examples/howto/groups.py +++ /dev/null @@ -1,62 +0,0 @@ -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import subprocess -import sys - -""" -Intended for use with other scripts. - -Usage: from groups import group_lists -""" - -if sys.platform == "win32": - gpgconfcmd = "gpgconf.exe --list-options gpg" -else: - gpgconfcmd = "gpgconf --list-options gpg" - -process = subprocess.Popen(gpgconfcmd.split(), stdout=subprocess.PIPE) -procom = process.communicate() - -if sys.version_info[0] == 2: - lines = procom[0].splitlines() -else: - lines = procom[0].decode().splitlines() - -for line in lines: - if line.startswith("group") is True: - break - -groups = line.split(":")[-1].replace('"', '').split(',') - -group_lines = [] -group_lists = [] - -for group in groups: - group_lines.append(group.split("=")) - group_lists.append(group.split("=")) - -for glist in group_lists: - glist[1] = glist[1].split() diff --git a/lang/python/examples/howto/import-key.py b/lang/python/examples/howto/import-key.py deleted file mode 100755 index c1cb6c11..00000000 --- a/lang/python/examples/howto/import-key.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import sys - -del absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script imports one or more public keys from a single file. -""") - -c = gpg.Context(armor=True) - -if len(sys.argv) >= 3: - keyfile = sys.argv[1] - homedir = sys.argv[2] -elif len(sys.argv) == 2: - keyfile = sys.argv[1] - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyfile = input("Enter the path and filename to import the key(s) from: ") - homedir = input("Enter the GPG configuration directory path (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 - -if os.path.isfile(keyfile) is True: - with open(keyfile, "rb") as f: - incoming = f.read() - result = c.key_import(incoming) -else: - result = None - -if result is not None and hasattr(result, "considered") is False: - print(result) -elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - print(""" -The total number of keys considered for import was: {0} - - Number of keys revoked: {1} - Number of new signatures: {2} - Number of new subkeys: {3} - Number of new user IDs: {4} -Number of new secret keys: {5} - Number of unchanged keys: {6} - -The key IDs for all considered keys were: -""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") -elif result is None: - print("You must specify a key file to import.") diff --git a/lang/python/examples/howto/import-keybasekey.py b/lang/python/examples/howto/import-keybasekey.py deleted file mode 100755 index d6a42e9c..00000000 --- a/lang/python/examples/howto/import-keybasekey.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import requests -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script imports one or more public keys from keybase.io with the username -of a keybase.io user. - -Multiple usernames may be provided, separated by spaces. -""") - -c = gpg.Context() -# https://keybase.io/<user>/pgp_keys.asc -results = [] - -if len(sys.argv) >= 2: - keys = sys.argv[1:] -else: - keys = input("Enter the username(s) to get keys for: ") - -for key in keys: - r = requests.get("https://keybase.io/{0}/pgp_keys.asc".format(key)) - import_result = c.key_import(r.content) - results.append(import_result) - -for result in results: - if result is not None and hasattr(result, "considered") is False: - print(result) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - print(""" -The total number of keys considered for import was: {0} - - Number of keys revoked: {1} - Number of new signatures: {2} - Number of new subkeys: {3} - Number of new user IDs: {4} -Number of new secret keys: {5} - Number of unchanged keys: {6} - -The key IDs for all considered keys were: -""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - else: - print("No keys were imported or found.") diff --git a/lang/python/examples/howto/import-keys-hkp.py b/lang/python/examples/howto/import-keys-hkp.py deleted file mode 100755 index f0d79915..00000000 --- a/lang/python/examples/howto/import-keys-hkp.py +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import hkp4py -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script imports one or more public keys from the SKS keyservers. -""") - -c = gpg.Context() -server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net") -results = [] -keys = [] - -if len(sys.argv) > 2: - pattern = " ".join(sys.argv[1:]) -elif len(sys.argv) == 2: - pattern = sys.argv[1] -else: - pattern = input("Enter the pattern to search for keys or user IDs: ") - - -if pattern is not None: - try: - key = server.search(hex(int(pattern, 16))) - keyed = True - except ValueError as ve: - key = server.search(pattern) - keyed = False - - if key is not None: - keys.append(key[0]) - if keyed is True: - try: - fob = server.search(pattern) - except Exception as e: - fob = None - if fob is not None: - keys.append(fob[0]) - else: - pass - else: - pass - - for logrus in pattern.split(): - if logrus != pattern: - try: - key = server.search(hex(int(logrus, 16))) - hexed = True - except ValueError as ve: - key = server.search(logrus) - hexed = False - - if key is not None: - keys.append(key[0]) - if hexed is True: - try: - fob = server.search(logrus) - except Exception as e: - fob = None - if fob is not None: - keys.append(fob[0]) - else: - pass - else: - pass - else: - pass - else: - pass - - -if len(keys) > 0: - for key in keys: - import_result = c.key_import(key.key_blob) - results.append(import_result) - -for result in results: - if result is not None and hasattr(result, "considered") is False: - print(result) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - print(""" -The total number of keys considered for import was: {0} - - Number of keys revoked: {1} - Number of new signatures: {2} - Number of new subkeys: {3} - Number of new user IDs: {4} -Number of new secret keys: {5} - Number of unchanged keys: {6} - -The key IDs for all considered keys were: -""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - else: - print("No keys were imported or found.") diff --git a/lang/python/examples/howto/import-keys.py b/lang/python/examples/howto/import-keys.py deleted file mode 100755 index 798a4fcc..00000000 --- a/lang/python/examples/howto/import-keys.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import requests - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script imports one or more public keys from the SKS keyservers. -""") - -c = gpg.Context() -url = "https://sks-keyservers.net/pks/lookup" -pattern = input("Enter the pattern to search for key or user IDs: ") -payload = {"op": "get", "search": pattern} - -r = requests.get(url, verify=True, params=payload) -result = c.key_import(r.content) - -if result is not None and hasattr(result, "considered") is False: - print(result) -elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - print(""" -The total number of keys considered for import was: {0} - - Number of keys revoked: {1} - Number of new signatures: {2} - Number of new subkeys: {3} - Number of new user IDs: {4} -Number of new secret keys: {5} - Number of unchanged keys: {6} - -The key IDs for all considered keys were: -""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") -else: - pass diff --git a/lang/python/examples/howto/import-mailvelope-keys.py b/lang/python/examples/howto/import-mailvelope-keys.py deleted file mode 100755 index 9d57ff81..00000000 --- a/lang/python/examples/howto/import-mailvelope-keys.py +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import hkp4py -import sys - -# Copyright (C) 2019 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script imports one or more public keys from the Mailvelope keyserver. -""") - -c = gpg.Context() -server = hkp4py.KeyServer("hkps://keys.mailvelope.com") -results = [] -keys = [] - -if len(sys.argv) > 2: - pattern = " ".join(sys.argv[1:]) -elif len(sys.argv) == 2: - pattern = sys.argv[1] -else: - pattern = input("Enter the pattern to search for keys or user IDs: ") - - -if pattern is not None: - try: - key = server.search(hex(int(pattern, 16))) - keyed = True - except ValueError as ve: - key = server.search(pattern) - keyed = False - - if key is not None: - keys.append(key[0]) - if keyed is True: - try: - fob = server.search(pattern) - except Exception as e: - fob = None - if fob is not None: - keys.append(fob[0]) - else: - pass - else: - pass - - for logrus in pattern.split(): - if logrus != pattern: - try: - key = server.search(hex(int(logrus, 16))) - hexed = True - except ValueError as ve: - key = server.search(logrus) - hexed = False - - if key is not None: - keys.append(key[0]) - if hexed is True: - try: - fob = server.search(logrus) - except Exception as e: - fob = None - if fob is not None: - keys.append(fob[0]) - else: - pass - else: - pass - else: - pass - else: - pass - - -if len(keys) > 0: - for key in keys: - import_result = c.key_import(key.key_blob) - results.append(import_result) - -for result in results: - if result is not None and hasattr(result, "considered") is False: - print(result) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - print(""" -The total number of keys considered for import was: {0} - - Number of keys revoked: {1} - Number of new signatures: {2} - Number of new subkeys: {3} - Number of new user IDs: {4} -Number of new secret keys: {5} - Number of unchanged keys: {6} - -The key IDs for all considered keys were: -""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - else: - print("No keys were imported or found.") diff --git a/lang/python/examples/howto/keycount.py b/lang/python/examples/howto/keycount.py deleted file mode 100755 index 4599d485..00000000 --- a/lang/python/examples/howto/keycount.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg - -c = gpg.Context() -seckeys = c.keylist(pattern=None, secret=True) -pubkeys = c.keylist(pattern=None, secret=False) - -seclist = list(seckeys) -secnum = len(seclist) - -publist = list(pubkeys) -pubnum = len(publist) - -print(""" -Number of secret keys: {0} -Number of public keys: {1} -""".format(secnum, pubnum)) diff --git a/lang/python/examples/howto/local-sign-group.py b/lang/python/examples/howto/local-sign-group.py deleted file mode 100755 index 321330f5..00000000 --- a/lang/python/examples/howto/local-sign-group.py +++ /dev/null @@ -1,183 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import subprocess -import sys - -from groups import group_lists - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script applies a local signature or certification to every key in a group. - -Usage: local-sign-group.py <group name> [signing keyid] [gnupg homedir] -""") - -c = gpg.Context(armor=True) -mkfpr = None -defkey_fpr = None -enckey_fpr = None -to_certify = [] - -if len(sys.argv) >= 4: - clique = sys.argv[1] - sigkey = sys.argv[2] - homedir = sys.argv[3] -elif len(sys.argv) == 3: - clique = sys.argv[1] - sigkey = sys.argv[2] - homedir = input("Enter the GPG configuration directory path (optional): ") -elif len(sys.argv) == 2: - clique = sys.argv[1] - sigkey = input("Enter the key ID to sign with (conditionally optional): ") - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - clique = input("Enter the group matching the key(s) to locally sign: ") - sigkey = input("Enter the key ID to sign with (conditionally optional): ") - homedir = input("Enter the GPG configuration directory path (optional): ") - -if len(homedir) == 0: - 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(sigkey) == 0: - sigkey = None -else: - pass - -if sys.platform == "win32": - gpgconfcmd = "gpgconf.exe --list-options gpg" -else: - gpgconfcmd = "gpgconf --list-options gpg" - -try: - lines = subprocess.getoutput(gpgconfcmd).splitlines() -except: - process = subprocess.Popen(gpgconfcmd.split(), stdout=subprocess.PIPE) - procom = process.communicate() - if sys.version_info[0] == 2: - lines = procom[0].splitlines() - else: - lines = procom[0].decode().splitlines() - -for i in range(len(lines)): - if lines[i].startswith("default-key") is True: - dline = lines[i] - elif lines[i].startswith("encrypt-to") is True: - eline = lines[i] - else: - pass - -defkey_fpr = dline.split(":")[-1].replace('"', '').split(',')[0].upper() -enckey_fpr = eline.split(":")[-1].replace('"', '').split(',')[0].upper() - -try: - dkey = c.keylist(pattern=defkey_fpr, secret=True) - dk = list(dkey) -except Exception as de: - print(de) - dk = None - print("No valid default key.") - -try: - ekey = c.keylist(pattern=defkey_fpr, secret=True) - ek = list(ekey) -except Exception as ee: - print(ee) - ek = None - print("No valid always encrypt to key.") - -if sigkey is not None: - mykey = c.keylist(pattern=sigkey, secret=True) - mk = list(mykey) - mkfpr = mk[0].fpr.upper() - c.signers = mk -else: - if dk is None and ek is not None: - c.signers = ek - else: - pass - -for group in group_lists: - if group[0] == clique: - for logrus in group[1]: - khole = c.keylist(pattern=logrus) - k = list(khole) - to_certify.append(k[0].fpr.upper()) - else: - pass - -if mkfpr is not None: - if to_certify.count(mkfpr) > 0: - for n in range(to_certify.count(mkfpr)): - to_certify.remove(mkfpr) - else: - pass -else: - pass - -if defkey_fpr is not None: - if to_certify.count(defkey_fpr) > 0: - for n in range(to_certify.count(defkey_fpr)): - to_certify.remove(defkey_fpr) - else: - pass -else: - pass - -if enckey_fpr is not None: - if to_certify.count(enckey_fpr) > 0: - for n in range(to_certify.count(enckey_fpr)): - to_certify.remove(enckey_fpr) - else: - pass -else: - pass - -for fpr in to_certify: - key = c.get_key(fpr) - c.key_sign(key, uids=None, expires_in=False, local=True) diff --git a/lang/python/examples/howto/mutt-groups.py b/lang/python/examples/howto/mutt-groups.py deleted file mode 100755 index ed99eb26..00000000 --- a/lang/python/examples/howto/mutt-groups.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import sys -from groups import group_lists - -""" -Uses the groups module to generate Mutt crypt-hooks from gpg.conf. - -""" - -if len(sys.argv) >= 2: - hook_file = sys.argv[1] -else: - hook_file = input("Enter the filename to save the crypt-hooks in: ") - -with open(hook_file, "w") as f: - f.write("""# Change settings based upon message recipient -# -# send-hook [!]<pattern> <command> -# -# <command> is executed when sending mail to an address matching <pattern> -# -# crypt-hook regexp key-id -# The crypt-hook command provides a method by which you can -# specify the ID of the public key to be used when encrypting -# messages to a certain recipient. The meaning of "key ID" is to -# be taken broadly: This can be a different e-mail address, a -# numerical key ID, or even just an arbitrary search string. You -# may use multiple crypt-hooks with the same regexp; multiple -# matching crypt-hooks result in the use of multiple key-ids for a -# recipient. -""") - -for n in range(len(group_lists)): - rule = group_lists[n][0].replace(".", "\\\\.") - with open(hook_file, "a") as f: - f.write("\n") - f.write("# {0}\n".format(group_lists[n][0])) - for i in range(len(group_lists[n][1])): - f.write("crypt-hook {0} {1}\n".format(rule, group_lists[n][1][i])) diff --git a/lang/python/examples/howto/pmkey-import-alt.py b/lang/python/examples/howto/pmkey-import-alt.py deleted file mode 100755 index 2212ea12..00000000 --- a/lang/python/examples/howto/pmkey-import-alt.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import os.path -import requests -import sys - -del absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script searches the ProtonMail key server for the specified key and -imports it. Optionally enables specifying a different GnuPG home directory. - -Usage: pmkey-import-alt.py [search string] [homedir] -""") - -c = gpg.Context(armor=True) -url = "https://api.protonmail.ch/pks/lookup" -ksearch = [] - -if len(sys.argv) >= 3: - keyterm = sys.argv[1] - homedir = sys.argv[2] -elif len(sys.argv) == 2: - keyterm = sys.argv[1] - homedir = input("Enter the GPG configuration directory path (optional): ") -else: - keyterm = input("Enter the key ID, UID or search string: ") - homedir = input("Enter the GPG configuration directory path (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 - -if keyterm.count("@") == 2 and keyterm.startswith("@") is True: - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) -elif keyterm.count("@") == 1 and keyterm.startswith("@") is True: - ksearch.append("{0}@protonmail.com".format(keyterm[1:])) - ksearch.append("{0}@protonmail.ch".format(keyterm[1:])) - ksearch.append("{0}@pm.me".format(keyterm[1:])) -elif keyterm.count("@") == 0: - ksearch.append("{0}@protonmail.com".format(keyterm)) - ksearch.append("{0}@protonmail.ch".format(keyterm)) - ksearch.append("{0}@pm.me".format(keyterm)) -elif keyterm.count("@") == 2 and keyterm.startswith("@") is False: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) -elif keyterm.count("@") > 2: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) -else: - ksearch.append(keyterm) - -for k in ksearch: - payload = {"op": "get", "search": k} - try: - r = requests.get(url, verify=True, params=payload) - if r.ok is True: - result = c.key_import(r.content) - elif r.ok is False: - result = r.content - except Exception as e: - result = None - - if result is not None and hasattr(result, "considered") is False: - print("{0} for {1}".format(result.decode(), k)) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - - def knom(): - for ki in result.imports: - for ku in c.get_key(ki.fpr).uids: - return ku.uid - - print(""" -The total number of keys considered for import was: {0} - -With UIDs wholely or partially matching the following string: - - {1} - - Number of keys revoked: {2} - Number of new signatures: {3} - Number of new subkeys: {4} - Number of new user IDs: {5} -Number of new secret keys: {6} - Number of unchanged keys: {7} - -The key IDs for all considered keys were: -""".format(num_keys, knom(), new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - elif result is None: - print(e) diff --git a/lang/python/examples/howto/pmkey-import-hkp-alt.py b/lang/python/examples/howto/pmkey-import-hkp-alt.py deleted file mode 100755 index 32553fbf..00000000 --- a/lang/python/examples/howto/pmkey-import-hkp-alt.py +++ /dev/null @@ -1,177 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import hkp4py -import os.path -import sys - -del absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script searches the ProtonMail key server for the specified key and -imports it. Optionally enables specifying a different GnuPG home directory. - -Usage: pmkey-import-hkp.py [homedir] [search string] - or: pmkey-import-hkp.py [search string] -""") - -c = gpg.Context(armor=True) -server = hkp4py.KeyServer("hkps://api.protonmail.ch") -keyterms = [] -ksearch = [] -allkeys = [] -results = [] -paradox = [] -homeless = None - -if len(sys.argv) > 3: - homedir = sys.argv[1] - keyterms = sys.argv[2:] -elif len(sys.argv) == 3: - homedir = sys.argv[1] - keyterm = sys.argv[2] - keyterms.append(keyterm) -elif len(sys.argv) == 2: - homedir = "" - keyterm = sys.argv[1] - keyterms.append(keyterm) -else: - keyterm = input("Enter the key ID, UID or search string: ") - homedir = input("Enter the GPG configuration directory path (optional): ") - keyterms.append(keyterm) - -if len(homedir) == 0: - homedir = None - homeless = False - -if homedir is not None: - if homedir.startswith("~"): - if os.path.exists(os.path.expanduser(homedir)) is True: - if os.path.isdir(os.path.expanduser(homedir)) is True: - c.home_dir = os.path.realpath(os.path.expanduser(homedir)) - else: - homeless = True - else: - homeless = True - elif os.path.exists(os.path.realpath(homedir)) is True: - if os.path.isdir(os.path.realpath(homedir)) is True: - c.home_dir = os.path.realpath(homedir) - else: - homeless = True - else: - homeless = True - -# First check to see if the homedir really is a homedir and if not, treat it as -# a search string. -if homeless is True: - keyterms.append(homedir) - c.home_dir = None -else: - pass - -for keyterm in keyterms: - if keyterm.count("@") == 2 and keyterm.startswith("@") is True: - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - elif keyterm.count("@") == 1 and keyterm.startswith("@") is True: - ksearch.append("{0}@protonmail.com".format(keyterm[1:])) - ksearch.append("{0}@protonmail.ch".format(keyterm[1:])) - ksearch.append("{0}@pm.me".format(keyterm[1:])) - elif keyterm.count("@") == 0: - ksearch.append("{0}@protonmail.com".format(keyterm)) - ksearch.append("{0}@protonmail.ch".format(keyterm)) - ksearch.append("{0}@pm.me".format(keyterm)) - elif keyterm.count("@") == 2 and keyterm.startswith("@") is False: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) - elif keyterm.count("@") > 2: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) - else: - ksearch.append(keyterm) - -for k in ksearch: - print("Checking for key for: {0}".format(k)) - import_result = None - keys = server.search(k) - if isinstance(keys, list) is True: - for key in keys: - allkeys.append(key) - try: - import_result = c.key_import(key.key_blob) - except Exception as e: - import_result = c.key_import(key.key) - else: - paradox.append(keys) - results.append(import_result) - -for result in results: - if result is not None and hasattr(result, "considered") is False: - print("{0} for {1}".format(result.decode(), k)) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - - def knom(): - for ki in result.imports: - for ku in c.get_key(ki.fpr).uids: - return ku.uid - - print(""" -The total number of keys considered for import was: {0} - -With UIDs wholely or partially matching the following string: - - {1} - - Number of keys revoked: {2} - Number of new signatures: {3} - Number of new subkeys: {4} - Number of new user IDs: {5} -Number of new secret keys: {6} - Number of unchanged keys: {7} - -The key IDs for all considered keys were: -""".format(num_keys, knom(), new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - elif result is None: - pass diff --git a/lang/python/examples/howto/pmkey-import-hkp.py b/lang/python/examples/howto/pmkey-import-hkp.py deleted file mode 100755 index dff7dfa5..00000000 --- a/lang/python/examples/howto/pmkey-import-hkp.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import hkp4py -import os.path -import sys - -del absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script searches the ProtonMail key server for the specified key and -imports it. - -Usage: pmkey-import-hkp.py [search strings] -""") - -c = gpg.Context(armor=True) -server = hkp4py.KeyServer("hkps://api.protonmail.ch") -keyterms = [] -ksearch = [] -allkeys = [] -results = [] -paradox = [] -homeless = None - -if len(sys.argv) > 2: - keyterms = sys.argv[1:] -elif len(sys.argv) == 2: - keyterm = sys.argv[1] - keyterms.append(keyterm) -else: - key_term = input("Enter the key ID, UID or search string: ") - keyterms = key_term.split() - -for keyterm in keyterms: - if keyterm.count("@") == 2 and keyterm.startswith("@") is True: - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - elif keyterm.count("@") == 1 and keyterm.startswith("@") is True: - ksearch.append("{0}@protonmail.com".format(keyterm[1:])) - ksearch.append("{0}@protonmail.ch".format(keyterm[1:])) - ksearch.append("{0}@pm.me".format(keyterm[1:])) - elif keyterm.count("@") == 0: - ksearch.append("{0}@protonmail.com".format(keyterm)) - ksearch.append("{0}@protonmail.ch".format(keyterm)) - ksearch.append("{0}@pm.me".format(keyterm)) - elif keyterm.count("@") == 2 and keyterm.startswith("@") is False: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) - elif keyterm.count("@") > 2: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) - else: - ksearch.append(keyterm) - -for k in ksearch: - print("Checking for key for: {0}".format(k)) - import_result = None - keys = server.search(k) - if isinstance(keys, list) is True: - for key in keys: - allkeys.append(key) - try: - import_result = c.key_import(key.key_blob) - except Exception as e: - import_result = c.key_import(key.key) - else: - paradox.append(keys) - results.append(import_result) - -for result in results: - if result is not None and hasattr(result, "considered") is False: - print("{0} for {1}".format(result.decode(), k)) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - - def knom(): - for ki in result.imports: - for ku in c.get_key(ki.fpr).uids: - return ku.uid - - print(""" -The total number of keys considered for import was: {0} - -With UIDs wholely or partially matching the following string(s): - - {1} - - Number of keys revoked: {2} - Number of new signatures: {3} - Number of new subkeys: {4} - Number of new user IDs: {5} -Number of new secret keys: {6} - Number of unchanged keys: {7} - -The key IDs for all considered keys were: -""".format(num_keys, knom(), new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - elif result is None: - pass diff --git a/lang/python/examples/howto/pmkey-import.py b/lang/python/examples/howto/pmkey-import.py deleted file mode 100755 index e95785b3..00000000 --- a/lang/python/examples/howto/pmkey-import.py +++ /dev/null @@ -1,124 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import requests -import sys - -del absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -print(""" -This script searches the ProtonMail key server for the specified key and -imports it. - -Usage: pmkey-import.py [search string] -""") - -c = gpg.Context(armor=True) -url = "https://api.protonmail.ch/pks/lookup" -ksearch = [] - -if len(sys.argv) >= 2: - keyterm = sys.argv[1] -else: - keyterm = input("Enter the key ID, UID or search string: ") - -if keyterm.count("@") == 2 and keyterm.startswith("@") is True: - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) - ksearch.append(keyterm[1:]) -elif keyterm.count("@") == 1 and keyterm.startswith("@") is True: - ksearch.append("{0}@protonmail.com".format(keyterm[1:])) - ksearch.append("{0}@protonmail.ch".format(keyterm[1:])) - ksearch.append("{0}@pm.me".format(keyterm[1:])) -elif keyterm.count("@") == 0: - ksearch.append("{0}@protonmail.com".format(keyterm)) - ksearch.append("{0}@protonmail.ch".format(keyterm)) - ksearch.append("{0}@pm.me".format(keyterm)) -elif keyterm.count("@") == 2 and keyterm.startswith("@") is False: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) -elif keyterm.count("@") > 2: - uidlist = keyterm.split("@") - for uid in uidlist: - ksearch.append("{0}@protonmail.com".format(uid)) - ksearch.append("{0}@protonmail.ch".format(uid)) - ksearch.append("{0}@pm.me".format(uid)) -else: - ksearch.append(keyterm) - -for k in ksearch: - payload = {"op": "get", "search": k} - try: - r = requests.get(url, verify=True, params=payload) - if r.ok is True: - result = c.key_import(r.content) - elif r.ok is False: - result = r.content - except Exception as e: - result = None - - if result is not None and hasattr(result, "considered") is False: - print("{0} for {1}".format(result.decode(), k)) - elif result is not None and hasattr(result, "considered") is True: - num_keys = len(result.imports) - new_revs = result.new_revocations - new_sigs = result.new_signatures - new_subs = result.new_sub_keys - new_uids = result.new_user_ids - new_scrt = result.secret_imported - nochange = result.unchanged - - def knom(): - for ki in result.imports: - for ku in c.get_key(ki.fpr).uids: - return ku.uid - - print(""" -The total number of keys considered for import was: {0} - -With UIDs wholely or partially matching the following string: - - {1} - - Number of keys revoked: {2} - Number of new signatures: {3} - Number of new subkeys: {4} - Number of new user IDs: {5} -Number of new secret keys: {6} - Number of unchanged keys: {7} - -The key IDs for all considered keys were: -""".format(num_keys, knom(), new_revs, new_sigs, new_subs, new_uids, new_scrt, - nochange)) - for i in range(num_keys): - print(result.imports[i].fpr) - print("") - elif result is None: - print(e) diff --git a/lang/python/examples/howto/post_installer.py b/lang/python/examples/howto/post_installer.py deleted file mode 100755 index b493f979..00000000 --- a/lang/python/examples/howto/post_installer.py +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -# Copyright (C) 2016-2018 g10 Code GmbH -# Copyright (C) 2015 Ben McGinnes <[email protected]> -# -# This library 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 library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -from __future__ import absolute_import, print_function, unicode_literals - -import glob -import os -import os.path -import shutil -import subprocess -import sys -import sysconfig - -from shutil import which - -del absolute_import, print_function, unicode_literals - -try: - emacs = os.path.realpath(which("emacs")) -except TypeError as e: - emacs = None - -try: - makeinfo = os.path.realpath(which("makeinfo")) -except TypeError as e: - makeinfo = None - -try: - pandoc = os.path.realpath(which("pandoc")) -except TypeError as e: - pandoc = None - -try: - texinfo = os.path.realpath(which("texinfo")) -except TypeError as e: - texinfo = None - -docsrc = glob.glob('doc/src/**/*', recursive=True) - -for srcdoc in docsrc: - process = subprocess.Popen([emacs, srcdoc, "--batch", "-f", - "org-texinfo-export-to-texinfo", "--kill"], - stdout=subprocess.PIPE) - procom = process.communicate() - -doctexi1 = glob.glob('doc/src/**/*.texi', recursive=True) -doctexi2 = [] -doctexi3 = [] - -for texi in doctexi1: - doctexi2.append(os.path.realpath(texi)) - -for texdoc in doctexi2: - newtex = texdoc.replace("doc/src/", "doc/texinfo/") - doctexi3.append(newtex) - with open(texdoc, "r") as f: - badtex = f.read() - goodtex = badtex.replace("@documentencoding UTF-8\n", - "@documentencoding utf-8\n") - with open(newtex, "w") as f: - f.write(goodtex) - -for srcdoc in docsrc: - rstdoc = "{0}.rst".format(srcdoc.replace("doc/src/", "doc/rst/")) - process = subprocess.Popen([pandoc, "-f", "org", "-t", "rst+smart", "-o", - rstdoc, srcdoc], stdout=subprocess.PIPE) - procom = process.communicate() - -with open("doc/rst/index.rst", "r") as f: - genindex = f.readlines() - -indextop = ['.. GPGME Python Bindings documentation master file, created by\n', - ' sphinx-quickstart on Wed Dec 5 09:04:47 2018.\n', - ' You can adapt this file completely to your liking, but it should at least\n', - ' contain the root `toctree` directive.\n', '\n', - 'GPGME Python Bindings\n', '=====================\n', '\n', - '.. toctree::\n', ' :maxdepth: 3\n', ' :caption: Contents:\n', - '\n'] - -with open("doc/rst/index.rst", "w") as f: - for line in indextop: - f.write(line) - for line in genindex[5:]: - f.write(line) - -with open("doc/rst/Makefile", "w") as f: - f.write("""# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -SOURCEDIR = . -BUILDDIR = _build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -""") - -info_path = os.path.realpath(sysconfig._PREFIX + "/share/info") -info_paths = os.environ["INFOPATH"].split(":") - -if info_paths.count(info_path) == 0: - info_paths.insert(0, info_path) -else: - pass - -for ipath in info_paths: - if os.path.exists(os.path.realpath(ipath)) is False: - info_paths.remove(ipath) - else: - pass - -# Remove the old generated .texi files from the org source directory. -for texifile in doctexi2: - os.remove(texifile) - -print(""" -You may now build your preferred documentation format using either: - - 1. Sphinx in the doc/rst/ directory; and/or - 2. Texinfo or Makeinfo in the doc/texinfo/ directory. - -Alternatively the original Org mode source files can be found in the doc/src/ -directory. -""") diff --git a/lang/python/examples/howto/requirements.txt b/lang/python/examples/howto/requirements.txt deleted file mode 100644 index 8f6e1e15..00000000 --- a/lang/python/examples/howto/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -hkp4py -requests diff --git a/lang/python/examples/howto/revoke-userid.py b/lang/python/examples/howto/revoke-userid.py deleted file mode 100755 index 70ed79a6..00000000 --- a/lang/python/examples/howto/revoke-userid.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -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 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 deleted file mode 100755 index 5f3eba70..00000000 --- a/lang/python/examples/howto/send-key-to-keyserver.py +++ /dev/null @@ -1,98 +0,0 @@ -#!/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 <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -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 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 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-file.py b/lang/python/examples/howto/sign-file.py deleted file mode 100755 index 5fbce5a3..00000000 --- a/lang/python/examples/howto/sign-file.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys - -""" -Signs a file with a specified key. If entering both the key and the filename -on the command line, the key must be entered first. - -Will produce both an ASCII armoured and GPG binary format copy of the signed -file. -""" - -if len(sys.argv) > 3: - logrus = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - logrus = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - logrus = sys.argv[1] - filename = input("Enter the path and filename to sign: ") -else: - logrus = input("Enter the fingerprint or key ID to sign with: ") - filename = input("Enter the path and filename to sign: ") - -with open(filename, "rb") as f: - text = f.read() - -key = list(gpg.Context().keylist(pattern=logrus)) - -with gpg.Context(armor=True, signers=key) as ca: - signed_data, result = ca.sign(text, mode=gpg.constants.sig.mode.NORMAL) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(signed_data) - -with gpg.Context(signers=key) as cg: - signed_data, result = cg.sign(text, mode=gpg.constants.sig.mode.NORMAL) - with open("{0}.gpg".format(filename), "wb") as fg: - fg.write(signed_data) diff --git a/lang/python/examples/howto/sign-key.py b/lang/python/examples/howto/sign-key.py deleted file mode 100755 index 82e96f88..00000000 --- a/lang/python/examples/howto/sign-key.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -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 userid and sig_type.lower() == "local": - c.key_sign(key, uids=userid, local=True) -elif userid and sig_type.lower() != "local": - c.key_sign(key, uids=userid) -elif not userid and sig_type.lower() == "local": - c.key_sign(key, local=True) -else: - c.key_sign(key) diff --git a/lang/python/examples/howto/symcrypt-file.py b/lang/python/examples/howto/symcrypt-file.py deleted file mode 100755 index eeeac991..00000000 --- a/lang/python/examples/howto/symcrypt-file.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import gpg -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -""" -Symmetrically encrypts a file. Passphrase will be prompted for via Pinentry. - -Will produce both an ASCII armoured and GPG binary format copy of the encrypted -file. -""" - -if len(sys.argv) > 2: - filename = " ".join(sys.argv[1:]) -elif len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = input("Enter the path and filename to encrypt: ") - -with open(filename, "rb") as f: - text = f.read() - -with gpg.Context(armor=True) as ca: - try: - ciphertext, result, sign_result = ca.encrypt(text, passphrase=None, - sign=False) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(ciphertext) - except gpg.errors.GPGMEError as e: - print(e) - -with gpg.Context() as cg: - try: - ciphertext, result, sign_result = cg.encrypt(text, passphrase=None, - sign=False) - with open("{0}.gpg".format(filename), "wb") as fg: - fg.write(ciphertext) - except gpg.errors.GPGMEError as e: - print(e) diff --git a/lang/python/examples/howto/temp-homedir-config.py b/lang/python/examples/howto/temp-homedir-config.py deleted file mode 100755 index cb968d3d..00000000 --- a/lang/python/examples/howto/temp-homedir-config.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -import os -import os.path -import sys - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -intro = """ -This script creates a temporary directory to use as a homedir for -testing key generation tasks with the correct permissions, along -with a gpg.conf file containing the same configuration options -listed in the HOWTO. - -You may wish to change the order of the cipher preferences or -remove those not relevant to your installation. These -configuration parameters assume that all ciphers and digests are -installed and available rather than limiting to the default -ciphers and digests. - -The script prompts for a directory name to be installed as a hidden -directory in the user's home directory on POSIX systems. So if you -enter "gnupg-temp" on a Linux, BSD or OS X system, it will create -"~/.gnupg-temp" (you do not need to enter the leading dot). - -This script has not been tested on Windows systems and may have -unpredictable results. That said, it will not delete or copy over -existing data. - -If the directory already exists, the script will terminate with a -message telling you to specify a new directory name. There is no -default directory name. -""" - -ciphers256 = "TWOFISH CAMELLIA256 AES256" -ciphers192 = "CAMELLIA192 AES192" -ciphers128 = "CAMELLIA128 AES" -ciphersBad = "BLOWFISH IDEA CAST5 3DES" -digests = "SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1" -compress = "ZLIB BZIP2 ZIP Uncompressed" - -gpgconf = """# gpg.conf settings for key generation: -expert -allow-freeform-uid -allow-secret-key-import -trust-model tofu+pgp -tofu-default-policy unknown -enable-large-rsa -enable-dsa2 -cert-digest-algo SHA512 -default-preference-list {0} {1} {2} {3} {4} {5} -personal-cipher-preferences {0} {1} {2} {3} -personal-digest-preferences {4} -personal-compress-preferences {5} -""".format(ciphers256, ciphers192, ciphers128, ciphersBad, digests, compress) - -agentconf = """# gpg-agent.conf settings for key generation: -default-cache-ttl 300 -""" - -if len(sys.argv) == 1: - print(intro) - new_homedir = input("Enter the temporary gnupg homedir name: ") -elif len(sys.argv) == 2: - new_homedir = sys.argv[1] -else: - new_homedir = " ".join(sys.argv[1:]) - -userdir = os.path.expanduser("~") - -if new_homedir.startswith("~"): - new_homedir.replace("~", "") -else: - pass - -if new_homedir.startswith("/"): - new_homedir.replace("/", "") -else: - pass - -if new_homedir.startswith("."): - new_homedir.replace(".", "_") -else: - pass - -if new_homedir.count(" ") > 0: - new_homedir.replace(" ", "_") -else: - pass - -nh = "{0}/.{1}".format(userdir, new_homedir) - -def open_0o600(path, flags): - return os.open(path, flags, mode=0o600) - -if os.path.exists(nh) is True: - print("The {0} directory already exists.".format(nh)) -else: - print("Creating the {0} directory.".format(nh)) - os.mkdir(nh, 0o700) - with open("{0}/{1}".format(nh, "gpg.conf"), "w", opener=open_0o600) as f1: - f1.write(gpgconf) - with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w", opener=open_0o600) as f2: - f2.write(gpgconf) - print("""You may now use the {0} directory as an alternative GPG homedir: - -gpg --homedir {0} -gpg --homedir --full-gen-key - -Or with GPGME scripts, including the GPGME Python bindings. -""") diff --git a/lang/python/examples/howto/verify-signatures.py b/lang/python/examples/howto/verify-signatures.py deleted file mode 100755 index 9c838f85..00000000 --- a/lang/python/examples/howto/verify-signatures.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys -import time - -""" -Verifies a signed file which has been signed with a detached signature. -""" - -if len(sys.argv) > 2: - filename = sys.argv[1] - sig_file = sys.argv[2] -elif len(sys.argv) == 2: - filename = sys.argv[1] - sig_file = input("Enter the path and filename of the detached signature: ") -else: - filename = input("Enter the path and filename to verify: ") - sig_file = input("Enter the path and filename of the detached signature: ") - -c = gpg.Context() - -try: - data, result = c.verify(open(filename), open(sig_file)) - verified = True -except gpg.errors.BadSignatures as e: - verified = False - print(e) - -if verified is True: - for i in range(len(result.signatures)): - sign = result.signatures[i] - print("""Good signature from: -{0} -with key {1} -made at {2} -""".format(c.get_key(sign.fpr).uids[0].uid, sign.fpr, - time.ctime(sign.timestamp))) -else: - pass diff --git a/lang/python/examples/howto/verify-signed-file.py b/lang/python/examples/howto/verify-signed-file.py deleted file mode 100755 index a7714309..00000000 --- a/lang/python/examples/howto/verify-signed-file.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes <[email protected]> -# -# 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 License along with this program; if not, see -# <https://www.gnu.org/licenses/>. - -import gpg -import sys -import time - -""" -Verifies a signed file which has been signed with either NORMAL or CLEAR modes. -""" - -if len(sys.argv) > 2: - filename = " ".join(sys.argv[1:]) -elif len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = input("Enter the path and filename to sign: ") - -c = gpg.Context() - -try: - data, result = c.verify(open(filename)) - verified = True -except gpg.errors.BadSignatures as e: - verified = False - print(e) - -if verified is True: - for i in range(len(result.signatures)): - sign = result.signatures[i] - print("""Good signature from: -{0} -with key {1} -made at {2} -""".format(c.get_key(sign.fpr).uids[0].uid, sign.fpr, - time.ctime(sign.timestamp))) -else: - pass |