2 #+TITLE: GNU Privacy Guard (GnuPG) Made Easy Python Bindings HOWTO (English)
4 #+LATEX_COMPILER: xelatex
6 #+LATEX_CLASS_OPTIONS: [12pt]
7 #+LATEX_HEADER: \usepackage{xltxtra}
8 #+LATEX_HEADER: \usepackage[margin=1in]{geometry}
9 #+LATEX_HEADER: \setmainfont[Ligatures={Common}]{Times New Roman}
10 #+LATEX_HEADER: \author{Ben McGinnes <ben@gnupg.org>}
19 | GPGME Version: | 1.13.0 |
20 | Author: | Ben McGinnes <ben@gnupg.org> |
21 | Author GPG Key: | DB4724E6FA4286C92B4E55C4321E4E2373590E5D |
22 | Language: | Australian English, British English |
23 | Language codes: | en-AU, en-GB, en |
25 This document provides basic instruction in how to use the GPGME
26 Python bindings to programmatically leverage the GPGME library.
29 ** Python 2 versus Python 3
31 :CUSTOM_ID: py2-vs-py3
34 Though the GPGME Python bindings themselves provide support for both
35 Python 2 and 3, the focus is unequivocally on Python 3 and
36 specifically from Python 3.4 and above. As a consequence all the
37 examples and instructions in this guide use Python 3 code.
39 Much of it will work with Python 2, but much of it also deals with
40 Python 3 byte literals, particularly when reading and writing data.
41 Developers concentrating on Python 2.7, and possibly even 2.6, will
42 need to make the appropriate modifications to support the older string
43 and unicode types as opposed to bytes.
45 There are multiple reasons for concentrating on Python 3; some of
46 which relate to the immediate integration of these bindings, some of
47 which relate to longer term plans for both GPGME and the python
48 bindings and some of which relate to the impending EOL period for
49 Python 2.7. Essentially, though, there is little value in tying the
50 bindings to a version of the language which is a dead end and the
51 advantages offered by Python 3 over Python 2 make handling the data
52 types with which GPGME deals considerably easier.
57 :CUSTOM_ID: howto-python3-examples
60 All of the examples found in this document can be found as Python 3
61 scripts in the =lang/python/examples/howto= directory.
66 :CUSTOM_ID: unofficial-drafts
69 In addition to shipping with each release of GPGME, there is a section
70 on locations to read or download [[#draft-editions][draft editions]] of this document from
71 at the end of it. These are unofficial versions produced in between
80 Full details of what is new are now available in the [[file:what-is-new.org][What's New]] file
81 and archives of the preceding /What's New/ sections are available in
82 the [[file:what-was-new][What Was New]] file.
85 *** New in GPGME 1·13·0
87 :CUSTOM_ID: new-stuff-1-13-0
90 See the [[file:what-is-new#new-stuff-1-13-0][What's New]] document for what is new in version 1.13.0.
93 *** New in GPGME 1·12·0
95 :CUSTOM_ID: new-stuff-1-12-0
98 See the [[file:what-was-new#new-stuff-1-12-0][What Was New]] document for what was new in version 1.12.0.
103 :CUSTOM_ID: gpgme-concepts
109 :CUSTOM_ID: gpgme-c-api
112 Unlike many modern APIs with which programmers will be more familiar
113 with these days, the GPGME API is a C API. The API is intended for
114 use by C coders who would be able to access its features by including
115 the =gpgme.h= header file with their own C source code and then access
116 its functions just as they would any other C headers.
118 This is a very effective method of gaining complete access to the API
119 and in the most efficient manner possible. It does, however, have the
120 drawback that it cannot be directly used by other languages without
121 some means of providing an interface to those languages. This is
122 where the need for bindings in various languages stems.
127 :CUSTOM_ID: gpgme-python-bindings
130 The Python bindings for GPGME provide a higher level means of
131 accessing the complete feature set of GPGME itself. It also provides
132 a more pythonic means of calling these API functions.
134 The bindings are generated dynamically with SWIG and the copy of
135 =gpgme.h= generated when GPGME is compiled.
137 This means that a version of the Python bindings is fundamentally tied
138 to the exact same version of GPGME used to generate that copy of
142 ** Difference between the Python bindings and other GnuPG Python packages
144 :CUSTOM_ID: gpgme-python-bindings-diffs
147 There have been numerous attempts to add GnuPG support to Python over
148 the years. Some of the most well known are listed here, along with
149 what differentiates them.
152 *** The python-gnupg package maintained by Vinay Sajip
154 :CUSTOM_ID: diffs-python-gnupg
157 This is arguably the most popular means of integrating GPG with
158 Python. The package utilises the =subprocess= module to implement
159 wrappers for the =gpg= and =gpg2= executables normally invoked on the
160 command line (=gpg.exe= and =gpg2.exe= on Windows).
162 The popularity of this package stemmed from its ease of use and
163 capability in providing the most commonly required features.
165 Unfortunately it has been beset by a number of security issues in the
166 past; most of which stemmed from using unsafe methods of accessing the
167 command line via the =subprocess= calls. While some effort has been
168 made over the last two to three years (as of 2018) to mitigate this,
169 particularly by no longer providing shell access through those
170 subprocess calls, the wrapper is still somewhat limited in the scope
171 of its GnuPG features coverage.
173 The python-gnupg package is available under the MIT license.
176 *** The gnupg package created and maintained by Isis Lovecruft
178 :CUSTOM_ID: diffs-isis-gnupg
181 In 2015 Isis Lovecruft from the Tor Project forked and then
182 re-implemented the python-gnupg package as just gnupg. This new
183 package also relied on subprocess to call the =gpg= or =gpg2=
184 binaries, but did so somewhat more securely.
186 The naming and version numbering selected for this package, however,
187 resulted in conflicts with the original python-gnupg and since its
188 functions were called in a different manner to python-gnupg, the
189 release of this package also resulted in a great deal of consternation
190 when people installed what they thought was an upgrade that
191 subsequently broke the code relying on it.
193 The gnupg package is available under the GNU General Public License
194 version 3.0 (or any later version).
197 *** The PyME package maintained by Martin Albrecht
199 :CUSTOM_ID: diffs-pyme
202 This package is the origin of these bindings, though they are somewhat
203 different now. For details of when and how the PyME package was
204 folded back into GPGME itself see the [[file:short-history.org][Short History]] document.[fn:1]
206 The PyME package was first released in 2002 and was also the first
207 attempt to implement a low level binding to GPGME. In doing so it
208 provided access to considerably more functionality than either the
209 =python-gnupg= or =gnupg= packages.
211 The PyME package is only available for Python 2.6 and 2.7.
213 Porting the PyME package to Python 3.4 in 2015 is what resulted in it
214 being folded into the GPGME project and the current bindings are the
215 end result of that effort.
217 The PyME package is available under the same dual licensing as GPGME
218 itself: the GNU General Public License version 2.0 (or any later
219 version) and the GNU Lesser General Public License version 2.1 (or any
223 * GPGME Python bindings installation
225 :CUSTOM_ID: gpgme-python-install
231 :CUSTOM_ID: do-not-use-pypi
234 Most third-party Python packages and modules are available and
235 distributed through the Python Package Installer, known as PyPI.
237 Due to the nature of what these bindings are and how they work, it is
238 infeasible to install the GPGME Python bindings in the same way.
240 This is because the bindings use SWIG to dynamically generate C
241 bindings against =gpgme.h= and =gpgme.h= is generated from
242 =gpgme.h.in= at compile time when GPGME is built from source. Thus to
243 include a package in PyPI which actually built correctly would require
244 either statically built libraries for every architecture bundled with
245 it or a full implementation of C for each architecture.
247 See the additional notes regarding [[#snafu-cffi][CFFI and SWIG]] at the end of this
248 section for further details.
253 :CUSTOM_ID: gpgme-python-requirements
256 The GPGME Python bindings only have three requirements:
258 1. A suitable version of Python 2 or Python 3. With Python 2 that
259 means CPython 2.7 and with Python 3 that means CPython 3.4 or
261 2. [[https://www.swig.org][SWIG]].
262 3. GPGME itself. Which also means that all of GPGME's dependencies
263 must be installed too.
266 *** Recommended Additions
268 :CUSTOM_ID: gpgme-python-recommendations
271 Though none of the following are absolute requirements, they are all
272 recommended for use with the Python bindings. In some cases these
273 recommendations refer to which version(s) of CPython to use the
274 bindings with, while others refer to third party modules which provide
275 a significant advantage in some way.
277 1. If possible, use Python 3 instead of 2.
278 2. Favour a more recent version of Python since even 3.4 is due to
279 reach EOL soon. In production systems and services, Python 3.6
280 should be robust enough to be relied on.
281 3. If possible add the following Python modules which are not part of
282 the standard library: [[http://docs.python-requests.org/en/latest/index.html][Requests]], [[https://cython.org/][Cython]], [[https://pendulum.eustace.io/][Pendulum]] and [[https://github.com/Selfnet/hkp4py][hkp4py]].
284 Chances are quite high that at least the first one and maybe two of
285 those will already be installed.
287 Note that, as with Cython, some of advanced use case scenarios will
288 bring with them additional requirements. Most of these will be fairly
289 well known and commonly installed ones, however, which are in many
290 cases likely to have already been installed on many systems or be
291 familiar to Python programmers.
296 :CUSTOM_ID: installation
299 Installing the Python bindings is effectively achieved by compiling
300 and installing GPGME itself.
302 Once SWIG is installed with Python and all the dependencies for GPGME
303 are installed you only need to confirm that the version(s) of Python
304 you want the bindings installed for are in your =$PATH=.
306 By default GPGME will attempt to install the bindings for the most
307 recent or highest version number of Python 2 and Python 3 it detects
308 in =$PATH=. It specifically checks for the =python= and =python3=
309 executables first and then checks for specific version numbers.
311 For Python 2 it checks for these executables in this order: =python=,
312 =python2= and =python2.7=.
314 For Python 3 it checks for these executables in this order: =python3=,
315 =python3.7=, =python3.6=, =python3.5= and =python3.4=.[fn:2]
317 On systems where =python= is actually =python3= and not =python2= it
318 may be possible that =python2= may be overlooked, but there have been
319 no reports of that actually occurring as yet.
321 In the three months or so since the release of Python 3.7.0 there has
322 been extensive testing and work with these bindings with no issues
323 specifically relating to the new version of Python or any of the new
324 features of either the language or the bindings. This has also been
325 the case with Python 3.7.1rc1. With that in mind and given the
326 release of Python 3.7.1 is scheduled for around the same time as GPGME
327 1.12.0, the order of preferred Python versions has been changed to
328 move Python 3.7 ahead of Python 3.6.
333 :CUSTOM_ID: install-gpgme
336 See the GPGME =README= file for details of how to install GPGME from
345 There are a few known issues with the current build process and the
346 Python bindings. For the most part these are easily addressed should
352 :CUSTOM_ID: snafu-a-swig-of-this-builds-character
355 Occasionally when installing GPGME with the Python bindings included
356 it may be observed that the =make= portion of that process induces a
357 large very number of warnings and, eventually errors which end that
358 part of the build process. Yet following that with =make check= and
359 =make install= appears to work seamlessly.
361 The cause of this is related to the way SWIG needs to be called to
362 dynamically generate the C bindings for GPGME in the first place. So
363 the entire process will always produce =lang/python/python2-gpg/= and
364 =lang/python/python3-gpg/= directories. These should contain the
365 build output generated during compilation, including the complete
366 bindings and module installed into =site-packages=.
368 Occasionally the errors in the early part or some other conflict
369 (e.g. not installing as */root/* or */su/*) may result in nothing
370 being installed to the relevant =site-packages= directory and the
371 build directory missing a lot of expected files. Even when this
372 occurs, the solution is actually quite simple and will always work.
374 That solution is simply to run the following commands as either the
375 *root* user or prepended with =sudo -H=[fn:3] in the =lang/python/=
379 /path/to/pythonX.Y setup.py build
380 /path/to/pythonX.Y setup.py build
381 /path/to/pythonX.Y setup.py install
384 Yes, the build command does need to be run twice. Yes, you still need
385 to run the potentially failing or incomplete steps during the
386 =configure=, =make= and =make install= steps with installing GPGME.
387 This is because those steps generate a lot of essential files needed,
388 both by and in order to create, the bindings (including both the
389 =setup.py= and =gpgme.h= files).
394 :CUSTOM_ID: snafu-swig-build-note
397 If specifying a selected number of languages to create bindings for,
398 try to leave Python last. Currently the majority of the other
399 language bindings are also preceding Python of either version when
400 listed alphabetically (not counting the Qt bindings).
402 If Python is set to precede one of the other languages then it is
403 possible that the errors described here may interrupt the build
404 process before generating bindings for those other languages. In
405 these cases it may be preferable to configure all preferred language
406 bindings separately with alternative =configure= steps for GPGME using
407 the =--enable-languages=$LANGUAGE= option.
409 Alternatively =make= (or =gmake=, depending on your platform) may be
410 run with the the =-k= option, which tells make to keep going even if
411 errors are encountered. In that case the failure of one language's
412 set of bindings to build should not hamper another language's bindings
416 *** Reinstalling Responsibly
418 :CUSTOM_ID: snafu-lessons-for-the-lazy
421 Regardless of whether you're installing for one version of Python or
422 several, there will come a point where reinstallation is required.
423 With most Python module installations, the installed files go into the
424 relevant site-packages directory and are then forgotten about. Then
425 the module is upgraded, the new files are copied over the old and
426 that's the end of the matter.
428 While the same is true of these bindings, there have been intermittent
429 issues observed on some platforms which have benefited significantly
430 from removing all the previous installations of the bindings before
431 installing the updated versions.
433 Removing the previous version(s) is simply a matter of changing to the
434 relevant =site-packages= directory for the version of Python in
435 question and removing the =gpg/= directory and any accompanying
436 egg-info files for that module.
438 In most cases this will require root or administration privileges on
439 the system, but the same is true of installing the module in the first
443 *** Multiple installations
445 :CUSTOM_ID: snafu-the-full-monty
448 For a variety of reasons it may be either necessary or just preferable
449 to install the bindings to alternative installed Python versions which
450 meet the requirements of these bindings.
452 On POSIX systems this will generally be most simply achieved by
453 running the manual installation commands (build, build, install) as
454 described in the previous section for each Python installation the
455 bindings need to be installed to.
457 As per the SWIG documentation: the compilers, libraries and runtime
458 used to build GPGME and the Python Bindings *must* match those used to
459 compile Python itself, including the version number(s) (at least going
460 by major version numbers and probably minor numbers too).
462 On most POSIX systems, including OS X, this will very likely be the
463 case in most, if not all, cases.
465 Note that from GPGME [[https://dev.gnupg.org/rMff6ff616aea6f59b7f2ce1176492850ecdf3851e][1.12.1]] the default installation installs to each
466 version of Python it can find first. That is that it will currently
467 install for the first copies of Python versions 2.7, 3.4, 3.5, and so on
468 up until the current dev branch that it finds. Usually this will be in the
469 same prefix as GPGME itself, but is dictated by the =$PATH= when the
470 installation is performed. The above instructions can still be
471 performed on other python installations which the installer does not
472 find, including alternative prefixes.
476 *** Won't Work With Windows
478 :CUSTOM_ID: snafu-runtime-not-funtime
481 There are semi-regular reports of Windows users having considerable
482 difficulty in installing and using the Python bindings at all. Very
483 often, possibly even always, these reports come from Cygwin users
484 and/or MinGW users and/or Msys2 users. Though not all of them have
485 been confirmed, it appears that these reports have also come from
486 people who installed Python using the Windows installer files from the
487 [[https://python.org][Python website]] (i.e. mostly MSI installers, sometimes self-extracting
490 The Windows versions of Python are not built using Cygwin, MinGW or
491 Msys2; they're built using Microsoft Visual Studio. Furthermore the
492 version used is /considerably/ more advanced than the version which
493 MinGW obtained a small number of files from many years ago in order to
494 be able to compile anything at all. Not only that, but there are
495 changes to the version of Visual Studio between some micro releases,
496 though that is is particularly the case with Python 2.7, since it has
497 been kept around far longer than it should have been.
499 There are two theoretical solutions to this issue:
501 1. Compile and install the GnuPG stack, including GPGME and the
502 Python bindings using the same version of Microsoft Visual Studio
503 used by the Python Foundation to compile the version of Python
506 If there are multiple versions of Python then this will need to be
507 done with each different version of Visual Studio used for those
510 2. Compile and install Python using the same tools used by choice,
511 such as MinGW or Msys2.
513 Do *not* use the official Windows installer for Python unless
514 following the first method.
516 In this type of situation it may even be for the best to accept that
517 there are less limitations on permissive software than free software
518 and simply opt to use a recent version of the Community Edition of
519 Microsoft Visual Studio to compile and build all of it, no matter
522 Investigations into the extent or the limitations of this issue are
525 The following table lists the version of Microsoft Visual Studio which
526 needs to be used when compiling GPGME and the Python bindings with
527 each version of the CPython binary released [[https://www.python.org/downloads/windows/][for Windows]]:
529 | CPython | Microsoft product name | runtime filename |
530 | 2.7.6 | Visual Studio 2008 | MSVCR90.DLL |
531 | 3.4.0 | Visual Studio 2010 | MSVCR100.DLL |
532 | 3.5.0 | Visual Studio 2015 | *see below* |
533 | 3.6.0 | Visual Studio 2015 | *see below* |
534 | 3.7.0 | Visual Studio 2017* | *see below* |
536 It is important to note that MingW and Msys2 ship with the Visual C
537 runtime from Microsoft Visual Studio 2005 and are thus *incompatible*
538 with all the versions of CPython which can be used with the GPGME
541 It is also important to note that from CPython 3.5 onwards, the Python
542 Foundation has adopted the reworking of the Visual C runtime which was
543 performed for Visual Studio 2015 and aimed at resolving many of these
544 kinds of issues. Much greater detail on these issues and the correct
545 file(s) to link to are available from Matthew Brett's invaluable page,
546 [[https://matthew-brett.github.io/pydagogue/python_msvc.html][Using Microsoft Visual C with Python]]. It is also worth reading the
547 Microsoft Developer Network blog post on [[http://blogs.msdn.com/b/vcblog/archive/2015/03/03/introducing-the-universal-crt.aspx][the universal CRT]] and Steve
548 Dower's blog posts on Python extensions ([[http://stevedower.id.au/blog/building-for-python-3-5][part 1]] and [[http://stevedower.id.au/blog/building-for-python-3-5-part-two][part 2]]).
550 The second of those two posts by Steve Dower contains the details of
551 specific configuration options required for compiling anything to be
552 used with official CPython releases. In addition to those
553 configuration and compiler settings to use, the versions of Visual
554 Studio prior to Visual Studio 2015 did not support 64-bit systems by
555 default. So compiling a 64-bit version of these bindings for a 64-bit
556 version of CPython 2.7 or 3.4 requires additional work.
558 In addition to the blog posts, the [[https://wiki.python.org/moin/WindowsCompilers][Windows compilers]] wiki page on the
559 CPython wiki is another essential reference on the relevant versions
560 of Visual Studio to use and the degree of compatibility with CPython
563 Eventually someone will ask why there isn't an installable binary for
564 Windows, which the GPGME of the licenses do not preclude as long as
565 the source code is available in conjunction with such a release.
567 The sheer number of versions of Visual Studio in conjunction with
568 differing configuration options depending on the target Windows
569 version and whether the architecture is 64-bit or 32-bit makes it
570 difficult to provide a correct binary installer for Windows users. At
571 the bare minimum doing so would require the GnuPG project compile ten
572 different versions of the bindings with each release; both 32-bit and
573 64-bit versions for CPython 2.7 and 3.4, with 64-bit versions for both
574 x86-64 (i.e. Intel and AMD) and ARM architectures for CPython 3.5,
575 3.6, 3.7 and later releases. That's the bare *minimum*, it'd probably
578 Additionally, with only a binary installation used in conjunction with
579 the CPython installer from =python.org= the advanced options available
580 which utilise [[#cython][Cython]] will not be able to be used at all. Cython
581 depends on being able to compile the C code it generates and that too
582 would need to utilise a matching runtime to both the installed version
583 of CPython and these bindings in order to work with the bindings.
585 Considering all of that, what do we recommend?
587 1. Use a recent version of CPython; at least 3.5, but ideally 3.6 or
590 2. Use Visual Studio 2015 or the standalone build tools for Visual
591 Studio 2017 (or later).
593 3. Compile both CPython and GPGME with these bindings using the tools
596 4. Ignore MingW, Msys2 and the official CPython binary installers.
598 5. Be thankful the answer to this question wasn't simply to say
599 something like, “install Linux” or “install FreeBSD” (or even
603 *** CFFI is the Best™ and GPGME should use it instead of SWIG
605 :CUSTOM_ID: snafu-cffi
608 There are many reasons for favouring [[https://cffi.readthedocs.io/en/latest/overview.html][CFFI]] and proponents of it are
609 quite happy to repeat these things as if all it would take to switch
610 from SWIG to CFFI is repeating that list as if it were a new concept.
612 The fact is that there are things which Python's CFFI implementation
613 cannot handle in the GPGME C code. Beyond that there are features of
614 SWIG which are simply not available with CFFI at all. SWIG generates
615 the bindings to Python using the =gpgme.h= file, but that file is not
616 a single version shipped with each release, it too is generated when
619 CFFI is currently unable to adapt to such a potentially mutable
620 codebase. If there were some means of applying SWIG's dynamic code
621 generation to produce the Python/CFFI API modes of accessing the GPGME
622 libraries (or the source source code directly), but such a thing does
623 not exist yet either and it currently appears that work is needed in
624 at least one of CFFI's dependencies before any of this can be
627 So if you're a massive fan of CFFI; that's great, but if you want this
628 project to switch to CFFI then rather than just insisting that it
629 should, I'd suggest you volunteer to bring CFFI up to the level this
632 If you're actually seriously considering doing so, then I'd suggest
633 taking the =gpgme-tool.c= file in the GPGME =src/= directory and
634 getting that to work with any of the CFFI API methods (not the ABI
635 methods, they'll work with pretty much anything). When you start
636 running into trouble with "ifdefs" then you'll know what sort of
637 things are lacking. That doesn't even take into account the amount of
638 work saved via SWIG's code generation techniques either.
641 *** Virtualised Environments
643 :CUSTOM_ID: snafu-venv
646 It is fairly common practice amongst Python developers to, as much as
647 possible, use packages like virtualenv to keep various things that are
648 to be installed from interfering with each other. Given how much of
649 the GPGME bindings is often at odds with the usual pythonic way of
650 doing things, it stands to reason that this would be called into
653 As it happens the answer as to whether or not the bindings can be used
654 with virtualenv, the answer is both yes and no.
656 In general we recommend installing to the relevant path and matching
657 prefix of GPGME itself. Which means that when GPGME, and ideally the
658 rest of the GnuPG stack, is installed to a prefix like =/usr/local= or
659 =/opt/local= then the bindings would need to be installed to the main
660 Python installation and not a virtualised abstraction. Attempts to
661 separate the two in the past have been known to cause weird and
662 intermittent errors ranging from minor annoyances to complete failures
663 in the build process.
665 As a consequence we only recommend building with and installing to the
666 main Python installations within the same prefix as GPGME is installed
667 to or which are found by GPGME's configuration stage immediately prior
668 to running the make commands. Which is exactly what the compiling and
669 installing process of GPGME does by default.
671 Once that is done, however, it appears that a copy of the compiled
672 module may be installed into a virtualenv of the same major and minor
673 version matching the build. Alternatively it is possible to utilise a
674 =sites.pth= file in the =site-packages/= directory of a virtualenv
675 installation, which links back to the system installations
676 corresponding directory in order to import anything installed system
677 wide. This may or may not be appropriate on a case by case basis.
679 Though extensive testing of either of these options is not yet
680 complete, preliminary testing of them indicates that both are viable
681 as long as the main installation is complete. Which means that
682 certain other options normally restricted to virtual environments are
683 also available, including integration with pythonic test suites
684 (e.g. [[https://docs.pytest.org/en/latest/index.html][pytest]]) and other large projects.
686 That said, it is worth reiterating the warning regarding non-standard
687 installations. If one were to attempt to install the bindings only to
688 a virtual environment without somehow also including the full GnuPG
689 stack (or enough of it as to include GPGME) then it is highly likely
690 that errors would be encountered at some point and more than a little
691 likely that the build process itself would break.
693 If a degree of separation from the main operating system is still
694 required in spite of these warnings, then consider other forms of
695 virtualisation. Either a virtual machine (e.g. [[https://www.virtualbox.org/][VirtualBox]]), a
696 hardware emulation layer (e.g. [[https://www.qemu.org/][QEMU]]) or an application container
697 (e.g. [[https://www.docker.com/why-docker][Docker]]).
699 Finally it should be noted that the limited tests conducted thus far
700 have been using the =virtualenv= command in a new directory to create
701 the virtual python environment. As opposed to the standard =python3
702 -m venv= and it is possible that this will make a difference depending
703 on the system and version of Python in use. Another option is to run
704 the command =python3 -m virtualenv /path/to/install/virtual/thingy=
708 *** Post installation
710 :CUSTOM_ID: snafu-docs
713 Following installation it is recommended to move the
714 =post_installer.py= script from the =lang/python/examples/howto/=
715 directory to the =lang/python/= directory and run it. This will fix
716 or restore files needed by Sphinx which may be removed during a
717 distribution build for release. It will also generate reST files from
718 Org mode files with Pandoc and generate Texinfo files from Org mode
719 files with GNU Emacs and Org mode (in batch mode). Additionally it
720 will fix the UTF-8 declaration line in the Texinfo files (Emacs
721 expects "UTF-8" to be "utf-8").
726 :CUSTOM_ID: howto-fund-a-mental
729 Before we can get to the fun stuff, there are a few matters regarding
730 GPGME's design which hold true whether you're dealing with the C code
731 directly or these Python bindings.
736 :CUSTOM_ID: no-rest-for-the-wicked
739 The first part of which is or will be fairly blatantly obvious upon
740 viewing the first example, but it's worth reiterating anyway. That
741 being that this API is /*not*/ a REST API. Nor indeed could it ever
744 Most, if not all, Python programmers (and not just Python programmers)
745 know how easy it is to work with a RESTful API. In fact they've
746 become so popular that many other APIs attempt to emulate REST-like
747 behaviour as much as they are able. Right down to the use of JSON
748 formatted output to facilitate the use of their API without having to
751 This API does not do that. It would not be able to do that and also
752 provide access to the entire C API on which it's built. It does,
753 however, provide a very pythonic interface on top of the direct
754 bindings and it's this pythonic layer that this HOWTO deals with.
759 :CUSTOM_ID: howto-get-context
762 One of the reasons which prevents this API from being RESTful is that
763 most operations require more than one instruction to the API to
764 perform the task. Sure, there are certain functions which can be
765 performed simultaneously, particularly if the result known or strongly
766 anticipated (e.g. selecting and encrypting to a key known to be in the
769 There are many more, however, which cannot be manipulated so readily:
770 they must be performed in a specific sequence and the result of one
771 operation has a direct bearing on the outcome of subsequent
772 operations. Not merely by generating an error either.
774 When dealing with this type of persistent state on the web, full of
775 both the RESTful and REST-like, it's most commonly referred to as a
776 session. In GPGME, however, it is called a context and every
777 operation type has one.
782 :CUSTOM_ID: howto-keys
788 :CUSTOM_ID: howto-keys-selection
791 Selecting keys to encrypt to or to sign with will be a common
792 occurrence when working with GPGMe and the means available for doing
795 They do depend on utilising a Context; however once the data is
796 recorded in another variable, that Context does not need to be the
797 same one which subsequent operations are performed.
799 The easiest way to select a specific key is by searching for that
800 key's key ID or fingerprint, preferably the full fingerprint without
801 any spaces in it. A long key ID will probably be okay, but is not
802 advised and short key IDs are already a problem with some being
803 generated to match specific patterns. It does not matter whether the
804 pattern is upper or lower case.
806 So this is the best method:
808 #+BEGIN_SRC python -i
811 k = gpg.Context().keylist(pattern="258E88DCBD3CD44D8E7AB43F6ECB6AF0DEADBEEF")
815 This is passable and very likely to be common:
817 #+BEGIN_SRC python -i
820 k = gpg.Context().keylist(pattern="0x6ECB6AF0DEADBEEF")
824 And this is a really bad idea:
826 #+BEGIN_SRC python -i
829 k = gpg.Context().keylist(pattern="0xDEADBEEF")
833 Alternatively it may be that the intention is to create a list of keys
834 which all match a particular search string. For instance all the
835 addresses at a particular domain, like this:
837 #+BEGIN_SRC python -i
840 ncsc = gpg.Context().keylist(pattern="ncsc.mil")
847 :CUSTOM_ID: howto-keys-counting
850 Counting the number of keys in your public keybox (=pubring.kbx=), the
851 format which has superseded the old keyring format (=pubring.gpg= and
852 =secring.gpg=), or the number of secret keys is a very simple task.
854 #+BEGIN_SRC python -i
858 seckeys = c.keylist(pattern=None, secret=True)
859 pubkeys = c.keylist(pattern=None, secret=False)
861 seclist = list(seckeys)
862 secnum = len(seclist)
864 publist = list(pubkeys)
865 pubnum = len(publist)
868 Number of secret keys: {0}
869 Number of public keys: {1}
870 """.format(secnum, pubnum))
873 NOTE: The [[#cython][Cython]] introduction in the [[#advanced-use][Advanced and Experimental]]
874 section uses this same key counting code with Cython to demonstrate
875 some areas where Cython can improve performance even with the
876 bindings. Users with large public keyrings or keyboxes, for instance,
877 should consider these options if they are comfortable with using
883 :CUSTOM_ID: howto-get-key
886 An alternative method of getting a single key via its fingerprint is
887 available directly within a Context with =Context().get_key=. This is
888 the preferred method of selecting a key in order to modify it, sign or
889 certify it and for obtaining relevant data about a single key as a
890 part of other functions; when verifying a signature made by that key,
893 By default this method will select public keys, but it can select
896 This first example demonstrates selecting the current key of Werner
897 Koch, which is due to expire at the end of 2018:
899 #+BEGIN_SRC python -i
902 fingerprint = "80615870F5BAD690333686D0F2AD85AC1E42B367"
903 key = gpg.Context().get_key(fingerprint)
906 Whereas this example demonstrates selecting the author's current key
907 with the =secret= key word argument set to =True=:
909 #+BEGIN_SRC python -i
912 fingerprint = "DB4724E6FA4286C92B4E55C4321E4E2373590E5D"
913 key = gpg.Context().get_key(fingerprint, secret=True)
916 It is, of course, quite possible to select expired, disabled and
917 revoked keys with this function, but only to effectively display
918 information about those keys.
920 It is also possible to use both unicode or string literals and byte
921 literals with the fingerprint when getting a key in this way.
926 :CUSTOM_ID: howto-import-key
929 Importing keys is possible with the =key_import()= method and takes
930 one argument which is a bytes literal object containing either the
931 binary or ASCII armoured key data for one or more keys.
933 The following example retrieves one or more keys from the SKS
934 keyservers via the web using the requests module. Since requests
935 returns the content as a bytes literal object, we can then use that
936 directly to import the resulting data into our keybox.
938 #+BEGIN_SRC python -i
944 url = "https://sks-keyservers.net/pks/lookup"
945 pattern = input("Enter the pattern to search for key or user IDs: ")
946 payload = {"op": "get", "search": pattern}
948 r = requests.get(url, verify=True, params=payload)
949 result = c.key_import(r.content)
951 if result is not None and hasattr(result, "considered") is False:
953 elif result is not None and hasattr(result, "considered") is True:
954 num_keys = len(result.imports)
955 new_revs = result.new_revocations
956 new_sigs = result.new_signatures
957 new_subs = result.new_sub_keys
958 new_uids = result.new_user_ids
959 new_scrt = result.secret_imported
960 nochange = result.unchanged
962 The total number of keys considered for import was: {0}
964 Number of keys revoked: {1}
965 Number of new signatures: {2}
966 Number of new subkeys: {3}
967 Number of new user IDs: {4}
968 Number of new secret keys: {5}
969 Number of unchanged keys: {6}
971 The key IDs for all considered keys were:
972 """.format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt,
974 for i in range(num_keys):
975 print("{0}\n".format(result.imports[i].fpr))
980 NOTE: When searching for a key ID of any length or a fingerprint
981 (without spaces), the SKS servers require the the leading =0x=
982 indicative of hexadecimal be included. Also note that the old short
983 key IDs (e.g. =0xDEADBEEF=) should no longer be used due to the
984 relative ease by which such key IDs can be reproduced, as demonstrated
985 by the Evil32 Project in 2014 (which was subsequently exploited in
988 Testing for whether a string in any given search is or may be a
989 hexadecimal value which may be missing the leading =0x= is a simple
990 matter of using a try/except statement which attempts to convert the
991 string as hex to an integer and then back to hex; then using that to
992 search with. Raising a ValueError simply results in treating the
993 string as a string. This is the method and logic utilised in the
994 =import-keys-hkp.py= script (see below).
997 *** Working with ProtonMail
999 :CUSTOM_ID: import-protonmail
1002 Here is a variation on the example above which checks the constrained
1003 ProtonMail keyserver for ProtonMail public keys.
1005 #+BEGIN_SRC python -i
1011 This script searches the ProtonMail key server for the specified key and
1015 c = gpg.Context(armor=True)
1016 url = "https://api.protonmail.ch/pks/lookup"
1019 if len(sys.argv) >= 2:
1020 keyterm = sys.argv[1]
1022 keyterm = input("Enter the key ID, UID or search string: ")
1024 if keyterm.count("@") == 2 and keyterm.startswith("@") is True:
1025 ksearch.append(keyterm[1:])
1026 ksearch.append(keyterm[1:])
1027 ksearch.append(keyterm[1:])
1028 elif keyterm.count("@") == 1 and keyterm.startswith("@") is True:
1029 ksearch.append("{0}@protonmail.com".format(keyterm[1:]))
1030 ksearch.append("{0}@protonmail.ch".format(keyterm[1:]))
1031 ksearch.append("{0}@pm.me".format(keyterm[1:]))
1032 elif keyterm.count("@") == 0:
1033 ksearch.append("{0}@protonmail.com".format(keyterm))
1034 ksearch.append("{0}@protonmail.ch".format(keyterm))
1035 ksearch.append("{0}@pm.me".format(keyterm))
1036 elif keyterm.count("@") == 2 and keyterm.startswith("@") is False:
1037 uidlist = keyterm.split("@")
1039 ksearch.append("{0}@protonmail.com".format(uid))
1040 ksearch.append("{0}@protonmail.ch".format(uid))
1041 ksearch.append("{0}@pm.me".format(uid))
1042 elif keyterm.count("@") > 2:
1043 uidlist = keyterm.split("@")
1045 ksearch.append("{0}@protonmail.com".format(uid))
1046 ksearch.append("{0}@protonmail.ch".format(uid))
1047 ksearch.append("{0}@pm.me".format(uid))
1049 ksearch.append(keyterm)
1052 payload = {"op": "get", "search": k}
1054 r = requests.get(url, verify=True, params=payload)
1056 result = c.key_import(r.content)
1059 except Exception as e:
1062 if result is not None and hasattr(result, "considered") is False:
1063 print("{0} for {1}".format(result.decode(), k))
1064 elif result is not None and hasattr(result, "considered") is True:
1065 num_keys = len(result.imports)
1066 new_revs = result.new_revocations
1067 new_sigs = result.new_signatures
1068 new_subs = result.new_sub_keys
1069 new_uids = result.new_user_ids
1070 new_scrt = result.secret_imported
1071 nochange = result.unchanged
1073 The total number of keys considered for import was: {0}
1075 With UIDs wholely or partially matching the following string:
1079 Number of keys revoked: {2}
1080 Number of new signatures: {3}
1081 Number of new subkeys: {4}
1082 Number of new user IDs: {5}
1083 Number of new secret keys: {6}
1084 Number of unchanged keys: {7}
1086 The key IDs for all considered keys were:
1087 """.format(num_keys, k, new_revs, new_sigs, new_subs, new_uids, new_scrt,
1089 for i in range(num_keys):
1090 print(result.imports[i].fpr)
1092 elif result is None:
1096 Both the above example, [[../examples/howto/pmkey-import.py][pmkey-import.py]], and a version which prompts
1097 for an alternative GnuPG home directory, [[../examples/howto/pmkey-import-alt.py][pmkey-import-alt.py]], are
1098 available with the other examples and are executable scripts.
1100 Note that while the ProtonMail servers are based on the SKS servers,
1101 their server is related more to their API and is not feature complete
1102 by comparison to the servers in the SKS pool. One notable difference
1103 being that the ProtonMail server does not permit non ProtonMail users
1104 to update their own keys, which could be a vector for attacking
1105 ProtonMail users who may not receive a key's revocation if it had been
1109 *** Importing with HKP for Python
1111 :CUSTOM_ID: import-hkp4py
1114 Performing the same tasks with the [[https://github.com/Selfnet/hkp4py][hkp4py module]] (available via PyPI)
1115 is not too much different, but does provide a number of options of
1116 benefit to end users. Not least of which being the ability to perform
1117 some checks on a key before importing it or not. For instance it may
1118 be the policy of a site or project to only import keys which have not
1119 been revoked. The hkp4py module permits such checks prior to the
1120 importing of the keys found.
1122 #+BEGIN_SRC python -i
1128 server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
1132 if len(sys.argv) > 2:
1133 pattern = " ".join(sys.argv[1:])
1134 elif len(sys.argv) == 2:
1135 pattern = sys.argv[1]
1137 pattern = input("Enter the pattern to search for keys or user IDs: ")
1140 if pattern is not None:
1142 key = server.search(hex(int(pattern, 16)))
1144 except ValueError as ve:
1145 key = server.search(pattern)
1152 fob = server.search(pattern)
1162 for logrus in pattern.split():
1164 key = server.search(hex(int(logrus, 16)))
1166 except ValueError as ve:
1167 key = server.search(logrus)
1174 fob = server.search(logrus)
1187 import_result = c.key_import(key.key_blob)
1188 results.append(import_result)
1190 for result in results:
1191 if result is not None and hasattr(result, "considered") is False:
1193 elif result is not None and hasattr(result, "considered") is True:
1194 num_keys = len(result.imports)
1195 new_revs = result.new_revocations
1196 new_sigs = result.new_signatures
1197 new_subs = result.new_sub_keys
1198 new_uids = result.new_user_ids
1199 new_scrt = result.secret_imported
1200 nochange = result.unchanged
1202 The total number of keys considered for import was: {0}
1204 Number of keys revoked: {1}
1205 Number of new signatures: {2}
1206 Number of new subkeys: {3}
1207 Number of new user IDs: {4}
1208 Number of new secret keys: {5}
1209 Number of unchanged keys: {6}
1211 The key IDs for all considered keys were:
1212 """.format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt,
1214 for i in range(num_keys):
1215 print(result.imports[i].fpr)
1221 Since the hkp4py module handles multiple keys just as effectively as
1222 one (=keys= is a list of responses per matching key), the example
1223 above is able to do a little bit more with the returned data before
1224 anything is actually imported.
1227 *** Importing from ProtonMail with HKP for Python
1229 :CUSTOM_ID: import-protonmail-hkp4py
1232 Though this can provide certain benefits even when working with
1233 ProtonMail, the scope is somewhat constrained there due to the
1234 limitations of the ProtonMail keyserver.
1236 For instance, searching the SKS keyserver pool for the term "gnupg"
1237 produces hundreds of results from any time the word appears in any
1238 part of a user ID. Performing the same search on the ProtonMail
1239 keyserver returns zero results, even though there are at least two
1240 test accounts which include it as part of the username.
1242 The cause of this discrepancy is the deliberate configuration of that
1243 server by ProtonMail to require an exact match of the full email
1244 address of the ProtonMail user whose key is being requested.
1245 Presumably this is intended to reduce breaches of privacy of their
1246 users as an email address must already be known before a key for that
1247 address can be obtained.
1250 **** Import from ProtonMail via HKP for Python Example no. 1
1252 :CUSTOM_ID: import-hkp4py-pm1
1255 The following script is available with the rest of the examples under
1256 the somewhat less than original name, =pmkey-import-hkp.py=.
1258 #+BEGIN_SRC python -i
1265 This script searches the ProtonMail key server for the specified key and
1268 Usage: pmkey-import-hkp.py [search strings]
1271 c = gpg.Context(armor=True)
1272 server = hkp4py.KeyServer("hkps://api.protonmail.ch")
1280 if len(sys.argv) > 2:
1281 keyterms = sys.argv[1:]
1282 elif len(sys.argv) == 2:
1283 keyterm = sys.argv[1]
1284 keyterms.append(keyterm)
1286 key_term = input("Enter the key ID, UID or search string: ")
1287 keyterms = key_term.split()
1289 for keyterm in keyterms:
1290 if keyterm.count("@") == 2 and keyterm.startswith("@") is True:
1291 ksearch.append(keyterm[1:])
1292 ksearch.append(keyterm[1:])
1293 ksearch.append(keyterm[1:])
1294 elif keyterm.count("@") == 1 and keyterm.startswith("@") is True:
1295 ksearch.append("{0}@protonmail.com".format(keyterm[1:]))
1296 ksearch.append("{0}@protonmail.ch".format(keyterm[1:]))
1297 ksearch.append("{0}@pm.me".format(keyterm[1:]))
1298 elif keyterm.count("@") == 0:
1299 ksearch.append("{0}@protonmail.com".format(keyterm))
1300 ksearch.append("{0}@protonmail.ch".format(keyterm))
1301 ksearch.append("{0}@pm.me".format(keyterm))
1302 elif keyterm.count("@") == 2 and keyterm.startswith("@") is False:
1303 uidlist = keyterm.split("@")
1305 ksearch.append("{0}@protonmail.com".format(uid))
1306 ksearch.append("{0}@protonmail.ch".format(uid))
1307 ksearch.append("{0}@pm.me".format(uid))
1308 elif keyterm.count("@") > 2:
1309 uidlist = keyterm.split("@")
1311 ksearch.append("{0}@protonmail.com".format(uid))
1312 ksearch.append("{0}@protonmail.ch".format(uid))
1313 ksearch.append("{0}@pm.me".format(uid))
1315 ksearch.append(keyterm)
1318 print("Checking for key for: {0}".format(k))
1320 keys = server.search(k)
1321 if isinstance(keys, list) is True:
1325 import_result = c.key_import(key.key_blob)
1326 except Exception as e:
1327 import_result = c.key_import(key.key)
1329 paradox.append(keys)
1330 import_result = None
1331 except Exception as e:
1332 import_result = None
1333 results.append(import_result)
1335 for result in results:
1336 if result is not None and hasattr(result, "considered") is False:
1337 print("{0} for {1}".format(result.decode(), k))
1338 elif result is not None and hasattr(result, "considered") is True:
1339 num_keys = len(result.imports)
1340 new_revs = result.new_revocations
1341 new_sigs = result.new_signatures
1342 new_subs = result.new_sub_keys
1343 new_uids = result.new_user_ids
1344 new_scrt = result.secret_imported
1345 nochange = result.unchanged
1347 The total number of keys considered for import was: {0}
1349 With UIDs wholely or partially matching the following string:
1353 Number of keys revoked: {2}
1354 Number of new signatures: {3}
1355 Number of new subkeys: {4}
1356 Number of new user IDs: {5}
1357 Number of new secret keys: {6}
1358 Number of unchanged keys: {7}
1360 The key IDs for all considered keys were:
1361 """.format(num_keys, k, new_revs, new_sigs, new_subs, new_uids, new_scrt,
1363 for i in range(num_keys):
1364 print(result.imports[i].fpr)
1366 elif result is None:
1371 **** Import from ProtonMail via HKP for Python Example no. 2
1373 :CUSTOM_ID: import-hkp4py-pm2
1376 Like its counterpart above, this script can also be found with the
1377 rest of the examples, by the name pmkey-import-hkp-alt.py.
1379 With this script a modicum of effort has been made to treat anything
1380 passed as a =homedir= which either does not exist or which is not a
1381 directory, as also being a pssible user ID to check for. It's not
1382 guaranteed to pick up on all such cases, but it should cover most of
1385 #+BEGIN_SRC python -i
1392 This script searches the ProtonMail key server for the specified key and
1393 imports it. Optionally enables specifying a different GnuPG home directory.
1395 Usage: pmkey-import-hkp.py [homedir] [search string]
1396 or: pmkey-import-hkp.py [search string]
1399 c = gpg.Context(armor=True)
1400 server = hkp4py.KeyServer("hkps://api.protonmail.ch")
1408 if len(sys.argv) > 3:
1409 homedir = sys.argv[1]
1410 keyterms = sys.argv[2:]
1411 elif len(sys.argv) == 3:
1412 homedir = sys.argv[1]
1413 keyterm = sys.argv[2]
1414 keyterms.append(keyterm)
1415 elif len(sys.argv) == 2:
1417 keyterm = sys.argv[1]
1418 keyterms.append(keyterm)
1420 keyterm = input("Enter the key ID, UID or search string: ")
1421 homedir = input("Enter the GPG configuration directory path (optional): ")
1422 keyterms.append(keyterm)
1424 if len(homedir) == 0:
1428 if homedir is not None:
1429 if homedir.startswith("~"):
1430 if os.path.exists(os.path.expanduser(homedir)) is True:
1431 if os.path.isdir(os.path.expanduser(homedir)) is True:
1432 c.home_dir = os.path.realpath(os.path.expanduser(homedir))
1437 elif os.path.exists(os.path.realpath(homedir)) is True:
1438 if os.path.isdir(os.path.realpath(homedir)) is True:
1439 c.home_dir = os.path.realpath(homedir)
1445 # First check to see if the homedir really is a homedir and if not, treat it as
1447 if homeless is True:
1448 keyterms.append(homedir)
1453 for keyterm in keyterms:
1454 if keyterm.count("@") == 2 and keyterm.startswith("@") is True:
1455 ksearch.append(keyterm[1:])
1456 ksearch.append(keyterm[1:])
1457 ksearch.append(keyterm[1:])
1458 elif keyterm.count("@") == 1 and keyterm.startswith("@") is True:
1459 ksearch.append("{0}@protonmail.com".format(keyterm[1:]))
1460 ksearch.append("{0}@protonmail.ch".format(keyterm[1:]))
1461 ksearch.append("{0}@pm.me".format(keyterm[1:]))
1462 elif keyterm.count("@") == 0:
1463 ksearch.append("{0}@protonmail.com".format(keyterm))
1464 ksearch.append("{0}@protonmail.ch".format(keyterm))
1465 ksearch.append("{0}@pm.me".format(keyterm))
1466 elif keyterm.count("@") == 2 and keyterm.startswith("@") is False:
1467 uidlist = keyterm.split("@")
1469 ksearch.append("{0}@protonmail.com".format(uid))
1470 ksearch.append("{0}@protonmail.ch".format(uid))
1471 ksearch.append("{0}@pm.me".format(uid))
1472 elif keyterm.count("@") > 2:
1473 uidlist = keyterm.split("@")
1475 ksearch.append("{0}@protonmail.com".format(uid))
1476 ksearch.append("{0}@protonmail.ch".format(uid))
1477 ksearch.append("{0}@pm.me".format(uid))
1479 ksearch.append(keyterm)
1482 print("Checking for key for: {0}".format(k))
1484 keys = server.search(k)
1485 if isinstance(keys, list) is True:
1489 import_result = c.key_import(key.key_blob)
1490 except Exception as e:
1491 import_result = c.key_import(key.key)
1493 paradox.append(keys)
1494 import_result = None
1495 except Exception as e:
1496 import_result = None
1497 results.append(import_result)
1499 for result in results:
1500 if result is not None and hasattr(result, "considered") is False:
1501 print("{0} for {1}".format(result.decode(), k))
1502 elif result is not None and hasattr(result, "considered") is True:
1503 num_keys = len(result.imports)
1504 new_revs = result.new_revocations
1505 new_sigs = result.new_signatures
1506 new_subs = result.new_sub_keys
1507 new_uids = result.new_user_ids
1508 new_scrt = result.secret_imported
1509 nochange = result.unchanged
1511 The total number of keys considered for import was: {0}
1513 With UIDs wholely or partially matching the following string:
1517 Number of keys revoked: {2}
1518 Number of new signatures: {3}
1519 Number of new subkeys: {4}
1520 Number of new user IDs: {5}
1521 Number of new secret keys: {6}
1522 Number of unchanged keys: {7}
1524 The key IDs for all considered keys were:
1525 """.format(num_keys, k, new_revs, new_sigs, new_subs, new_uids, new_scrt,
1527 for i in range(num_keys):
1528 print(result.imports[i].fpr)
1530 elif result is None:
1537 :CUSTOM_ID: howto-export-key
1540 Exporting keys remains a reasonably simple task, but has been
1541 separated into three different functions for the OpenPGP cryptographic
1542 engine. Two of those functions are for exporting public keys and the
1543 third is for exporting secret keys.
1546 *** Exporting public keys
1548 :CUSTOM_ID: howto-export-public-key
1551 There are two methods of exporting public keys, both of which are very
1552 similar to the other. The default method, =key_export()=, will export
1553 a public key or keys matching a specified pattern as normal. The
1554 alternative, the =key_export_minimal()= method, will do the same thing
1555 except producing a minimised output with extra signatures and third
1556 party signatures or certifications removed.
1558 #+BEGIN_SRC python -i
1564 This script exports one or more public keys.
1567 c = gpg.Context(armor=True)
1569 if len(sys.argv) >= 4:
1570 keyfile = sys.argv[1]
1571 logrus = sys.argv[2]
1572 homedir = sys.argv[3]
1573 elif len(sys.argv) == 3:
1574 keyfile = sys.argv[1]
1575 logrus = sys.argv[2]
1576 homedir = input("Enter the GPG configuration directory path (optional): ")
1577 elif len(sys.argv) == 2:
1578 keyfile = sys.argv[1]
1579 logrus = input("Enter the UID matching the key(s) to export: ")
1580 homedir = input("Enter the GPG configuration directory path (optional): ")
1582 keyfile = input("Enter the path and filename to save the secret key to: ")
1583 logrus = input("Enter the UID matching the key(s) to export: ")
1584 homedir = input("Enter the GPG configuration directory path (optional): ")
1586 if homedir.startswith("~"):
1587 if os.path.exists(os.path.expanduser(homedir)) is True:
1588 c.home_dir = os.path.expanduser(homedir)
1591 elif os.path.exists(homedir) is True:
1592 c.home_dir = homedir
1597 result = c.key_export(pattern=logrus)
1599 result = c.key_export(pattern=None)
1601 if result is not None:
1602 with open(keyfile, "wb") as f:
1608 It should be noted that the result will only return =None= when a
1609 search pattern has been entered, but has not matched any keys. When
1610 the search pattern itself is set to =None= this triggers the exporting
1611 of the entire public keybox.
1613 #+BEGIN_SRC python -i
1619 This script exports one or more public keys in minimised form.
1622 c = gpg.Context(armor=True)
1624 if len(sys.argv) >= 4:
1625 keyfile = sys.argv[1]
1626 logrus = sys.argv[2]
1627 homedir = sys.argv[3]
1628 elif len(sys.argv) == 3:
1629 keyfile = sys.argv[1]
1630 logrus = sys.argv[2]
1631 homedir = input("Enter the GPG configuration directory path (optional): ")
1632 elif len(sys.argv) == 2:
1633 keyfile = sys.argv[1]
1634 logrus = input("Enter the UID matching the key(s) to export: ")
1635 homedir = input("Enter the GPG configuration directory path (optional): ")
1637 keyfile = input("Enter the path and filename to save the secret key to: ")
1638 logrus = input("Enter the UID matching the key(s) to export: ")
1639 homedir = input("Enter the GPG configuration directory path (optional): ")
1641 if homedir.startswith("~"):
1642 if os.path.exists(os.path.expanduser(homedir)) is True:
1643 c.home_dir = os.path.expanduser(homedir)
1646 elif os.path.exists(homedir) is True:
1647 c.home_dir = homedir
1652 result = c.key_export_minimal(pattern=logrus)
1654 result = c.key_export_minimal(pattern=None)
1656 if result is not None:
1657 with open(keyfile, "wb") as f:
1664 *** Exporting secret keys
1666 :CUSTOM_ID: howto-export-secret-key
1669 Exporting secret keys is, functionally, very similar to exporting
1670 public keys; save for the invocation of =pinentry= via =gpg-agent= in
1671 order to securely enter the key's passphrase and authorise the export.
1673 The following example exports the secret key to a file which is then
1674 set with the same permissions as the output files created by the
1675 command line secret key export options.
1677 #+BEGIN_SRC python -i
1684 This script exports one or more secret keys.
1686 The gpg-agent and pinentry are invoked to authorise the export.
1689 c = gpg.Context(armor=True)
1691 if len(sys.argv) >= 4:
1692 keyfile = sys.argv[1]
1693 logrus = sys.argv[2]
1694 homedir = sys.argv[3]
1695 elif len(sys.argv) == 3:
1696 keyfile = sys.argv[1]
1697 logrus = sys.argv[2]
1698 homedir = input("Enter the GPG configuration directory path (optional): ")
1699 elif len(sys.argv) == 2:
1700 keyfile = sys.argv[1]
1701 logrus = input("Enter the UID matching the secret key(s) to export: ")
1702 homedir = input("Enter the GPG configuration directory path (optional): ")
1704 keyfile = input("Enter the path and filename to save the secret key to: ")
1705 logrus = input("Enter the UID matching the secret key(s) to export: ")
1706 homedir = input("Enter the GPG configuration directory path (optional): ")
1708 if len(homedir) == 0:
1710 elif homedir.startswith("~"):
1711 userdir = os.path.expanduser(homedir)
1712 if os.path.exists(userdir) is True:
1713 homedir = os.path.realpath(userdir)
1717 homedir = os.path.realpath(homedir)
1719 if os.path.exists(homedir) is False:
1722 if os.path.isdir(homedir) is False:
1727 if homedir is not None:
1728 c.home_dir = homedir
1733 result = c.key_export_secret(pattern=logrus)
1735 result = c.key_export_secret(pattern=None)
1737 if result is not None:
1738 with open(keyfile, "wb") as f:
1740 os.chmod(keyfile, 0o600)
1745 Alternatively the approach of the following script can be used. This
1746 longer example saves the exported secret key(s) in files in the GnuPG
1747 home directory, in addition to setting the file permissions as only
1748 readable and writable by the user. It also exports the secret key(s)
1749 twice in order to output both GPG binary (=.gpg=) and ASCII armoured
1752 #+BEGIN_SRC python -i
1760 This script exports one or more secret keys as both ASCII armored and binary
1761 file formats, saved in files within the user's GPG home directory.
1763 The gpg-agent and pinentry are invoked to authorise the export.
1766 if sys.platform == "win32":
1767 gpgconfcmd = "gpgconf.exe --list-dirs homedir"
1769 gpgconfcmd = "gpgconf --list-dirs homedir"
1771 a = gpg.Context(armor=True)
1775 if len(sys.argv) >= 4:
1776 keyfile = sys.argv[1]
1777 logrus = sys.argv[2]
1778 homedir = sys.argv[3]
1779 elif len(sys.argv) == 3:
1780 keyfile = sys.argv[1]
1781 logrus = sys.argv[2]
1782 homedir = input("Enter the GPG configuration directory path (optional): ")
1783 elif len(sys.argv) == 2:
1784 keyfile = sys.argv[1]
1785 logrus = input("Enter the UID matching the secret key(s) to export: ")
1786 homedir = input("Enter the GPG configuration directory path (optional): ")
1788 keyfile = input("Enter the filename to save the secret key to: ")
1789 logrus = input("Enter the UID matching the secret key(s) to export: ")
1790 homedir = input("Enter the GPG configuration directory path (optional): ")
1792 if len(homedir) == 0:
1794 elif homedir.startswith("~"):
1795 userdir = os.path.expanduser(homedir)
1796 if os.path.exists(userdir) is True:
1797 homedir = os.path.realpath(userdir)
1801 homedir = os.path.realpath(homedir)
1803 if os.path.exists(homedir) is False:
1806 if os.path.isdir(homedir) is False:
1811 if homedir is not None:
1812 c.home_dir = homedir
1816 if c.home_dir is not None:
1817 if c.home_dir.endswith("/"):
1818 gpgfile = "{0}{1}.gpg".format(c.home_dir, keyfile)
1819 ascfile = "{0}{1}.asc".format(c.home_dir, keyfile)
1821 gpgfile = "{0}/{1}.gpg".format(c.home_dir, keyfile)
1822 ascfile = "{0}/{1}.asc".format(c.home_dir, keyfile)
1824 if os.path.exists(os.environ["GNUPGHOME"]) is True:
1825 hd = os.environ["GNUPGHOME"]
1828 hd = subprocess.getoutput(gpgconfcmd)
1830 process = subprocess.Popen(gpgconfcmd.split(),
1831 stdout=subprocess.PIPE)
1832 procom = process.communicate()
1833 if sys.version_info[0] == 2:
1834 hd = procom[0].strip()
1836 hd = procom[0].decode().strip()
1837 gpgfile = "{0}/{1}.gpg".format(hd, keyfile)
1838 ascfile = "{0}/{1}.asc".format(hd, keyfile)
1841 a_result = a.key_export_secret(pattern=logrus)
1842 b_result = b.key_export_secret(pattern=logrus)
1844 a_result = a.key_export_secret(pattern=None)
1845 b_result = b.key_export_secret(pattern=None)
1847 if a_result is not None:
1848 with open(ascfile, "wb") as f:
1850 os.chmod(ascfile, 0o600)
1854 if b_result is not None:
1855 with open(gpgfile, "wb") as f:
1857 os.chmod(gpgfile, 0o600)
1863 *** Sending public keys to the SKS Keyservers
1865 :CUSTOM_ID: howto-send-public-key
1868 As with the previous section on importing keys, the =hkp4py= module
1869 adds another option with exporting keys in order to send them to the
1872 The following example demonstrates how this may be done.
1874 #+BEGIN_SRC python -i
1881 This script sends one or more public keys to the SKS keyservers and is
1882 essentially a slight variation on the export-key.py script.
1885 c = gpg.Context(armor=True)
1886 server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
1888 if len(sys.argv) > 2:
1889 logrus = " ".join(sys.argv[1:])
1890 elif len(sys.argv) == 2:
1891 logrus = sys.argv[1]
1893 logrus = input("Enter the UID matching the key(s) to send: ")
1897 export_result = c.key_export(pattern=logrus)
1898 except Exception as e:
1900 export_result = None
1902 export_result = c.key_export(pattern=None)
1904 if export_result is not None:
1907 send_result = server.add(export_result)
1909 send_result = server.add(export_result.decode())
1910 if send_result is not None:
1914 except Exception as e:
1920 An expanded version of this script with additional functions for
1921 specifying an alternative homedir location is in the examples
1922 directory as =send-key-to-keyserver.py=.
1924 The =hkp4py= module appears to handle both string and byte literal text
1925 data equally well, but the GPGME bindings deal primarily with byte
1926 literal data only and so this script sends in that format first, then
1927 tries the string literal form.
1932 :CUSTOM_ID: howto-the-basics
1935 The most frequently called features of any cryptographic library will
1936 be the most fundamental tasks for encryption software. In this
1937 section we will look at how to programmatically encrypt data, decrypt
1938 it, sign it and verify signatures.
1943 :CUSTOM_ID: howto-basic-encryption
1946 Encrypting is very straight forward. In the first example below the
1947 message, =text=, is encrypted to a single recipient's key. In the
1948 second example the message will be encrypted to multiple recipients.
1951 *** Encrypting to one key
1953 :CUSTOM_ID: howto-basic-encryption-single
1956 Once the the Context is set the main issues with encrypting data is
1957 essentially reduced to key selection and the keyword arguments
1958 specified in the =gpg.Context().encrypt()= method.
1960 Those keyword arguments are: =recipients=, a list of keys encrypted to
1961 (covered in greater detail in the following section); =sign=, whether
1962 or not to sign the plaintext data, see subsequent sections on signing
1963 and verifying signatures below (defaults to =True=); =sink=, to write
1964 results or partial results to a secure sink instead of returning it
1965 (defaults to =None=); =passphrase=, only used when utilising symmetric
1966 encryption (defaults to =None=); =always_trust=, used to override the
1967 trust model settings for recipient keys (defaults to =False=);
1968 =add_encrypt_to=, utilises any preconfigured =encrypt-to= or
1969 =default-key= settings in the user's =gpg.conf= file (defaults to
1970 =False=); =prepare=, prepare for encryption (defaults to =False=);
1971 =expect_sign=, prepare for signing (defaults to =False=); =compress=,
1972 compresses the plaintext prior to encryption (defaults to =True=).
1974 #+BEGIN_SRC python -i
1977 a_key = "0x12345678DEADBEEF"
1978 text = b"""Some text to test with.
1980 Since the text in this case must be bytes, it is most likely that
1981 the input form will be a separate file which is opened with "rb"
1982 as this is the simplest method of obtaining the correct data format.
1985 c = gpg.Context(armor=True)
1986 rkey = list(c.keylist(pattern=a_key, secret=False))
1987 ciphertext, result, sign_result = c.encrypt(text, recipients=rkey, sign=False)
1989 with open("secret_plans.txt.asc", "wb") as afile:
1990 afile.write(ciphertext)
1993 Though this is even more likely to be used like this; with the
1994 plaintext input read from a file, the recipient keys used for
1995 encryption regardless of key trust status and the encrypted output
1996 also encrypted to any preconfigured keys set in the =gpg.conf= file:
1998 #+BEGIN_SRC python -i
2001 a_key = "0x12345678DEADBEEF"
2003 with open("secret_plans.txt", "rb") as afile:
2006 c = gpg.Context(armor=True)
2007 rkey = list(c.keylist(pattern=a_key, secret=False))
2008 ciphertext, result, sign_result = c.encrypt(text, recipients=rkey, sign=True,
2010 add_encrypt_to=True)
2012 with open("secret_plans.txt.asc", "wb") as afile:
2013 afile.write(ciphertext)
2016 If the =recipients= parameter is empty then the plaintext is encrypted
2017 symmetrically. If no =passphrase= is supplied as a parameter or via a
2018 callback registered with the =Context()= then an out-of-band prompt
2019 for the passphrase via pinentry will be invoked.
2022 *** Encrypting to multiple keys
2024 :CUSTOM_ID: howto-basic-encryption-multiple
2027 Encrypting to multiple keys essentially just expands upon the key
2028 selection process and the recipients from the previous examples.
2030 The following example encrypts a message (=text=) to everyone with an
2031 email address on the =gnupg.org= domain,[fn:4] but does /not/ encrypt
2032 to a default key or other key which is configured to normally encrypt
2035 #+BEGIN_SRC python -i
2038 text = b"""Oh look, another test message.
2040 The same rules apply as with the previous example and more likely
2041 than not, the message will actually be drawn from reading the
2042 contents of a file or, maybe, from entering data at an input()
2045 Since the text in this case must be bytes, it is most likely that
2046 the input form will be a separate file which is opened with "rb"
2047 as this is the simplest method of obtaining the correct data
2051 c = gpg.Context(armor=True)
2052 rpattern = list(c.keylist(pattern="@gnupg.org", secret=False))
2055 for i in range(len(rpattern)):
2056 if rpattern[i].can_encrypt == 1:
2057 logrus.append(rpattern[i])
2059 ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
2060 sign=False, always_trust=True)
2062 with open("secret_plans.txt.asc", "wb") as afile:
2063 afile.write(ciphertext)
2066 All it would take to change the above example to sign the message
2067 and also encrypt the message to any configured default keys would
2068 be to change the =c.encrypt= line to this:
2070 #+BEGIN_SRC python -i
2071 ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
2073 add_encrypt_to=True)
2076 The only keyword arguments requiring modification are those for which
2077 the default values are changing. The default value of =sign= is
2078 =True=, the default of =always_trust= is =False=, the default of
2079 =add_encrypt_to= is =False=.
2081 If =always_trust= is not set to =True= and any of the recipient keys
2082 are not trusted (e.g. not signed or locally signed) then the
2083 encryption will raise an error. It is possible to mitigate this
2084 somewhat with something more like this:
2086 #+BEGIN_SRC python -i
2089 with open("secret_plans.txt.asc", "rb") as afile:
2092 c = gpg.Context(armor=True)
2093 rpattern = list(c.keylist(pattern="@gnupg.org", secret=False))
2096 for i in range(len(rpattern)):
2097 if rpattern[i].can_encrypt == 1:
2098 logrus.append(rpattern[i])
2101 ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
2102 add_encrypt_to=True)
2103 except gpg.errors.InvalidRecipients as e:
2104 for i in range(len(e.recipients)):
2105 for n in range(len(logrus)):
2106 if logrus[n].fpr == e.recipients[i].fpr:
2107 logrus.remove(logrus[n])
2111 ciphertext, result, sign_result = c.encrypt(text,
2113 add_encrypt_to=True)
2114 with open("secret_plans.txt.asc", "wb") as afile:
2115 afile.write(ciphertext)
2120 This will attempt to encrypt to all the keys searched for, then remove
2121 invalid recipients if it fails and try again.
2126 :CUSTOM_ID: howto-basic-decryption
2129 Decrypting something encrypted to a key in one's secret keyring is
2130 fairly straight forward.
2132 In this example code, however, preconfiguring either =gpg.Context()=
2133 or =gpg.core.Context()= as =c= is unnecessary because there is no need
2134 to modify the Context prior to conducting the decryption and since the
2135 Context is only used once, setting it to =c= simply adds lines for no
2138 #+BEGIN_SRC python -i
2141 ciphertext = input("Enter path and filename of encrypted file: ")
2142 newfile = input("Enter path and filename of file to save decrypted data to: ")
2144 with open(ciphertext, "rb") as cfile:
2146 plaintext, result, verify_result = gpg.Context().decrypt(cfile)
2147 except gpg.errors.GPGMEError as e:
2151 if plaintext is not None:
2152 with open(newfile, "wb") as nfile:
2153 nfile.write(plaintext)
2158 The data available in =plaintext= in this example is the decrypted
2159 content as a byte object, the recipient key IDs and algorithms in
2160 =result= and the results of verifying any signatures of the data in
2163 If =gpg.Context().decrypt(cfile, verify=False)= is called instead,
2164 then =verify_result= will be returned as =None= and the rest remains
2168 ** Signing text and files
2170 :CUSTOM_ID: howto-basic-signing
2173 The following sections demonstrate how to specify keys to sign with.
2176 *** Signing key selection
2178 :CUSTOM_ID: howto-basic-signing-signers
2181 By default GPGME and the Python bindings will use the default key
2182 configured for the user invoking the GPGME API. If there is no
2183 default key specified and there is more than one secret key available
2184 it may be necessary to specify the key or keys with which to sign
2187 #+BEGIN_SRC python -i
2190 logrus = input("Enter the email address or string to match signing keys to: ")
2191 hancock = gpg.Context().keylist(pattern=logrus, secret=True)
2192 sig_src = list(hancock)
2195 The signing examples in the following sections include the explicitly
2196 designated =signers= parameter in two of the five examples; once where
2197 the resulting signature would be ASCII armoured and once where it
2198 would not be armoured.
2200 While it would be possible to enter a key ID or fingerprint here to
2201 match a specific key, it is not possible to enter two fingerprints and
2202 match two keys since the patten expects a string, bytes or None and
2203 not a list. A string with two fingerprints won't match any single
2207 *** Normal or default signing messages or files
2209 :CUSTOM_ID: howto-basic-signing-normal
2212 The normal or default signing process is essentially the same as is
2213 most often invoked when also encrypting a message or file. So when
2214 the encryption component is not utilised, the result is to produce an
2215 encoded and signed output which may or may not be ASCII armoured and
2216 which may or may not also be compressed.
2218 By default compression will be used unless GnuPG detects that the
2219 plaintext is already compressed. ASCII armouring will be determined
2220 according to the value of =gpg.Context().armor=.
2222 The compression algorithm is selected in much the same way as the
2223 symmetric encryption algorithm or the hash digest algorithm is when
2224 multiple keys are involved; from the preferences saved into the key
2225 itself or by comparison with the preferences with all other keys
2228 #+BEGIN_SRC python -i
2231 text0 = """Declaration of ... something.
2234 text = text0.encode()
2236 c = gpg.Context(armor=True, signers=sig_src)
2237 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.NORMAL)
2239 with open("/path/to/statement.txt.asc", "w") as afile:
2240 afile.write(signed_data.decode())
2243 Though everything in this example is accurate, it is more likely that
2244 reading the input data from another file and writing the result to a
2245 new file will be performed more like the way it is done in the next
2246 example. Even if the output format is ASCII armoured.
2248 #+BEGIN_SRC python -i
2251 with open("/path/to/statement.txt", "rb") as tfile:
2255 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.NORMAL)
2257 with open("/path/to/statement.txt.sig", "wb") as afile:
2258 afile.write(signed_data)
2262 *** Detached signing messages and files
2264 :CUSTOM_ID: howto-basic-signing-detached
2267 Detached signatures will often be needed in programmatic uses of
2268 GPGME, either for signing files (e.g. tarballs of code releases) or as
2269 a component of message signing (e.g. PGP/MIME encoded email).
2271 #+BEGIN_SRC python -i
2274 text0 = """Declaration of ... something.
2277 text = text0.encode()
2279 c = gpg.Context(armor=True)
2280 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH)
2282 with open("/path/to/statement.txt.asc", "w") as afile:
2283 afile.write(signed_data.decode())
2286 As with normal signatures, detached signatures are best handled as
2287 byte literals, even when the output is ASCII armoured.
2289 #+BEGIN_SRC python -i
2292 with open("/path/to/statement.txt", "rb") as tfile:
2295 c = gpg.Context(signers=sig_src)
2296 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH)
2298 with open("/path/to/statement.txt.sig", "wb") as afile:
2299 afile.write(signed_data)
2303 *** Clearsigning messages or text
2305 :CUSTOM_ID: howto-basic-signing-clear
2308 Though PGP/in-line messages are no longer encouraged in favour of
2309 PGP/MIME, there is still sometimes value in utilising in-line
2310 signatures. This is where clear-signed messages or text is of value.
2312 #+BEGIN_SRC python -i
2315 text0 = """Declaration of ... something.
2318 text = text0.encode()
2321 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.CLEAR)
2323 with open("/path/to/statement.txt.asc", "w") as afile:
2324 afile.write(signed_data.decode())
2327 In spite of the appearance of a clear-signed message, the data handled
2328 by GPGME in signing it must still be byte literals.
2330 #+BEGIN_SRC python -i
2333 with open("/path/to/statement.txt", "rb") as tfile:
2337 signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.CLEAR)
2339 with open("/path/to/statement.txt.asc", "wb") as afile:
2340 afile.write(signed_data)
2344 ** Signature verification
2346 :CUSTOM_ID: howto-basic-verification
2349 Essentially there are two principal methods of verification of a
2350 signature. The first of these is for use with the normal or default
2351 signing method and for clear-signed messages. The second is for use
2352 with files and data with detached signatures.
2354 The following example is intended for use with the default signing
2355 method where the file was not ASCII armoured:
2357 #+BEGIN_SRC python -i
2361 filename = "statement.txt"
2362 gpg_file = "statement.txt.gpg"
2367 data, result = c.verify(open(gpg_file))
2369 except gpg.errors.BadSignatures as e:
2373 if verified is True:
2374 for i in range(len(result.signatures)):
2375 sign = result.signatures[i]
2376 print("""Good signature from:
2380 """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
2381 time.ctime(sign.timestamp)))
2386 Whereas this next example, which is almost identical would work with
2387 normal ASCII armoured files and with clear-signed files:
2389 #+BEGIN_SRC python -i
2393 filename = "statement.txt"
2394 asc_file = "statement.txt.asc"
2399 data, result = c.verify(open(asc_file))
2401 except gpg.errors.BadSignatures as e:
2405 if verified is True:
2406 for i in range(len(result.signatures)):
2407 sign = result.signatures[i]
2408 print("""Good signature from:
2412 """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
2413 time.ctime(sign.timestamp)))
2418 In both of the previous examples it is also possible to compare the
2419 original data that was signed against the signed data in =data= to see
2420 if it matches with something like this:
2422 #+BEGIN_SRC python -i
2423 with open(filename, "rb") as afile:
2427 print("Good signature.")
2432 The following two examples, however, deal with detached signatures.
2433 With his method of verification the data that was signed does not get
2434 returned since it is already being explicitly referenced in the first
2435 argument of =c.verify=. So =data= is =None= and only the information
2436 in =result= is available.
2438 #+BEGIN_SRC python -i
2442 filename = "statement.txt"
2443 sig_file = "statement.txt.sig"
2448 data, result = c.verify(open(filename), open(sig_file))
2450 except gpg.errors.BadSignatures as e:
2454 if verified is True:
2455 for i in range(len(result.signatures)):
2456 sign = result.signatures[i]
2457 print("""Good signature from:
2461 """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
2462 time.ctime(sign.timestamp)))
2467 #+BEGIN_SRC python -i
2471 filename = "statement.txt"
2472 asc_file = "statement.txt.asc"
2477 data, result = c.verify(open(filename), open(asc_file))
2479 except gpg.errors.BadSignatures as e:
2483 if verified is True:
2484 for i in range(len(result.signatures)):
2485 sign = result.signatures[i]
2486 print("""Good signature from:
2490 """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
2491 time.ctime(sign.timestamp)))
2497 * Creating keys and subkeys
2499 :CUSTOM_ID: key-generation
2502 The one thing, aside from GnuPG itself, that GPGME depends on, of
2503 course, is the keys themselves. So it is necessary to be able to
2504 generate them and modify them by adding subkeys, revoking or disabling
2505 them, sometimes deleting them and doing the same for user IDs.
2507 In the following examples a key will be created for the world's
2508 greatest secret agent, Danger Mouse. Since Danger Mouse is a secret
2509 agent he needs to be able to protect information to =SECRET= level
2510 clearance, so his keys will be 3072-bit keys.
2512 The pre-configured =gpg.conf= file which sets cipher, digest and other
2513 preferences contains the following configuration parameters:
2518 allow-secret-key-import
2519 trust-model tofu+pgp
2520 tofu-default-policy unknown
2523 cert-digest-algo SHA512
2524 default-preference-list TWOFISH CAMELLIA256 AES256 CAMELLIA192 AES192 CAMELLIA128 AES BLOWFISH IDEA CAST5 3DES SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 ZLIB BZIP2 ZIP Uncompressed
2525 personal-cipher-preferences TWOFISH CAMELLIA256 AES256 CAMELLIA192 AES192 CAMELLIA128 AES BLOWFISH IDEA CAST5 3DES
2526 personal-digest-preferences SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1
2527 personal-compress-preferences ZLIB BZIP2 ZIP Uncompressed
2533 :CUSTOM_ID: keygen-primary
2536 Generating a primary key uses the =create_key= method in a Context.
2537 It contains multiple arguments and keyword arguments, including:
2538 =userid=, =algorithm=, =expires_in=, =expires=, =sign=, =encrypt=,
2539 =certify=, =authenticate=, =passphrase= and =force=. The defaults for
2540 all of those except =userid=, =algorithm=, =expires_in=, =expires= and
2541 =passphrase= is =False=. The defaults for =algorithm= and
2542 =passphrase= is =None=. The default for =expires_in= is =0=. The
2543 default for =expires= is =True=. There is no default for =userid=.
2545 If =passphrase= is left as =None= then the key will not be generated
2546 with a passphrase, if =passphrase= is set to a string then that will
2547 be the passphrase and if =passphrase= is set to =True= then gpg-agent
2548 will launch pinentry to prompt for a passphrase. For the sake of
2549 convenience, these examples will keep =passphrase= set to =None=.
2551 #+BEGIN_SRC python -i
2556 c.home_dir = "~/.gnupg-dm"
2557 userid = "Danger Mouse <dm@secret.example.net>"
2559 dmkey = c.create_key(userid, algorithm="rsa3072", expires_in=31536000,
2560 sign=True, certify=True)
2563 One thing to note here is the use of setting the =c.home_dir=
2564 parameter. This enables generating the key or keys in a different
2565 location. In this case to keep the new key data created for this
2566 example in a separate location rather than adding it to existing and
2567 active key store data. As with the default directory, =~/.gnupg=, any
2568 temporary or separate directory needs the permissions set to only
2569 permit access by the directory owner. On posix systems this means
2570 setting the directory permissions to 700.
2572 The =temp-homedir-config.py= script in the HOWTO examples directory
2573 will create an alternative homedir with these configuration options
2574 already set and the correct directory and file permissions.
2576 The successful generation of the key can be confirmed via the returned
2577 =GenkeyResult= object, which includes the following data:
2579 #+BEGIN_SRC python -i
2587 """.format(dmkey.fpr, dmkey.primary, dmkey.pubkey, dmkey.seckey, dmkey.sub,
2591 Alternatively the information can be confirmed using the command line
2595 bash-4.4$ gpg --homedir ~/.gnupg-dm -K
2596 ~/.gnupg-dm/pubring.kbx
2597 ----------------------
2598 sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
2599 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
2600 uid [ultimate] Danger Mouse <dm@secret.example.net>
2605 As with generating keys manually, to preconfigure expanded preferences
2606 for the cipher, digest and compression algorithms, the =gpg.conf= file
2607 must contain those details in the home directory in which the new key
2608 is being generated. I used a cut down version of my own =gpg.conf=
2609 file in order to be able to generate this:
2612 bash-4.4$ gpg --homedir ~/.gnupg-dm --edit-key 177B7C25DB99745EE2EE13ED026D2F19E99E63AA showpref quit
2613 Secret key is available.
2615 sec rsa3072/026D2F19E99E63AA
2616 created: 2018-03-15 expires: 2019-03-15 usage: SC
2617 trust: ultimate validity: ultimate
2618 [ultimate] (1). Danger Mouse <dm@secret.example.net>
2620 [ultimate] (1). Danger Mouse <dm@secret.example.net>
2621 Cipher: TWOFISH, CAMELLIA256, AES256, CAMELLIA192, AES192, CAMELLIA128, AES, BLOWFISH, IDEA, CAST5, 3DES
2622 Digest: SHA512, SHA384, SHA256, SHA224, RIPEMD160, SHA1
2623 Compression: ZLIB, BZIP2, ZIP, Uncompressed
2624 Features: MDC, Keyserver no-modify
2632 :CUSTOM_ID: keygen-subkeys
2635 Adding subkeys to a primary key is fairly similar to creating the
2636 primary key with the =create_subkey= method. Most of the arguments
2637 are the same, but not quite all. Instead of the =userid= argument
2638 there is now a =key= argument for selecting which primary key to add
2641 In the following example an encryption subkey will be added to the
2642 primary key. Since Danger Mouse is a security conscious secret agent,
2643 this subkey will only be valid for about six months, half the length
2646 #+BEGIN_SRC python -i
2650 c.home_dir = "~/.gnupg-dm"
2652 key = c.get_key(dmkey.fpr, secret=True)
2653 dmsub = c.create_subkey(key, algorithm="rsa3072", expires_in=15768000,
2657 As with the primary key, the results here can be checked with:
2659 #+BEGIN_SRC python -i
2667 """.format(dmsub.fpr, dmsub.primary, dmsub.pubkey, dmsub.seckey, dmsub.sub,
2671 As well as on the command line with:
2674 bash-4.4$ gpg --homedir ~/.gnupg-dm -K
2675 ~/.gnupg-dm/pubring.kbx
2676 ----------------------
2677 sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
2678 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
2679 uid [ultimate] Danger Mouse <dm@secret.example.net>
2680 ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13]
2688 :CUSTOM_ID: keygen-uids
2694 :CUSTOM_ID: keygen-uids-add
2697 By comparison to creating primary keys and subkeys, adding a new user
2698 ID to an existing key is much simpler. The method used to do this is
2699 =key_add_uid= and the only arguments it takes are for the =key= and
2702 #+BEGIN_SRC python -i
2706 c.home_dir = "~/.gnupg-dm"
2708 dmfpr = "177B7C25DB99745EE2EE13ED026D2F19E99E63AA"
2709 key = c.get_key(dmfpr, secret=True)
2710 uid = "Danger Mouse <danger.mouse@secret.example.net>"
2712 c.key_add_uid(key, uid)
2715 Unsurprisingly the result of this is:
2718 bash-4.4$ gpg --homedir ~/.gnupg-dm -K
2719 ~/.gnupg-dm/pubring.kbx
2720 ----------------------
2721 sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
2722 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
2723 uid [ultimate] Danger Mouse <danger.mouse@secret.example.net>
2724 uid [ultimate] Danger Mouse <dm@secret.example.net>
2725 ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13]
2731 *** Revoking User IDs
2733 :CUSTOM_ID: keygen-uids-revoke
2736 Revoking a user ID is a fairly similar process, except that it uses
2737 the =key_revoke_uid= method.
2739 #+BEGIN_SRC python -i
2743 c.home_dir = "~/.gnupg-dm"
2745 dmfpr = "177B7C25DB99745EE2EE13ED026D2F19E99E63AA"
2746 key = c.get_key(dmfpr, secret=True)
2747 uid = "Danger Mouse <danger.mouse@secret.example.net>"
2749 c.key_revoke_uid(key, uid)
2753 ** Key certification
2755 :CUSTOM_ID: key-sign
2758 Since key certification is more frequently referred to as key signing,
2759 the method used to perform this function is =key_sign=.
2761 The =key_sign= method takes four arguments: =key=, =uids=,
2762 =expires_in= and =local=. The default value of =uids= is =None= and
2763 which results in all user IDs being selected. The default value of
2764 both =expires_in= and =local= is =False=; which results in the
2765 signature never expiring and being able to be exported.
2767 The =key= is the key being signed rather than the key doing the
2768 signing. To change the key doing the signing refer to the signing key
2769 selection above for signing messages and files.
2771 If the =uids= value is not =None= then it must either be a string to
2772 match a single user ID or a list of strings to match multiple user
2773 IDs. In this case the matching of those strings must be precise and
2774 it is case sensitive.
2776 To sign Danger Mouse's key for just the initial user ID with a
2777 signature which will last a little over a month, do this:
2779 #+BEGIN_SRC python -i
2783 uid = "Danger Mouse <dm@secret.example.net>"
2785 dmfpr = "177B7C25DB99745EE2EE13ED026D2F19E99E63AA"
2786 key = c.get_key(dmfpr, secret=True)
2787 c.key_sign(key, uids=uid, expires_in=2764800)
2791 *** Verifying key certifications
2793 :CUSTOM_ID: key-sign-verify
2796 #+BEGIN_SRC python -i
2801 dmfpr = "177B7C25DB99745EE2EE13ED026D2F19E99E63AA"
2802 keys = list(c.keylist(pattern=dmuid, mode=gpg.constants.keylist.mode.SIGS))
2805 for user in key.uids:
2806 for sig in user.signatures:
2807 print("0x{0}".format(sig.keyid), "", time.ctime(sig.timestamp), "",
2811 Which for Danger Mouse displays the following:
2814 0x92E3F6115435C65A Thu Mar 15 13:17:44 2018 Danger Mouse <dm@secret.example.net>
2815 0x321E4E2373590E5D Mon Nov 26 12:46:05 2018 Ben McGinnes <ben@adversary.org>
2818 The two key signatures listed are for the self-certification of Danger
2819 Mouse's key made when the key was created in March, 2018; and the
2820 second is a signature made by the author and set to expire at the end
2821 of the year. Note that the second signature was made with the
2822 following code (including the preceding code to display the output of
2823 the certifications or key signatures):
2825 #+BEGIN_SRC python -i
2831 hd = "/home/dm/.gnupg"
2833 d = gpg.Context(home_dir=hd)
2834 dmfpr = "177B7C25DB99745EE2EE13ED026D2F19E99E63AA"
2835 dmuid = "Danger Mouse <dm@secret.example.net>"
2836 dkeys = list(c.keylist(pattern=dmuid))
2839 c.key_import(d.key_export(pattern=None))
2841 tp = pendulum.period(pendulum.now(tz="local"), pendulum.datetime(2019, 1, 1))
2842 ts = tp.total_seconds()
2843 total_secs = math.ceil(ts)
2844 c.key_sign(dmkey, uids=dmuid, expires_in=total_secs)
2846 d.key_import(c.key_export(pattern=dmuid))
2847 keys = list(c.keylist(pattern=dmuid, mode=gpg.constants.keylist.mode.SIGS))
2850 for user in key.uids:
2851 for sig in user.signatures:
2852 print("0x{0}".format(sig.keyid), "", time.ctime(sig.timestamp), "",
2856 Note that this final code block includes the use of a module which is
2857 /not/ part of Python's standard library, the [[https://pendulum.eustace.io/][pendulum module]]. Unlike
2858 the standard datetime module, pendulum makes working with dates and
2859 times significantly easier in Python; just as the requests module
2860 makes working with HTTP and HTTPS easier than the builtin modules do.
2862 Though neither requests nor pendulum are required modules for using
2863 the GPGME Python bindings, they are both highly recommended more
2867 * Advanced or Experimental Use Cases
2869 :CUSTOM_ID: advanced-use
2873 ** C plus Python plus SWIG plus Cython
2878 In spite of the apparent incongruence of using Python bindings to a C
2879 interface only to generate more C from the Python; it is in fact quite
2880 possible to use the GPGME bindings with [[http://docs.cython.org/en/latest/index.html][Cython]]. Though in many cases
2881 the benefits may not be obvious since the most computationally
2882 intensive work never leaves the level of the C code with which GPGME
2883 itself is interacting with.
2885 Nevertheless, there are some situations where the benefits are
2886 demonstrable. One of the better and easier examples being the one of
2887 the early examples in this HOWTO, the [[#howto-keys-counting][key counting]] code. Running that
2888 example as an executable Python script, =keycount.py= (available in
2889 the =examples/howto/= directory), will take a noticeable amount of time
2890 to run on most systems where the public keybox or keyring contains a
2891 few thousand public keys.
2893 Earlier in the evening, prior to starting this section, I ran that
2894 script on my laptop; as I tend to do periodically and timed it using
2895 =time= utility, with the following results:
2898 bash-4.4$ time keycount.py
2900 Number of secret keys: 23
2901 Number of public keys: 12112
2911 Sometime after that I imported another key and followed it with a
2912 little test of Cython. This test was kept fairly basic, essentially
2913 lifting the material from the [[http://docs.cython.org/en/latest/src/tutorial/cython_tutorial.html][Cython Basic Tutorial]] to demonstrate
2914 compiling Python code to C. The first step was to take the example
2915 key counting code quoted previously, essentially from the importing of
2916 the =gpg= module to the end of the script:
2918 #+BEGIN_SRC python -i
2922 seckeys = c.keylist(pattern=None, secret=True)
2923 pubkeys = c.keylist(pattern=None, secret=False)
2925 seclist = list(seckeys)
2926 secnum = len(seclist)
2928 publist = list(pubkeys)
2929 pubnum = len(publist)
2932 Number of secret keys: {0}
2933 Number of public keys: {1}
2935 """.format(secnum, pubnum))
2938 Save that into a file called =keycount.pyx= and then create a
2939 =setup.py= file which contains this:
2941 #+BEGIN_SRC python -i
2942 from distutils.core import setup
2943 from Cython.Build import cythonize
2946 ext_modules = cythonize("keycount.pyx")
2953 bash-4.4$ python setup.py build_ext --inplace
2957 Then run it in a similar manner to =keycount.py=:
2960 bash-4.4$ time python3.7 -c "import keycount"
2962 Number of secret keys: 23
2963 Number of public keys: 12113
2973 Cython turned =keycount.pyx= into an 81KB =keycount.o= file in the
2974 =build/= directory, a 24KB =keycount.cpython-37m-darwin.so= file to be
2975 imported into Python 3.7 and a 113KB =keycount.c= generated C source
2976 code file of nearly three thousand lines. Quite a bit bigger than the
2977 314 bytes of the =keycount.pyx= file or the full 1,452 bytes of the
2978 full executable =keycount.py= example script.
2980 On the other hand it ran in nearly half the time; taking 6 minutes and
2981 47.905 seconds to run. As opposed to the 11 minutes and 52.945 seconds
2982 which the CPython script alone took.
2984 The =keycount.pyx= and =setup.py= files used to generate this example
2985 have been added to the =examples/howto/advanced/cython/= directory
2986 The example versions include some additional options to annotate the
2987 existing code and to detect Cython's use. The latter comes from the
2988 [[http://docs.cython.org/en/latest/src/tutorial/pure.html#magic-attributes-within-the-pxd][Magic Attributes]] section of the Cython documentation.
2991 * Miscellaneous extras and work-arounds
2993 :CUSTOM_ID: cheats-and-hacks
2996 Most of the things in the following sections are here simply because
2997 there was no better place to put them, even though some are only
2998 peripherally related to the GPGME Python bindings. Some are also
2999 workarounds for functions not integrated with GPGME as yet. This is
3000 especially true of the first of these, dealing with [[#group-lines][group lines]].
3005 :CUSTOM_ID: group-lines
3008 There is not yet an easy way to access groups configured in the
3009 gpg.conf file from within GPGME. As a consequence these central
3010 groupings of keys cannot be shared amongst multiple programs, such as
3013 The following code, however, provides a work-around for obtaining this
3014 information in Python.
3016 #+BEGIN_SRC python -i
3020 if sys.platform == "win32":
3021 gpgconfcmd = "gpgconf.exe --list-options gpg"
3023 gpgconfcmd = "gpgconf --list-options gpg"
3025 process = subprocess.Popen(gpgconfcmd.split(), stdout=subprocess.PIPE)
3026 procom = process.communicate()
3028 if sys.version_info[0] == 2:
3029 lines = procom[0].splitlines()
3031 lines = procom[0].decode().splitlines()
3034 if line.startswith("group") is True:
3037 groups = line.split(":")[-1].replace('"', '').split(',')
3042 for group in groups:
3043 group_lines.append(group.split("="))
3044 group_lists.append(group.split("="))
3046 for glist in group_lists:
3047 glist[1] = glist[1].split()
3050 The result of that code is that =group_lines= is a list of lists where
3051 =group_lines[i][0]= is the name of the group and =group_lines[i][1]=
3052 is the key IDs of the group as a string.
3054 The =group_lists= result is very similar in that it is a list of
3055 lists. The first part, =group_lists[i][0]= matches
3056 =group_lines[i][0]= as the name of the group, but =group_lists[i][1]=
3057 is the key IDs of the group as a list.
3059 A demonstration of using the =groups.py= module is also available in
3060 the form of the executable =mutt-groups.py= script. This second
3061 script reads all the group entries in a user's =gpg.conf= file and
3062 converts them into crypt-hooks suitable for use with the Mutt and
3063 Neomutt mail clients.
3066 ** Keyserver access for Python
3071 The [[https://github.com/Selfnet/hkp4py][hkp4py]] module by Marcel Fest was originally a port of the old
3072 [[https://github.com/dgladkov/python-hkp][python-hkp]] module from Python 2 to Python 3 and updated to use the
3073 [[http://docs.python-requests.org/en/latest/index.html][requests]] module instead. It has since been modified to provide
3074 support for Python 2.7 as well and is available via PyPI.
3076 Since it rewrites the =hkp= protocol prefix as =http= and =hkps= as
3077 =https=, the module is able to be used even with servers which do not
3078 support the full scope of keyserver functions.[fn:5] It also works quite
3079 readily when incorporated into a [[#cython][Cython]] generated and compiled version
3083 *** Key import format
3085 :CUSTOM_ID: hkp4py-strings
3088 The hkp4py module returns key data via requests as string literals
3089 (=r.text=) instead of byte literals (=r.content=). This means that
3090 the retrurned key data must be encoded to UTF-8 when importing that
3091 key material using a =gpg.Context().key_import()= method.
3093 For this reason an alternative method has been added to the =search=
3094 function of =hkp4py.KeyServer()= which returns the key in the correct
3095 format as expected by =key_import=. When importing using this module,
3096 it is now possible to import with this:
3098 #+BEGIN_SRC python -i
3100 if key.revoked is False:
3101 gpg.Context().key_import(key.key_blob)
3106 Without that recent addition it would have been necessary to encode
3107 the contents of each =hkp4py.KeyServer().search()[i].key= in
3108 =hkp4py.KeyServer().search()= before trying to import it.
3110 An example of this is included in the [[#howto-import-key][Importing Keys]] section of this
3111 HOWTO and the corresponding executable version of that example is
3112 available in the =lang/python/examples/howto= directory as normal; the
3113 executable version is the =import-keys-hkp.py= file.
3116 ** GPGME version checking
3118 :CUSTOM_ID: gpgme-version-check
3121 For various reasons it may be necessary to check which version of
3122 GPGME the bindings have been built against; including whether a
3123 minimum required version of GPGME is in use.
3125 For the most part the =gpg.version.versionstr= and
3126 =gpg.version.versionlist= methods have been quite sufficient. The
3127 former returns the same string as =gpgme-config --version=, while the
3128 latter returns the major, minor and patch values in a list.
3130 To check if the installed bindings have actually been built against
3131 the current installed libgpgme version, this check can be performed:
3133 #+BEGIN_SRC python -i
3138 gpgme_version_call = subprocess.Popen(["gpgme-config", "--version"],
3139 stdout=subprocess.PIPE,
3140 stderr=subprocess.PIPE)
3141 gpgme_version_str = gpgme_version_call.communicate()
3143 if sys.version_info[0] == 2:
3144 gpgme_version = gpgme_version_str[0].strip()
3145 elif sys.version_info[0] >= 3:
3146 gpgme_version = gpgme_version_str[0].decode().strip()
3148 gpgme_version = None
3150 if gpgme_version is not None:
3151 if gpgme_version == gpg.version.versionstr:
3152 print("The GPGME Python bindings match libgpgme.")
3154 print("The GPGME Python bindings do NOT match libgpgme.")
3156 print("Upgrade Python and reinstall the GPGME Python bindings.")
3159 For many developers, however, the preferred checking means checking
3160 for a minimum version or point release. This is now readily available
3161 via the =gpg.version.versionintlist= method (added in version
3162 =1.12.1-beta79=). It is also now possible to easily check whether the
3163 installed GPGME Python bindings were built from a development or beta
3164 branch of the GPGME source code.
3166 The following code demonstrates how both of those methods may be used:
3168 #+BEGIN_SRC python -i
3172 if gpg.version.is_beta is True:
3173 print("The installed GPGME Python bindings were built from beta code.")
3175 print("The installed GPGME Python bindings are a released version.")
3176 except Exception as e:
3180 if gpg.version.versionintlist[0] == 1:
3181 if gpg.version.versionintlist[1] == 12:
3182 if gpg.version.versionintlist[2] == 1:
3183 print("This is the minimum version for using versionintlist.")
3184 elif gpg.version.versionintlist[2] > 1:
3185 print("The versionintlist method is available.")
3188 elif gpg.version.versionintlist[1] > 12:
3189 print("The versionintlist method is available.")
3192 elif gpg.version.versionintlist[0] > 1:
3193 print("The versionintlist method is available.")
3196 except Exception as e:
3200 The points where =pass= is used in the above example will most likely
3201 also produce an =Exception= error since those results should only
3202 occur in versions which do not have the =gpgme.version.is_beta= and
3203 =gpgme.version.versionintlist= methods available.
3206 * Copyright and Licensing
3208 :CUSTOM_ID: copyright-and-license
3214 :CUSTOM_ID: copyright
3217 Copyright © The GnuPG Project, 2018.
3220 ** Draft Editions of this HOWTO
3222 :CUSTOM_ID: draft-editions
3225 Draft editions of this HOWTO may be periodically available directly
3226 from the author at any of the following URLs:
3228 - [[https://files.au.adversary.org/crypto/gpgme-python-howto.html][GPGME Python Bindings HOWTO draft (HTML single file, AWS S3 SSL)]]
3229 - [[http://files.au.adversary.org/crypto/gpgme-python-howto.html][GPGME Python Bindings HOWTO draft (HTML single file, AWS S3 no SSL)]]
3230 - [[https://files.au.adversary.org/crypto/gpgme-python-howto-split/index.html][GPGME Python Bindings HOWTO draft (HTML multiple files, AWS S3 SSL)]]
3231 - [[http://files.au.adversary.org/crypto/gpgme-python-howto/index.html][GPGME Python Bindings HOWTO draft (HTML multiple files, AWS S3 no SSL)]]
3233 These draft versions have been generated from this document via GNU
3234 Emacs [[https://orgmode.org/][Org mode]] to =.texi= and [[https://www.gnu.org/software/texinfo/][GNU Texinfo]] to HTML. Though it is
3235 likely that the specific [[https://files.au.adversary.org/crypto/gpgme-python-howto][file]] [[http://files.au.adversary.org/crypto/gpgme-python-howto.org][version]] used will be on the same server
3236 with the generated output formats. Occasionally I may include the Org
3237 mode generated XHTML versions:
3239 - [[https://files.au.adversary.org/crypto/gpgme-python-howto.xhtml][GPGME Python Bindings HOWTO draft (HTML single file, AWS S3 SSL)]]
3240 - [[http://files.au.adversary.org/crypto/gpgme-python-howto.xhtml][GPGME Python Bindings HOWTO draft (HTML single file, AWS S3 no SSL)]]
3242 That XHTML version, however, is exported in a way which inherits a
3243 colour scheme from [[https://github.com/holomorph/emacs-zenburn][the author's Emacs theme]] (which is a higher contrast
3244 version of [[http://kippura.org/zenburnpage/][Zenburn]] ported by [[https://github.com/holomorph][Holomorph]]). So it's fine for people who
3245 prefer dark themed web pages, but not so great for everyone else.
3247 The GNU Texinfo and reStructured Text versions ship with the software,
3248 while the GNU Emacs Info version is generated from the Texinfo
3249 version using GNU Texinfo or GNU Makeinfo. The Texinfo format is
3250 generated from the original Org mode source file in Org mode itself
3251 either within GNU Emacs or via the command line by invoking Emacs in
3255 emacs gpgme-python-howto.org --batch -f org-texinfo-export-to-texinfo --kill
3256 emacs gpgme-python-howto --batch -f org-texinfo-export-to-texinfo --kill
3259 The reStructuredText format is also generated from the Org mode source
3260 file, except it is generated using [[https://pandoc.org][Pandoc]] with either of the following
3261 commands (depending on the filename):
3264 pandoc -f org -t rst+smart -o gpgme-python-howto.rst gpgme-python-howto.org
3265 pandoc -f org -t rst+smart -o gpgme-python-howto.rst gpgme-python-howto
3268 Note that the Org mode source files are identified as such via a mode
3269 line at the top of each file and have had their =.org= file extensions
3270 dropped in order to make scripted generation of output formats easier
3271 and not require renaming files post-conversion.
3273 Due to a bug in Org mode's texinfo conversion method, the recommended
3274 steps for generating the Texinfo files for all the files in the
3275 =lang/python/doc/src/= directory are as follows:
3279 emacs $x --batch -f org-texinfo-export-to-texinfo --kill
3280 cat $x.texi | sed -e 's/@documentencoding UTF-8/@documentencoding utf-8/g' > ../texinfo/$x.texi
3281 pandoc -f org -t rst+smart -o ../rst/$x.rst $x
3287 for x in *.texi ; do
3289 makeinfo --html --no-split $x
3295 This code snippet includes the generation of the reStructuredText
3296 files and would be expected to be run from the =doc/src/= directory
3297 containing the Org mode source files. It also assumes that the
3298 commands are being run on POSIX compliant systems with basic tools
3299 like sed, the Bourne shell and GNU Emacs[fn:6] available. The code
3300 snippet also includes the steps for generating the Emacs Info files
3301 and HTML files from the Texinfo files. Using reStructuredText files
3302 with Sphinx is best left for the documentation of that project.
3304 In addition to these there is a significantly less frequently updated
3305 version as a HTML [[https://files.au.adversary.org/crypto/gpgme-python/dita/webhelp/index.html][WebHelp site]] (AWS S3 SSL); generated from DITA XML
3306 source files, which can be found in [[https://dev.gnupg.org/source/gpgme/browse/ben%252Fhowto-dita/][an alternative branch]] of the GPGME
3309 Various generated output formats may occasionally be found in
3310 subdirectories of the [[https://s3.amazonaws.com/files.au.adversary.org/crypto/gpgme-python][gpgme-python]] directory. In particular within
3311 the [[https://s3.amazonaws.com/files.au.adversary.org/crypto/gpgme-python/dita][DITA]], [[https://s3.amazonaws.com/files.au.adversary.org/crypto/gpgme-python/rst][reStructuredText]] and [[https://s3.amazonaws.com/files.au.adversary.org/crypto/gpgme-python/texinfo][Texinfo]] subdirectories. The =rst=
3312 directory contains output files generated with Sphinx and may include a
3313 considerable number of its possible output formats, but there are no
3314 guarantees as to how recent these are or even if they are present.
3316 These draft editions are not official documents and the version of
3317 documentation in the master branch or which ships with released
3318 versions is the only official documentation. Nevertheless, these
3319 draft editions may occasionally be of use by providing more accessible
3320 web versions which are updated between releases. They are provided on
3321 the understanding that they may contain errors or may contain content
3322 subject to change prior to an official release.
3325 ** License GPL compatible
3330 This file is free software; as a special exception the author gives
3331 unlimited permission to copy and/or distribute it, with or without
3332 modifications, as long as this notice is preserved.
3334 This file is distributed in the hope that it will be useful, but
3335 WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
3336 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
3342 [fn:1] =short-history= and/or =short-history.html=.
3344 [fn:2] With no issues reported specific to Python 3.7, the release of
3345 Python 3.7.1 at around the same time as GPGME 1.12.0 and the testing
3346 with Python 3.7.1rc1, there is no reason to delay moving 3.7 ahead of
3347 3.6 now. Production environments with more conservative requirements
3348 will always enforce their own policies anyway and installation to each
3349 supported minor release is quite possible too.
3351 [fn:3] Yes, even if you use virtualenv with everything you do in
3352 Python. If you want to install this module as just your user account
3353 then you will need to manually configure, compile and install the
3354 /entire/ GnuPG stack as that user as well. This includes libraries
3355 which are not often installed that way. It can be done and there are
3356 circumstances under which it is worthwhile, but generally only on
3357 POSIX systems which utilise single user mode (some even require it).
3359 [fn:4] You probably don't really want to do this. Searching the
3360 keyservers for "gnupg.org" produces over 400 results, the majority of
3361 which aren't actually at the gnupg.org domain, but just included a
3362 comment regarding the project in their key somewhere.
3364 [fn:5] Such as with ProtonMail servers. This also means that
3365 restricted servers which only advertise either HTTP or HTTPS end
3366 points and not HKP or HKPS end points must still be identified as as
3367 HKP or HKPS within the Python Code. The =hkp4py= module will rewrite
3368 these appropriately when the connection is made to the server.
3370 [fn:6] Okay, Emacs might not necessarily qualify as a basic tool, but
3371 it is common enough that having it installed on a system isn't too
3372 great an expectation, nor is it difficult to add to most POSIX
3373 systems, even if the users of those systems do not personally use it.