aboutsummaryrefslogtreecommitdiffstats
path: root/lang/python/examples/howto
diff options
context:
space:
mode:
authorBen McGinnes <[email protected]>2018-03-21 19:33:16 +0000
committerBen McGinnes <[email protected]>2018-03-21 19:33:16 +0000
commit76055dd5c7d755c6f8a242b701aeadba621fbc0f (patch)
treeb995196bcadcfa0485429d753397f61317b17292 /lang/python/examples/howto
parentcore: Do not clobber R_KEY in gpgme_get_key on error. (diff)
parentexamples: multi-key selection operations (diff)
downloadgpgme-76055dd5c7d755c6f8a242b701aeadba621fbc0f.tar.gz
gpgme-76055dd5c7d755c6f8a242b701aeadba621fbc0f.zip
Merge branch 'ben/docs/2018-03' of ssh+git://playfair.gnupg.org/git/gpgme into ben/docs/2018-03
Diffstat (limited to 'lang/python/examples/howto')
-rw-r--r--lang/python/examples/howto/README.org58
-rwxr-xr-xlang/python/examples/howto/clear-sign-file.py56
-rwxr-xr-xlang/python/examples/howto/decrypt-file.py44
-rwxr-xr-xlang/python/examples/howto/detach-sign-file.py64
-rwxr-xr-xlang/python/examples/howto/encrypt-file.py71
-rwxr-xr-xlang/python/examples/howto/encrypt-sign-file.py70
-rw-r--r--lang/python/examples/howto/groups.py50
-rwxr-xr-xlang/python/examples/howto/keycount.py42
-rwxr-xr-xlang/python/examples/howto/sign-file.py64
-rwxr-xr-xlang/python/examples/howto/verify-signatures.py64
-rwxr-xr-xlang/python/examples/howto/verify-signed-file.py61
11 files changed, 644 insertions, 0 deletions
diff --git a/lang/python/examples/howto/README.org b/lang/python/examples/howto/README.org
new file mode 100644
index 00000000..b74ae7e2
--- /dev/null
+++ b/lang/python/examples/howto/README.org
@@ -0,0 +1,58 @@
+#+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/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 <[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 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
+# <http://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/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 <[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 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
+# <http://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 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)
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 <[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 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
+# <http://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
new file mode 100755
index 00000000..ad4e1cef
--- /dev/null
+++ b/lang/python/examples/howto/encrypt-file.py
@@ -0,0 +1,71 @@
+#!/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 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
+# <http://www.gnu.org/licenses/>.
+
+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(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
new file mode 100755
index 00000000..41aaac86
--- /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 <[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 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
+# <http://www.gnu.org/licenses/>.
+
+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 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/groups.py b/lang/python/examples/howto/groups.py
new file mode 100644
index 00000000..5e7fdf60
--- /dev/null
+++ b/lang/python/examples/howto/groups.py
@@ -0,0 +1,50 @@
+# -*- 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 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
+# <http://www.gnu.org/licenses/>.
+
+import subprocess
+
+"""
+Intended for use with other scripts.
+
+Usage: from groups import group_lists
+"""
+
+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()
diff --git a/lang/python/examples/howto/keycount.py b/lang/python/examples/howto/keycount.py
new file mode 100755
index 00000000..8e25454c
--- /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 <[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 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
+# <http://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/sign-file.py b/lang/python/examples/howto/sign-file.py
new file mode 100755
index 00000000..01006df0
--- /dev/null
+++ b/lang/python/examples/howto/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 <[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 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
+# <http://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/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 <[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 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
+# <http://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
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 <[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 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
+# <http://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