python bindings: howto examples

* Made sure all example scripts meet PEP8 compliance.
* Required fixing approx. a dozen of them in minor ways.
This commit is contained in:
Ben McGinnes 2018-07-08 03:40:35 +10:00
parent 66c2a99422
commit cacca62d06
13 changed files with 71 additions and 67 deletions

View File

@ -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:

View File

@ -3,6 +3,9 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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:

View File

@ -3,6 +3,9 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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)

View File

@ -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 <ben@gnupg.org>
#
# 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

View File

@ -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 <ben@gnupg.org>
#
# 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

View File

@ -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 <ben@gnupg.org>
#
# 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

View File

@ -3,6 +3,10 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import os.path
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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.
""")

View File

@ -3,6 +3,10 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import os.path
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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.
""")

View File

@ -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 <ben@gnupg.org>
#
# 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.

View File

@ -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 <ben@gnupg.org>
#
# 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.

View File

@ -3,6 +3,10 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import os.path
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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.
""")

View File

@ -3,6 +3,10 @@
from __future__ import absolute_import, division, unicode_literals
import gpg
import os.path
import requests
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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)

View File

@ -3,6 +3,10 @@
from __future__ import absolute_import, division, unicode_literals
import os
import os.path
import sys
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
#
# 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