diff options
| author | Ben McGinnes <[email protected]> | 2018-07-07 17:40:35 +0000 | 
|---|---|---|
| committer | Ben McGinnes <[email protected]> | 2018-07-07 17:40:35 +0000 | 
| commit | cacca62d06c6cf4a291f7ab2571cf52d671d140f (patch) | |
| tree | 561ba716f584abd3ba5648a397036a30be8a95ec /lang/python/examples | |
| parent | qt: Handle OpenPGP Diagnostic log (diff) | |
| download | gpgme-cacca62d06c6cf4a291f7ab2571cf52d671d140f.tar.gz gpgme-cacca62d06c6cf4a291f7ab2571cf52d671d140f.zip  | |
python bindings: howto examples
* Made sure all example scripts meet PEP8 compliance.
* Required fixing approx. a dozen of them in minor ways.
Diffstat (limited to 'lang/python/examples')
| -rwxr-xr-x | lang/python/examples/howto/decrypt-file.py | 4 | ||||
| -rwxr-xr-x | lang/python/examples/howto/encrypt-file.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/encrypt-sign-file.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/encrypt-to-group-gullible.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/encrypt-to-group-trustno1.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/encrypt-to-group.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/export-key.py | 8 | ||||
| -rwxr-xr-x | lang/python/examples/howto/export-minimised-key.py | 8 | ||||
| -rwxr-xr-x | lang/python/examples/howto/export-secret-key.py | 10 | ||||
| -rwxr-xr-x | lang/python/examples/howto/export-secret-keys.py | 12 | ||||
| -rwxr-xr-x | lang/python/examples/howto/import-key.py | 8 | ||||
| -rwxr-xr-x | lang/python/examples/howto/import-keys.py | 13 | ||||
| -rwxr-xr-x | lang/python/examples/howto/temp-homedir-config.py | 25 | 
13 files changed, 71 insertions, 67 deletions
diff --git a/lang/python/examples/howto/decrypt-file.py b/lang/python/examples/howto/decrypt-file.py index b38acc79..2fe37f27 100755 --- a/lang/python/examples/howto/decrypt-file.py +++ b/lang/python/examples/howto/decrypt-file.py @@ -32,10 +32,10 @@ if len(sys.argv) == 3:      newfile = sys.argv[2]  elif len(sys.argv) == 2:      ciphertext = sys.argv[1] -    newfile = input("Enter path and filename of file to save decrypted data to: ") +    newfile = input("Enter path and filename to save decrypted data to: ")  else:      ciphertext = input("Enter path and filename of encrypted file: ") -    newfile = input("Enter path and filename of file to save decrypted data to: ") +    newfile = input("Enter path and filename to save decrypted data to: ")  with open(ciphertext, "rb") as cfile:      try: diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py index ad4e1cef..7c84a6f9 100755 --- a/lang/python/examples/howto/encrypt-file.py +++ b/lang/python/examples/howto/encrypt-file.py @@ -3,6 +3,9 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,9 +27,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import sys -  """  Encrypts a file to a specified key.  If entering both the key and the filename  on the command line, the key must be entered first. @@ -55,7 +55,7 @@ with open(filename, "rb") as f:  with gpg.Context(armor=True) as ca:      try:          ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, -                                                    sign=False) +                                                     sign=False)          with open("{0}.asc".format(filename), "wb") as fa:              fa.write(ciphertext)      except gpg.errors.InvalidRecipients as e: @@ -64,7 +64,7 @@ with gpg.Context(armor=True) as ca:  with gpg.Context() as cg:      try:          ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, -                                                    sign=False) +                                                     sign=False)          with open("{0}.gpg".format(filename), "wb") as fg:              fg.write(ciphertext)      except gpg.errors.InvalidRecipients as e: diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py index 41aaac86..a08176b7 100755 --- a/lang/python/examples/howto/encrypt-sign-file.py +++ b/lang/python/examples/howto/encrypt-sign-file.py @@ -3,6 +3,9 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,9 +27,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import sys -  """  Signs and encrypts a file to a specified key.  If entering both the key and the  filename on the command line, the key must be entered first. @@ -58,13 +58,13 @@ with open(filename, "rb") as f:  with gpg.Context(armor=True) as ca:      ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey,                                                   always_trust=True, -                                                     add_encrypt_to=True) +                                                 add_encrypt_to=True)      with open("{0}.asc".format(filename), "wb") as fa:          fa.write(ciphertext)  with gpg.Context() as cg:      ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey,                                                   always_trust=True, -                                                     add_encrypt_to=True) +                                                 add_encrypt_to=True)      with open("{0}.gpg".format(filename), "wb") as fg:          fg.write(ciphertext) diff --git a/lang/python/examples/howto/encrypt-to-group-gullible.py b/lang/python/examples/howto/encrypt-to-group-gullible.py index 7ebfb6bc..c96e8294 100755 --- a/lang/python/examples/howto/encrypt-to-group-gullible.py +++ b/lang/python/examples/howto/encrypt-to-group-gullible.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import sys -from groups import group_lists -  """  Uses the groups module to encrypt to multiple recipients. @@ -74,7 +74,7 @@ if klist is not None:                                                      add_encrypt_to=True,                                                      always_trust=True)      with open("{0}.asc".format(filepath), "wb") as f: -              f.write(ciphertext) +        f.write(ciphertext)  else:      pass diff --git a/lang/python/examples/howto/encrypt-to-group-trustno1.py b/lang/python/examples/howto/encrypt-to-group-trustno1.py index 736c5f10..da0376b5 100755 --- a/lang/python/examples/howto/encrypt-to-group-trustno1.py +++ b/lang/python/examples/howto/encrypt-to-group-trustno1.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import sys -from groups import group_lists -  """  Uses the groups module to encrypt to multiple recipients. @@ -83,7 +83,7 @@ if klist is not None:          except:              pass      with open("{0}.asc".format(filepath), "wb") as f: -              f.write(ciphertext) +        f.write(ciphertext)  else:      pass diff --git a/lang/python/examples/howto/encrypt-to-group.py b/lang/python/examples/howto/encrypt-to-group.py index 84f8d103..d4cb0745 100755 --- a/lang/python/examples/howto/encrypt-to-group.py +++ b/lang/python/examples/howto/encrypt-to-group.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import sys -from groups import group_lists -  """  Uses the groups module to encrypt to multiple recipients. @@ -84,7 +84,7 @@ if klist is not None:          except:              pass      with open("{0}.asc".format(filepath), "wb") as f: -              f.write(ciphertext) +        f.write(ciphertext)  else:      pass diff --git a/lang/python/examples/howto/export-key.py b/lang/python/examples/howto/export-key.py index 41be64f2..6def6871 100755 --- a/lang/python/examples/howto/export-key.py +++ b/lang/python/examples/howto/export-key.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os.path -import sys -  print("""  This script exports one or more public keys.  """) diff --git a/lang/python/examples/howto/export-minimised-key.py b/lang/python/examples/howto/export-minimised-key.py index d28b1cb7..c2c533ee 100755 --- a/lang/python/examples/howto/export-minimised-key.py +++ b/lang/python/examples/howto/export-minimised-key.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os.path -import sys -  print("""  This script exports one or more public keys in minimised form.  """) diff --git a/lang/python/examples/howto/export-secret-key.py b/lang/python/examples/howto/export-secret-key.py index 8bbe4095..e9c53fe5 100755 --- a/lang/python/examples/howto/export-secret-key.py +++ b/lang/python/examples/howto/export-secret-key.py @@ -3,6 +3,11 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os +import os.path +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,11 +29,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os -import os.path -import sys -  print("""  This script exports one or more secret keys. diff --git a/lang/python/examples/howto/export-secret-keys.py b/lang/python/examples/howto/export-secret-keys.py index 03037c92..f0a791ef 100755 --- a/lang/python/examples/howto/export-secret-keys.py +++ b/lang/python/examples/howto/export-secret-keys.py @@ -3,6 +3,12 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os +import os.path +import subprocess +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,12 +30,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os -import os.path -import subprocess -import sys -  print("""  This script exports one or more secret keys as both ASCII armored and binary  file formats, saved in files within the user's GPG home directory. diff --git a/lang/python/examples/howto/import-key.py b/lang/python/examples/howto/import-key.py index 56cfe259..464052d3 100755 --- a/lang/python/examples/howto/import-key.py +++ b/lang/python/examples/howto/import-key.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os.path -import sys -  print("""  This script exports one or more public keys.  """) diff --git a/lang/python/examples/howto/import-keys.py b/lang/python/examples/howto/import-keys.py index 8a3bb29d..bdc15a68 100755 --- a/lang/python/examples/howto/import-keys.py +++ b/lang/python/examples/howto/import-keys.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import requests +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,21 +28,14 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import gpg -import os.path -import requests -  print("""  This script imports one or more public keys from the SKS keyservers.  """) -import gpg -import requests -  c = gpg.Context()  url = "https://sks-keyservers.net/pks/lookup"  pattern = input("Enter the pattern to search for key or user IDs: ") -payload = { "op": "get", "search": pattern } +payload = {"op": "get", "search": pattern}  r = requests.get(url, verify=True, params=payload)  result = c.key_import(r.content) diff --git a/lang/python/examples/howto/temp-homedir-config.py b/lang/python/examples/howto/temp-homedir-config.py index ddd79327..3bb5cf35 100755 --- a/lang/python/examples/howto/temp-homedir-config.py +++ b/lang/python/examples/howto/temp-homedir-config.py @@ -3,6 +3,10 @@  from __future__ import absolute_import, division, unicode_literals +import os +import os.path +import sys +  # Copyright (C) 2018 Ben McGinnes <[email protected]>  #  # This program is free software; you can redistribute it and/or modify it under @@ -24,10 +28,6 @@ from __future__ import absolute_import, division, unicode_literals  # Lesser General Public along with this program; if not, see  # <http://www.gnu.org/licenses/>. -import os -import os.path -import sys -  intro = """  This script creates a temporary directory to use as a homedir for  testing key generation tasks with the correct permissions, along @@ -54,6 +54,13 @@ message telling you to specify a new directory name.  There is no  default directory name.  """ +ciphers256 = "TWOFISH CAMELLIA256 AES256" +ciphers192 = "CAMELLIA192 AES192" +ciphers128 = "CAMELLIA128 AES" +ciphersBad = "BLOWFISH IDEA CAST5 3DES" +digests = "SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1" +compress = "ZLIB BZIP2 ZIP Uncompressed" +  gpgconf = """# gpg.conf settings for key generation:  expert  allow-freeform-uid @@ -63,11 +70,11 @@ tofu-default-policy unknown  enable-large-rsa  enable-dsa2  cert-digest-algo SHA512 -default-preference-list TWOFISH CAMELLIA256 AES256 CAMELLIA192 AES192 CAMELLIA128 AES BLOWFISH IDEA CAST5 3DES SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 ZLIB BZIP2 ZIP Uncompressed -personal-cipher-preferences TWOFISH CAMELLIA256 AES256 CAMELLIA192 AES192 CAMELLIA128 AES BLOWFISH IDEA CAST5 3DES -personal-digest-preferences SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 -personal-compress-preferences ZLIB BZIP2 ZIP Uncompressed -""" +default-preference-list {0} {1} {2} {3} {4} {5} +personal-cipher-preferences {0} {1} {2} {3} +personal-digest-preferences {4} +personal-compress-preferences {5} +""".format(ciphers256, ciphers192, ciphers128, ciphersBad, digests, compress)  agentconf = """# gpg-agent.conf settings for key generation:  default-cache-ttl 300  | 
