From 6950a63e63d60685ddb6f4cbff7b826b8acb5b13 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 08:26:57 +1100 Subject: docs: python bindings examples * Added reference to location where all the examples included in the HOWTO will be available as executable scripts. * Included a short README file in that location. --- lang/python/examples/howto/README.org | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lang/python/examples/howto/README.org (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/README.org b/lang/python/examples/howto/README.org new file mode 100644 index 00000000..ee8c9863 --- /dev/null +++ b/lang/python/examples/howto/README.org @@ -0,0 +1,24 @@ +#+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 } + + +* 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. + -- cgit v1.2.3 From 8f7672ad1b267f122f647bb5f984734d0ff66a5c Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 08:31:53 +1100 Subject: doc: python bindings example README * Added the same license as used with the HOWTO. * Since these examples are so basic, they'll be dual licensed the same as GPGME itself (otherwise it would slip too dangerously against the need for permissive licensing of crypto libraries). --- lang/python/examples/howto/README.org | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/README.org b/lang/python/examples/howto/README.org index ee8c9863..604037f1 100644 --- a/lang/python/examples/howto/README.org +++ b/lang/python/examples/howto/README.org @@ -22,3 +22,31 @@ 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: + + +** 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. -- cgit v1.2.3 From b30ebf89725641018b3b08f77876530f9b983fa2 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 08:39:49 +1100 Subject: doc: python bindings examples * Explicitly stated that all this code is released under the GPLv2+ and the LGPLv2.1+. --- lang/python/examples/howto/README.org | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/README.org b/lang/python/examples/howto/README.org index 604037f1..b74ae7e2 100644 --- a/lang/python/examples/howto/README.org +++ b/lang/python/examples/howto/README.org @@ -28,6 +28,12 @@ :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: -- cgit v1.2.3 From cfbdcb7fb3fa438cafba82e4fb8f327df596f98e Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 08:55:01 +1100 Subject: example: python bindings key count * Added script wo count the number of keys in both the public and secret key stores. --- lang/python/examples/howto/keycount.py | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 lang/python/examples/howto/keycount.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/keycount.py b/lang/python/examples/howto/keycount.py new file mode 100755 index 00000000..7dd5e778 --- /dev/null +++ b/lang/python/examples/howto/keycount.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg + +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) -- cgit v1.2.3 From 7ab42e79ade89f28507ea42d51148a40b4bfc736 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 09:25:34 +1100 Subject: example: encrypt file * Example to encrypt a file to a single key. * Takes key ID and/or fpr as a CLI parameter. * Takes path and filename as a CLI parameter. * Encrypts to specified key only, no signing and writes the output in both ASCII armoured and GPG binary formats with output filenames based on input filename. --- lang/python/examples/howto/encrypt-file.py | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lang/python/examples/howto/encrypt-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py new file mode 100755 index 00000000..718c7157 --- /dev/null +++ b/lang/python/examples/howto/encrypt-file.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import sys + +""" +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(c.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, + sign=False) + with open("{0}.asc".format(filename), "wb") as fa: + fa.write(ciphertext) + +with gpg.Context() as cg: + ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, + sign=False) + with open("{0}.gpg".format(filename), "wb") as fg: + fg.write(ciphertext) -- cgit v1.2.3 From f0790f224d7af9521efe96e69a8f719fb89a5af2 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 09:39:48 +1100 Subject: example: encrypt file * Fixed typo in second encryption call. --- lang/python/examples/howto/encrypt-file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py index 718c7157..8aee52ad 100755 --- a/lang/python/examples/howto/encrypt-file.py +++ b/lang/python/examples/howto/encrypt-file.py @@ -58,7 +58,7 @@ with gpg.Context(armor=True) as ca: fa.write(ciphertext) with gpg.Context() as cg: - ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, + ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, sign=False) with open("{0}.gpg".format(filename), "wb") as fg: fg.write(ciphertext) -- cgit v1.2.3 From f3fe47e8fd2e7bc748016befcae494421223368c Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 09:47:39 +1100 Subject: example: sign and encrypt file * Example to sign and encrypt a file. * Similar to encrypt-file.py except all keys are considered trusted and signs with the default key. * Also encrypts to the default key. --- lang/python/examples/howto/encrypt-sign-file.py | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 lang/python/examples/howto/encrypt-sign-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py new file mode 100755 index 00000000..c8850b24 --- /dev/null +++ b/lang/python/examples/howto/encrypt-sign-file.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import sys + +""" +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 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(c.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) -- cgit v1.2.3 From 7221bb67642eb01a07957d66d0cbcd4ef8aadbf8 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 09:53:27 +1100 Subject: example: encrypt file * Nested encryption in try/except statement in case recipient key is untrusted or invalid. --- lang/python/examples/howto/encrypt-file.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py index 8aee52ad..017a3421 100755 --- a/lang/python/examples/howto/encrypt-file.py +++ b/lang/python/examples/howto/encrypt-file.py @@ -52,13 +52,19 @@ 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, - sign=False) - with open("{0}.asc".format(filename), "wb") as fa: - fa.write(ciphertext) + 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: - ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, - sign=False) - with open("{0}.gpg".format(filename), "wb") as fg: - fg.write(ciphertext) + 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) -- cgit v1.2.3 From 29e918171f352c71a90a16c04d4a3dcafa5db682 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 14:19:16 +1100 Subject: example: groups work-around * Added groups selection work around code. * Intended for use as a module to be imported by other scripts, usually with "from groups import group_lists" or "from groups import group_lines" or similar. --- lang/python/examples/howto/groups.py | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lang/python/examples/howto/groups.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/groups.py b/lang/python/examples/howto/groups.py new file mode 100644 index 00000000..67fd7838 --- /dev/null +++ b/lang/python/examples/howto/groups.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import subprocess + +lines = subprocess.getoutput("gpgconf --list-options gpg").splitlines() + +for i in range(len(lines)): + if lines[i].startswith("group") is True: + line = lines[i] + else: + pass + +groups = line.split(":")[-1].replace('"', '').split(',') + +group_lines = groups +for i in range(len(group_lines)): + group_lines[i] = group_lines[i].split("=") + +group_lists = group_lines +for i in range(len(group_lists)): + group_lists[i][1] = group_lists[i][1].split() -- cgit v1.2.3 From 51258975d763c9471859d635e6080c2ec02e8647 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 14:32:53 +1100 Subject: example: decrypt file * Decrypts a file taking file names as command line parameters. --- lang/python/examples/howto/decrypt-file.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 lang/python/examples/howto/decrypt-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/decrypt-file.py b/lang/python/examples/howto/decrypt-file.py new file mode 100755 index 00000000..60a050bd --- /dev/null +++ b/lang/python/examples/howto/decrypt-file.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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 of file to save decrypted data to: ") +else: + ciphertext = input("Enter path and filename of encrypted file: ") + newfile = input("Enter path and filename of file to save decrypted data to: ") + +with open(ciphertext, "rb") as cfile: + plaintext, result, verify_result = gpg.Context().decrypt(cfile) + +with open(newfile, "wb") as nfile: + nfile.write(plaintext) -- cgit v1.2.3 From 96d0395bccbbff91f73c06cb7bd6c131f04b8a9a Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Tue, 20 Mar 2018 14:55:05 +1100 Subject: example: keycount * Fixed missing parenthesis. --- lang/python/examples/howto/keycount.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/keycount.py b/lang/python/examples/howto/keycount.py index 7dd5e778..8e25454c 100755 --- a/lang/python/examples/howto/keycount.py +++ b/lang/python/examples/howto/keycount.py @@ -39,4 +39,4 @@ pubnum = len(publist) print(""" Number of secret keys: {0} Number of public keys: {1} -""".format(secnum, pubnum) +""".format(secnum, pubnum)) -- cgit v1.2.3 From 0390ede18696520be9cc1a42f628e23159b7c2eb Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Wed, 21 Mar 2018 12:28:03 +1100 Subject: example: sign file * Similar to encrypt file except for signing a file in normal mode. * Noticed additional changes to be made to the howto to match this, but they will have to wait due to a power outage (currently running on battery and a mobile connection, but that won't last). --- lang/python/examples/howto/sign-file.py | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 lang/python/examples/howto/sign-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/sign-file.py b/lang/python/examples/howto/sign-file.py new file mode 100755 index 00000000..8e2cdc2d --- /dev/null +++ b/lang/python/examples/howto/sign-file.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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 encrypted 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) -- cgit v1.2.3 From 1fdd1f306d45f6aeee91c7f016f7c37286ee3b3b Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 01:18:37 +1100 Subject: example: clear signing * Added example to clear sign a file with signing key selection. --- lang/python/examples/howto/clear-sign-file.py | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 lang/python/examples/howto/clear-sign-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/clear-sign-file.py b/lang/python/examples/howto/clear-sign-file.py new file mode 100755 index 00000000..597bbc59 --- /dev/null +++ b/lang/python/examples/howto/clear-sign-file.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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) -- cgit v1.2.3 From 6fa2a344282e369e6aca8155bc77dd2c12a29414 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 01:24:52 +1100 Subject: examples: doc strings * Fixed minor errors in two doc strings. --- lang/python/examples/howto/encrypt-file.py | 3 ++- lang/python/examples/howto/sign-file.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py index 017a3421..877226d0 100755 --- a/lang/python/examples/howto/encrypt-file.py +++ b/lang/python/examples/howto/encrypt-file.py @@ -31,7 +31,8 @@ import sys 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. +Will produce both an ASCII armoured and GPG binary format copy of the encrypted +file. """ if len(sys.argv) > 3: diff --git a/lang/python/examples/howto/sign-file.py b/lang/python/examples/howto/sign-file.py index 8e2cdc2d..01006df0 100755 --- a/lang/python/examples/howto/sign-file.py +++ b/lang/python/examples/howto/sign-file.py @@ -31,7 +31,8 @@ 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 encrypted file. +Will produce both an ASCII armoured and GPG binary format copy of the signed +file. """ if len(sys.argv) > 3: -- cgit v1.2.3 From af6cbba18ba5e2bbecce5f8268c146282cd12367 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 01:26:43 +1100 Subject: example: encrypt-sign-file.py * Adjusted the doc string. --- lang/python/examples/howto/encrypt-sign-file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py index c8850b24..4b29b27b 100755 --- a/lang/python/examples/howto/encrypt-sign-file.py +++ b/lang/python/examples/howto/encrypt-sign-file.py @@ -34,8 +34,8 @@ 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 encrypted -file. +Will produce both an ASCII armoured and GPG binary format copy of the signed +and encrypted file. """ if len(sys.argv) > 3: -- cgit v1.2.3 From ac6a552c37147a000de74f49d1bff34dad52252e Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 01:30:32 +1100 Subject: example: detach sign file * Added example to make detached signatures of a file with key selection. --- lang/python/examples/howto/detach-sign-file.py | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lang/python/examples/howto/detach-sign-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/detach-sign-file.py b/lang/python/examples/howto/detach-sign-file.py new file mode 100755 index 00000000..99fbe65e --- /dev/null +++ b/lang/python/examples/howto/detach-sign-file.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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) -- cgit v1.2.3 From ae2767eb27b6a76284ee4403e575869afe2e80a8 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 01:50:08 +1100 Subject: example: verify signed file * Added example to verify normal and clearsigned files. --- lang/python/examples/howto/verify-signed-file.py | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 lang/python/examples/howto/verify-signed-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/verify-signed-file.py b/lang/python/examples/howto/verify-signed-file.py new file mode 100755 index 00000000..9f8702f5 --- /dev/null +++ b/lang/python/examples/howto/verify-signed-file.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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 -- cgit v1.2.3 From ad6cb4f9b8b97a2bc501c17fc542a84b725dedea Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 03:58:58 +1100 Subject: example: verify signatures * Added example for verifying detached signatures against the files they're the signatures for. --- lang/python/examples/howto/verify-signatures.py | 64 +++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lang/python/examples/howto/verify-signatures.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/verify-signatures.py b/lang/python/examples/howto/verify-signatures.py new file mode 100755 index 00000000..8aafc3ba --- /dev/null +++ b/lang/python/examples/howto/verify-signatures.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import 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 -- cgit v1.2.3 From a4e3f827652c59d850b4e5506a92c1ecd190c1bb Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 04:04:05 +1100 Subject: example: groups * Added a docstring. --- lang/python/examples/howto/groups.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/groups.py b/lang/python/examples/howto/groups.py index 67fd7838..e7adc440 100644 --- a/lang/python/examples/howto/groups.py +++ b/lang/python/examples/howto/groups.py @@ -25,6 +25,12 @@ from __future__ import absolute_import, division, unicode_literals import subprocess +""" +Intended for use with other scripts. + +Usage: from groups import group_lines, group_lists +""" + lines = subprocess.getoutput("gpgconf --list-options gpg").splitlines() for i in range(len(lines)): -- cgit v1.2.3 From 6c6af9a7b0ae4e7182d669bec282c6edaaa7eaa1 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 05:07:56 +1100 Subject: example groups work around * Updated usage so it only references importing the final list of lists produced. Trying to use some of the mid-points can have unpredictable results (this is part of the problem with work arounds). --- lang/python/examples/howto/groups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/groups.py b/lang/python/examples/howto/groups.py index e7adc440..5e7fdf60 100644 --- a/lang/python/examples/howto/groups.py +++ b/lang/python/examples/howto/groups.py @@ -28,7 +28,7 @@ import subprocess """ Intended for use with other scripts. -Usage: from groups import group_lines, group_lists +Usage: from groups import group_lists """ lines = subprocess.getoutput("gpgconf --list-options gpg").splitlines() -- cgit v1.2.3 From 8b401bfc76eac762553f76faab53c2f4cd117a8d Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 05:20:51 +1100 Subject: example: group key selection * Example of preparing a keylist object using an existing group line from the gpg.conf file. --- lang/python/examples/howto/group-key-selection.py | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 lang/python/examples/howto/group-key-selection.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/group-key-selection.py b/lang/python/examples/howto/group-key-selection.py new file mode 100755 index 00000000..2e3d2ebb --- /dev/null +++ b/lang/python/examples/howto/group-key-selection.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import sys + +from groups import group_lists + +""" +Takes an existing group specified as a command line parameter and converts it +to a list object 'keys' as expected by the gpg module. + +Requires the groups module in this directory. +""" + +if len(sys.argv) == 2: + group = sys.argv[1] +elif len(sys.argv) == 1: + group = input("Enter the name of the group to select keys for: ") +else: + group = input("Enter the name of the group to select keys for: ") + +keys = [] +a = [] + +for i in range(len(group_lists)): + a.append(group_lists[i][0]) + +b = a.index(group) + +for i in range(len(group_lists[b][1])): + logrus = group_lists[b][1][i] + keys.append(gpg.Context().keylist(pattern=logrus)) -- cgit v1.2.3 From 0ccc57c9512246d82d46e7732bfb0f95c18ca9d3 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 05:40:02 +1100 Subject: example: sign and encrypt to group * Begins to string together some of the simpler examples to do more useful things. * Signs and encrypts a file while encrypting to every key in a group specified in the gpg.conf file. --- .../examples/howto/group-encrypt-sign-file.py | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 lang/python/examples/howto/group-encrypt-sign-file.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/group-encrypt-sign-file.py b/lang/python/examples/howto/group-encrypt-sign-file.py new file mode 100755 index 00000000..920e50da --- /dev/null +++ b/lang/python/examples/howto/group-encrypt-sign-file.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import sys + +from groups import group_lists + +""" +Signs and encrypts a file to a specified group of keys. If entering both the +group and the filename on the command line, the group 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: + group = sys.argv[1] + filename = " ".join(sys.argv[2:]) +elif len(sys.argv) == 3: + group = sys.argv[1] + filename = sys.argv[2] +elif len(sys.argv) == 2: + group = sys.argv[1] + filename = input("Enter the path and filename to encrypt: ") +else: + group = input("Enter the name of the group to select keys for: ") + filename = input("Enter the path and filename to encrypt: ") + +keys = [] +a = [] + +for i in range(len(group_lists)): + a.append(group_lists[i][0]) + +b = a.index(group) + +for i in range(len(group_lists[b][1])): + logrus = group_lists[b][1][i] + keys.append(gpg.Context().keylist(pattern=logrus)) + +with open(filename, "rb") as f: + text = f.read() + +with gpg.Context(armor=True) as ca: + ciphertext, result, sign_result = ca.encrypt(text, recipients=keys, + 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=keys, + always_trust=True, + add_encrypt_to=True) + with open("{0}.gpg".format(filename), "wb") as fg: + fg.write(ciphertext) -- cgit v1.2.3 From 0a0d57fd41380cd797d29e11cec8a77c7404e960 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 05:52:55 +1100 Subject: example: key selection * Similar to group-key-selection.py, but does not use an existing group from gpg.conf; instead takes multiple key IDs, fingerprints or patterns on the command line and adds them to a keylist object. --- lang/python/examples/howto/key-selection.py | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 lang/python/examples/howto/key-selection.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/key-selection.py b/lang/python/examples/howto/key-selection.py new file mode 100755 index 00000000..8c2d7b58 --- /dev/null +++ b/lang/python/examples/howto/key-selection.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, unicode_literals + +# Copyright (C) 2018 Ben McGinnes +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2.1 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU +# Lesser General Public Licensefor more details. +# +# You should have received a copy of the GNU General Public License and the GNU +# Lesser General Public along with this program; if not, see +# . + +import gpg +import sys + +""" +Uses key IDs, fingerprints or other patterns as space separated input and +creates a keylist object for use by the gpg module. + +Similar to the group-key-selection.py script, but does not require an existing +group in the gpg.conf file. +""" + +if len(sys.argv) < 2: + key_ids_str = sys.argv[1:] +elif len(sys.argv) == 2: + key_ids_str = sys.argv[1] +elif len(sys.argv) == 1: + key_ids_str = input("Enter the keys to select by key ID or fingerprint: ") +else: + key_ids_str = input("Enter the keys to select by key ID or fingerprint: ") + +key_ids = key_ids_str.split() +keys = [] +for i in range(len(key_ids)): + logrus = key_ids[i] + keys.append(gpg.Context().keylist(pattern=logrus)) + -- cgit v1.2.3 From c6a0395f0a3a57071f0c943f7815f58a02f9d2f3 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 05:55:53 +1100 Subject: example: key selection * Removed extraneous blank line. --- lang/python/examples/howto/key-selection.py | 1 - 1 file changed, 1 deletion(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/key-selection.py b/lang/python/examples/howto/key-selection.py index 8c2d7b58..a007219e 100755 --- a/lang/python/examples/howto/key-selection.py +++ b/lang/python/examples/howto/key-selection.py @@ -49,4 +49,3 @@ keys = [] for i in range(len(key_ids)): logrus = key_ids[i] keys.append(gpg.Context().keylist(pattern=logrus)) - -- cgit v1.2.3 From 7ddff71908a85111c8e0da41312197b3b1a77da6 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 06:05:10 +1100 Subject: examples: encryption * Fixed two incorrect Context() objects. --- lang/python/examples/howto/encrypt-file.py | 2 +- lang/python/examples/howto/encrypt-sign-file.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py index 877226d0..ad4e1cef 100755 --- a/lang/python/examples/howto/encrypt-file.py +++ b/lang/python/examples/howto/encrypt-file.py @@ -48,7 +48,7 @@ else: a_key = input("Enter the fingerprint or key ID to encrypt to: ") filename = input("Enter the path and filename to encrypt: ") -rkey = list(c.keylist(pattern=a_key, secret=False)) +rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) with open(filename, "rb") as f: text = f.read() diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py index 4b29b27b..41aaac86 100755 --- a/lang/python/examples/howto/encrypt-sign-file.py +++ b/lang/python/examples/howto/encrypt-sign-file.py @@ -51,7 +51,7 @@ else: a_key = input("Enter the fingerprint or key ID to encrypt to: ") filename = input("Enter the path and filename to encrypt: ") -rkey = list(c.keylist(pattern=a_key, secret=False)) +rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) with open(filename, "rb") as f: text = f.read() -- cgit v1.2.3 From 61a988036bd3f0d43f7d55bfa43f5f05bec978c4 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 06:18:13 +1100 Subject: example: group encryption * Troubleshooting. --- lang/python/examples/howto/group-encrypt-sign-file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/group-encrypt-sign-file.py b/lang/python/examples/howto/group-encrypt-sign-file.py index 920e50da..da73e4cd 100755 --- a/lang/python/examples/howto/group-encrypt-sign-file.py +++ b/lang/python/examples/howto/group-encrypt-sign-file.py @@ -63,7 +63,7 @@ b = a.index(group) for i in range(len(group_lists[b][1])): logrus = group_lists[b][1][i] - keys.append(gpg.Context().keylist(pattern=logrus)) + keys.append(list(gpg.Context().keylist(pattern=logrus))) with open(filename, "rb") as f: text = f.read() -- cgit v1.2.3 From 05e59933056ee8ef8ba7579351a58ed25dd7f754 Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Thu, 22 Mar 2018 06:19:36 +1100 Subject: examples: multi-key selection operations * Temporarily removing multi-key selection based examples. * There are a few issues with getting the key selections to play nicely with gpg.Context().keylist object types. * Will troubleshoot them separately and restore them when that's worked out, but I don't want these more complicated examples to delay merging the HOWTO with master. --- .../examples/howto/group-encrypt-sign-file.py | 83 ---------------------- lang/python/examples/howto/group-key-selection.py | 56 --------------- lang/python/examples/howto/key-selection.py | 51 ------------- 3 files changed, 190 deletions(-) delete mode 100755 lang/python/examples/howto/group-encrypt-sign-file.py delete mode 100755 lang/python/examples/howto/group-key-selection.py delete mode 100755 lang/python/examples/howto/key-selection.py (limited to 'lang/python/examples/howto') diff --git a/lang/python/examples/howto/group-encrypt-sign-file.py b/lang/python/examples/howto/group-encrypt-sign-file.py deleted file mode 100755 index da73e4cd..00000000 --- a/lang/python/examples/howto/group-encrypt-sign-file.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, unicode_literals - -# Copyright (C) 2018 Ben McGinnes -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation; either version 2.1 of the License, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU -# Lesser General Public Licensefor more details. -# -# You should have received a copy of the GNU General Public License and the GNU -# Lesser General Public along with this program; if not, see -# . - -import gpg -import sys - -from groups import group_lists - -""" -Signs and encrypts a file to a specified group of keys. If entering both the -group and the filename on the command line, the group 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: - group = sys.argv[1] - filename = " ".join(sys.argv[2:]) -elif len(sys.argv) == 3: - group = sys.argv[1] - filename = sys.argv[2] -elif len(sys.argv) == 2: - group = sys.argv[1] - filename = input("Enter the path and filename to encrypt: ") -else: - group = input("Enter the name of the group to select keys for: ") - filename = input("Enter the path and filename to encrypt: ") - -keys = [] -a = [] - -for i in range(len(group_lists)): - a.append(group_lists[i][0]) - -b = a.index(group) - -for i in range(len(group_lists[b][1])): - logrus = group_lists[b][1][i] - keys.append(list(gpg.Context().keylist(pattern=logrus))) - -with open(filename, "rb") as f: - text = f.read() - -with gpg.Context(armor=True) as ca: - ciphertext, result, sign_result = ca.encrypt(text, recipients=keys, - 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=keys, - 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/group-key-selection.py b/lang/python/examples/howto/group-key-selection.py deleted file mode 100755 index 2e3d2ebb..00000000 --- a/lang/python/examples/howto/group-key-selection.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 -# -# 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 Licensefor more details. -# -# You should have received a copy of the GNU General Public License and the GNU -# Lesser General Public along with this program; if not, see -# . - -import gpg -import sys - -from groups import group_lists - -""" -Takes an existing group specified as a command line parameter and converts it -to a list object 'keys' as expected by the gpg module. - -Requires the groups module in this directory. -""" - -if len(sys.argv) == 2: - group = sys.argv[1] -elif len(sys.argv) == 1: - group = input("Enter the name of the group to select keys for: ") -else: - group = input("Enter the name of the group to select keys for: ") - -keys = [] -a = [] - -for i in range(len(group_lists)): - a.append(group_lists[i][0]) - -b = a.index(group) - -for i in range(len(group_lists[b][1])): - logrus = group_lists[b][1][i] - keys.append(gpg.Context().keylist(pattern=logrus)) diff --git a/lang/python/examples/howto/key-selection.py b/lang/python/examples/howto/key-selection.py deleted file mode 100755 index a007219e..00000000 --- a/lang/python/examples/howto/key-selection.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 -# -# 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 Licensefor more details. -# -# You should have received a copy of the GNU General Public License and the GNU -# Lesser General Public along with this program; if not, see -# . - -import gpg -import sys - -""" -Uses key IDs, fingerprints or other patterns as space separated input and -creates a keylist object for use by the gpg module. - -Similar to the group-key-selection.py script, but does not require an existing -group in the gpg.conf file. -""" - -if len(sys.argv) < 2: - key_ids_str = sys.argv[1:] -elif len(sys.argv) == 2: - key_ids_str = sys.argv[1] -elif len(sys.argv) == 1: - key_ids_str = input("Enter the keys to select by key ID or fingerprint: ") -else: - key_ids_str = input("Enter the keys to select by key ID or fingerprint: ") - -key_ids = key_ids_str.split() -keys = [] -for i in range(len(key_ids)): - logrus = key_ids[i] - keys.append(gpg.Context().keylist(pattern=logrus)) -- cgit v1.2.3