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 @@ -1,51 +1,51 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . import gpg import sys if len(sys.argv) == 3: ciphertext = sys.argv[1] newfile = sys.argv[2] elif len(sys.argv) == 2: ciphertext = sys.argv[1] - newfile = input("Enter path and filename of file to save decrypted data to: ") + 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: plaintext, result, verify_result = gpg.Context().decrypt(cfile) except gpg.errors.GPGMEError as e: plaintext = None print(e) if plaintext is not None: with open(newfile, "wb") as nfile: nfile.write(plaintext) else: pass 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 @@ -1,71 +1,71 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import sys - """ Encrypts a file to a specified key. If entering both the key and the filename on the command line, the key must be entered first. Will produce both an ASCII armoured and GPG binary format copy of the encrypted file. """ if len(sys.argv) > 3: a_key = sys.argv[1] filename = " ".join(sys.argv[2:]) elif len(sys.argv) == 3: a_key = sys.argv[1] filename = sys.argv[2] elif len(sys.argv) == 2: a_key = sys.argv[1] filename = input("Enter the path and filename to encrypt: ") else: a_key = input("Enter the fingerprint or key ID to encrypt to: ") filename = input("Enter the path and filename to encrypt: ") rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) with open(filename, "rb") as f: text = f.read() with gpg.Context(armor=True) as ca: try: ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, - sign=False) + sign=False) with open("{0}.asc".format(filename), "wb") as fa: fa.write(ciphertext) except gpg.errors.InvalidRecipients as e: print(e) with gpg.Context() as cg: try: ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey, - sign=False) + sign=False) with open("{0}.gpg".format(filename), "wb") as fg: fg.write(ciphertext) except gpg.errors.InvalidRecipients as e: print(e) diff --git a/lang/python/examples/howto/encrypt-sign-file.py b/lang/python/examples/howto/encrypt-sign-file.py index 41aaac86..a08176b7 100755 --- a/lang/python/examples/howto/encrypt-sign-file.py +++ b/lang/python/examples/howto/encrypt-sign-file.py @@ -1,70 +1,70 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import sys - """ Signs and encrypts a file to a specified key. If entering both the key and the filename on the command line, the key must be entered first. Signs with and also encrypts to the default key of the user invoking the script. Will treat all recipients as trusted to permit encryption. Will produce both an ASCII armoured and GPG binary format copy of the signed and encrypted file. """ if len(sys.argv) > 3: a_key = sys.argv[1] filename = " ".join(sys.argv[2:]) elif len(sys.argv) == 3: a_key = sys.argv[1] filename = sys.argv[2] elif len(sys.argv) == 2: a_key = sys.argv[1] filename = input("Enter the path and filename to encrypt: ") else: a_key = input("Enter the fingerprint or key ID to encrypt to: ") filename = input("Enter the path and filename to encrypt: ") rkey = list(gpg.Context().keylist(pattern=a_key, secret=False)) with open(filename, "rb") as f: text = f.read() with gpg.Context(armor=True) as ca: ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey, always_trust=True, - add_encrypt_to=True) + 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 @@ -1,81 +1,81 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import sys -from groups import group_lists - """ Uses the groups module to encrypt to multiple recipients. """ c = gpg.Context(armor=True) if len(sys.argv) > 3: group_id = sys.argv[1] filepath = sys.argv[2:] elif len(sys.argv) == 3: group_id = sys.argv[1] filepath = sys.argv[2] elif len(sys.argv) == 2: group_id = sys.argv[1] filepath = input("Enter the filename to encrypt: ") else: group_id = input("Enter the group name to encrypt to: ") filepath = input("Enter the filename to encrypt: ") with open(filepath, "rb") as f: text = f.read() for i in range(len(group_lists)): if group_lists[i][0] == group_id: klist = group_lists[i][1] else: klist = None logrus = [] if klist is not None: for i in range(len(klist)): apattern = list(c.keylist(pattern=klist[i], secret=False)) if apattern[0].can_encrypt == 1: logrus.append(apattern[0]) else: pass try: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True) except: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True, always_trust=True) with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) + f.write(ciphertext) else: pass # EOF 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 @@ -1,90 +1,90 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import sys -from groups import group_lists - """ Uses the groups module to encrypt to multiple recipients. """ c = gpg.Context(armor=True) if len(sys.argv) > 3: group_id = sys.argv[1] filepath = sys.argv[2:] elif len(sys.argv) == 3: group_id = sys.argv[1] filepath = sys.argv[2] elif len(sys.argv) == 2: group_id = sys.argv[1] filepath = input("Enter the filename to encrypt: ") else: group_id = input("Enter the group name to encrypt to: ") filepath = input("Enter the filename to encrypt: ") with open(filepath, "rb") as f: text = f.read() for i in range(len(group_lists)): if group_lists[i][0] == group_id: klist = group_lists[i][1] else: klist = None logrus = [] if klist is not None: for i in range(len(klist)): apattern = list(c.keylist(pattern=klist[i], secret=False)) if apattern[0].can_encrypt == 1: logrus.append(apattern[0]) else: pass try: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True) except gpg.errors.InvalidRecipients as e: for i in range(len(e.recipients)): for n in range(len(logrus)): if logrus[n].fpr == e.recipients[i].fpr: logrus.remove(logrus[n]) else: pass try: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True) except: pass with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) + f.write(ciphertext) else: pass # EOF 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 @@ -1,91 +1,91 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import sys +from groups import group_lists + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import sys -from groups import group_lists - """ Uses the groups module to encrypt to multiple recipients. """ c = gpg.Context(armor=True) if len(sys.argv) > 3: group_id = sys.argv[1] filepath = sys.argv[2:] elif len(sys.argv) == 3: group_id = sys.argv[1] filepath = sys.argv[2] elif len(sys.argv) == 2: group_id = sys.argv[1] filepath = input("Enter the filename to encrypt: ") else: group_id = input("Enter the group name to encrypt to: ") filepath = input("Enter the filename to encrypt: ") with open(filepath, "rb") as f: text = f.read() for i in range(len(group_lists)): if group_lists[i][0] == group_id: klist = group_lists[i][1] else: klist = None logrus = [] if klist is not None: for i in range(len(klist)): apattern = list(c.keylist(pattern=klist[i], secret=False)) if apattern[0].can_encrypt == 1: logrus.append(apattern[0]) else: pass try: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True) except gpg.errors.InvalidRecipients as e: for i in range(len(e.recipients)): for n in range(len(logrus)): if logrus[n].fpr == e.recipients[i].fpr: logrus.remove(logrus[n]) else: pass try: ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, add_encrypt_to=True, always_trust=True) except: pass with open("{0}.asc".format(filepath), "wb") as f: - f.write(ciphertext) + f.write(ciphertext) else: pass # EOF 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 @@ -1,73 +1,73 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import os.path -import sys - print(""" This script exports one or more public keys. """) c = gpg.Context(armor=True) if len(sys.argv) >= 4: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = sys.argv[3] elif len(sys.argv) == 3: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = input("Enter the GPG configuration directory path (optional): ") elif len(sys.argv) == 2: keyfile = sys.argv[1] logrus = input("Enter the UID matching the key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") else: keyfile = input("Enter the path and filename to save the secret key to: ") logrus = input("Enter the UID matching the key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass try: result = c.key_export(pattern=logrus) except: result = c.key_export(pattern=None) if result is not None: with open(keyfile, "wb") as f: f.write(result) else: pass 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 @@ -1,73 +1,73 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import os.path -import sys - print(""" This script exports one or more public keys in minimised form. """) c = gpg.Context(armor=True) if len(sys.argv) >= 4: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = sys.argv[3] elif len(sys.argv) == 3: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = input("Enter the GPG configuration directory path (optional): ") elif len(sys.argv) == 2: keyfile = sys.argv[1] logrus = input("Enter the UID matching the key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") else: keyfile = input("Enter the path and filename to save the secret key to: ") logrus = input("Enter the UID matching the key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass try: result = c.key_export_minimal(pattern=logrus) except: result = c.key_export_minimal(pattern=None) if result is not None: with open(keyfile, "wb") as f: f.write(result) else: pass 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 @@ -1,77 +1,77 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os +import os.path +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import os -import os.path -import sys - print(""" This script exports one or more secret keys. The gpg-agent and pinentry are invoked to authorise the export. """) c = gpg.Context(armor=True) if len(sys.argv) >= 4: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = sys.argv[3] elif len(sys.argv) == 3: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = input("Enter the GPG configuration directory path (optional): ") elif len(sys.argv) == 2: keyfile = sys.argv[1] logrus = input("Enter the UID matching the secret key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") else: keyfile = input("Enter the path and filename to save the secret key to: ") logrus = input("Enter the UID matching the secret key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass try: result = c.key_export_secret(pattern=logrus) except: result = c.key_export_secret(pattern=None) if result is not None: with open(keyfile, "wb") as f: f.write(result) os.chmod(keyfile, 0o600) else: pass 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 @@ -1,110 +1,110 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os +import os.path +import subprocess +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -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. The gpg-agent and pinentry are invoked to authorise the export. """) if sys.platform == "win32": gpgconfcmd = "gpgconf.exe --list-dirs homedir" else: gpgconfcmd = "gpgconf --list-dirs homedir" a = gpg.Context(armor=True) b = gpg.Context() c = gpg.Context() if len(sys.argv) >= 4: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = sys.argv[3] elif len(sys.argv) == 3: keyfile = sys.argv[1] logrus = sys.argv[2] homedir = input("Enter the GPG configuration directory path (optional): ") elif len(sys.argv) == 2: keyfile = sys.argv[1] logrus = input("Enter the UID matching the secret key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") else: keyfile = input("Enter the filename to save the secret key to: ") logrus = input("Enter the UID matching the secret key(s) to export: ") homedir = input("Enter the GPG configuration directory path (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass if c.home_dir is not None: if c.home_dir.endswith("/"): gpgfile = "{0}{1}.gpg".format(c.home_dir, keyfile) ascfile = "{0}{1}.asc".format(c.home_dir, keyfile) else: gpgfile = "{0}/{1}.gpg".format(c.home_dir, keyfile) ascfile = "{0}/{1}.asc".format(c.home_dir, keyfile) else: if os.path.exists(os.environ["GNUPGHOME"]) is True: hd = os.environ["GNUPGHOME"] else: hd = subprocess.getoutput(gpgconfcmd) gpgfile = "{0}/{1}.gpg".format(hd, keyfile) ascfile = "{0}/{1}.asc".format(hd, keyfile) try: a_result = a.key_export_secret(pattern=logrus) b_result = b.key_export_secret(pattern=logrus) except: a_result = a.key_export_secret(pattern=None) b_result = b.key_export_secret(pattern=None) if a_result is not None: with open(ascfile, "wb") as f: f.write(a_result) os.chmod(ascfile, 0o600) else: pass if b_result is not None: with open(gpgfile, "wb") as f: f.write(b_result) os.chmod(gpgfile, 0o600) else: pass 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 @@ -1,91 +1,91 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -import gpg -import os.path -import sys - print(""" This script exports one or more public keys. """) c = gpg.Context(armor=True) if len(sys.argv) >= 3: keyfile = sys.argv[1] homedir = sys.argv[2] elif len(sys.argv) == 2: keyfile = sys.argv[1] homedir = input("Enter the GPG configuration directory path (optional): ") else: keyfile = input("Enter the path and filename to import the key(s) from: ") homedir = input("Enter the GPG configuration directory path (optional): ") if homedir.startswith("~"): if os.path.exists(os.path.expanduser(homedir)) is True: c.home_dir = os.path.expanduser(homedir) else: pass elif os.path.exists(homedir) is True: c.home_dir = homedir else: pass if os.path.isfile(keyfile) is True: with open(keyfile, "rb") as f: incoming = f.read() result = c.key_import(incoming) else: result = None if result is not None and hasattr(result, "considered") is False: print(result) elif result is not None and hasattr(result, "considered") is True: num_keys = len(result.imports) new_revs = result.new_revocations new_sigs = result.new_signatures new_subs = result.new_sub_keys new_uids = result.new_user_ids new_scrt = result.secret_imported nochange = result.unchanged print(""" The total number of keys considered for import was: {0} Number of keys revoked: {1} Number of new signatures: {2} Number of new subkeys: {3} Number of new user IDs: {4} Number of new secret keys: {5} Number of unchanged keys: {6} The key IDs for all considered keys were: """.format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, nochange)) for i in range(num_keys): print(result.imports[i].fpr) print("") elif result is None: print("You must specify a key file to import.") 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 @@ -1,73 +1,70 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import gpg +import os.path +import requests + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -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) if result is not None and hasattr(result, "considered") is False: print(result) elif result is not None and hasattr(result, "considered") is True: num_keys = len(result.imports) new_revs = result.new_revocations new_sigs = result.new_signatures new_subs = result.new_sub_keys new_uids = result.new_user_ids new_scrt = result.secret_imported nochange = result.unchanged print(""" The total number of keys considered for import was: {0} Number of keys revoked: {1} Number of new signatures: {2} Number of new subkeys: {3} Number of new user IDs: {4} Number of new secret keys: {5} Number of unchanged keys: {6} The key IDs for all considered keys were: """.format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt, nochange)) for i in range(num_keys): print(result.imports[i].fpr) print("") else: pass 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 @@ -1,126 +1,133 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals +import os +import os.path +import sys + # Copyright (C) 2018 Ben McGinnes # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU # Lesser General Public Licensefor more details. # # You should have received a copy of the GNU General Public License and the GNU # Lesser General Public along with this program; if not, see # . -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 with a gpg.conf file containing the same configuration options listed in the HOWTO. You may wish to change the order of the cipher preferences or remove those not relevant to your installation. These configuration parameters assume that all ciphers and digests are installed and available rather than limiting to the default ciphers and digests. The script prompts for a directory name to be installed as a hidden directory in the user's home directory on POSIX systems. So if you enter "gnupg-temp" on a Linux, BSD or OS X system, it will create "~/.gnupg-temp" (you do not need to enter the leading dot). This script has not been tested on Windows systems and may have unpredictable results. That said, it will not delete or copy over existing data. If the directory already exists, the script will terminate with a 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 allow-secret-key-import trust-model tofu+pgp 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 """ if len(sys.argv) == 1: print(intro) new_homedir = input("Enter the temporary gnupg homedir name: ") elif len(sys.argv) == 2: new_homedir = sys.argv[1] else: new_homedir = " ".join(sys.argv[1:]) userdir = os.path.expanduser("~") if new_homedir.startswith("~"): new_homdir.replace("~", "") else: pass if new_homedir.startswith("/"): new_homdir.replace("/", "") else: pass if new_homedir.startswith("."): new_homdir.replace(".", "_") else: pass if new_homedir.count(" ") > 0: new_homedir.replace(" ", "_") else: pass nh = "{0}/.{1}".format(userdir, new_homedir) if os.path.exists(nh) is True: print("The {0} directory already exists.".format(nh)) else: print("Creating the {0} directory.".format(nh)) os.mkdir(nh) os.chmod(nh, 0o700) with open("{0}/{1}".format(nh, "gpg.conf"), "w") as f1: f1.write(gpgconf) os.chmod("{0}/{1}".format(nh, "gpg.conf"), 0o600) with open("{0}/{1}".format(nh, "gpg-agent.conf"), "w") as f2: f2.write(gpgconf) os.chmod("{0}/{1}".format(nh, "gpg-agent.conf"), 0o600) print("""You may now use the {0} directory as an alternative GPG homedir: gpg --homedir {0} gpg --homedir --full-gen-key Or with GPGME scripts, including the GPGME Python bindings. """)