doc: python bindings howto

* Added example for verifying both detached and "in-line" signatures.
This commit is contained in:
Ben McGinnes 2018-03-09 15:22:24 +11:00
parent f685cda281
commit ab81c2d868

View File

@ -13,12 +13,12 @@
:CUSTOM_ID: intro :CUSTOM_ID: intro
:END: :END:
Version: 0.0.1-alpha [2018-03-07 Wed] Version: 0.0.1-alpha [2018-03-07 Wed]
Author: Ben McGinnes <ben@gnupg.org> Author: Ben McGinnes <ben@gnupg.org>
Author GPG Key: DB4724E6FA4286C92B4E55C4321E4E2373590E5D Author GPG Key: DB4724E6FA4286C92B4E55C4321E4E2373590E5D
This document provides basic instruction in how to use the GPGME This document provides basic instruction in how to use the GPGME
Python bindings to programmatically leverage the GPGME library. Python bindings to programmatically leverage the GPGME library.
* GPGME Concepts * GPGME Concepts
@ -401,23 +401,22 @@ Python bindings to programmatically leverage the GPGME library.
afile.close() afile.close()
#+end_src #+end_src
Detached binary signing (maybe change text to be reading a file's Detached binary signing of a file.
content):
#+begin_src python #+begin_src python
import gpg import gpg
text = """Declaration of ... something. tfile = open("/path/to/statement.txt", "r")
text = tfile.read()
tfile.close()
""" c = gpg.Context()
c.armor = True
signed = c.sign(text, mode=mode.DETACH)
c = gpg.Context() afile = open("/path/to/statement.txt.sig", "wb")
c.armor = True afile.write(signed[0])
signed = c.sign(text, mode=mode.DETACH) afile.close()
afile = open("/path/to/statement.txt.sig", "wb")
afile.write(signed[0])
afile.close()
#+end_src #+end_src
@ -426,7 +425,31 @@ afile.close()
:CUSTOM_ID: howto-basic-verification :CUSTOM_ID: howto-basic-verification
:END: :END:
x Verify a signed file, both detached and not:
#+begin_src python
import gpg
import sys
import time
c = gpg.Context()
data, result = c.verify(open(filename),
open(detached_sig_filename)
if detached_sig_filename else None)
for index, sign in enumerate(result.signatures):
print("signature", index, ":")
print(" summary: %#0x" % (sign.summary))
print(" status: %#0x" % (sign.status))
print(" timestamp: ", sign.timestamp)
print(" timestamp: ", time.ctime(sign.timestamp))
print(" fingerprint:", sign.fpr)
print(" uid: ", c.get_key(sign.fpr).uids[0].uid)
if data:
sys.stdout.buffer.write(data)
#+end_src
* Copyright and Licensing * Copyright and Licensing