diff --git a/lang/python/examples/assuan.py b/lang/python/examples/assuan.py
index dd42ad40..6784c9eb 100644
--- a/lang/python/examples/assuan.py
+++ b/lang/python/examples/assuan.py
@@ -1,28 +1,28 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-
"""Demonstrate the use of the Assuan protocol engine"""
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import gpg
+del absolute_import, print_function, unicode_literals
+
with gpg.Context(protocol=gpg.constants.protocol.ASSUAN) as c:
# Invoke the pinentry to get a confirmation.
err = c.assuan_transact(['GET_CONFIRMATION', 'Hello there'])
print("You chose {}.".format("cancel" if err else "ok"))
diff --git a/lang/python/examples/decryption-filter.py b/lang/python/examples/decryption-filter.py
index 987dfd13..4d99330b 100644
--- a/lang/python/examples/decryption-filter.py
+++ b/lang/python/examples/decryption-filter.py
@@ -1,32 +1,33 @@
#!/usr/bin/env python
#
-# Copyright (C) 2016 g10 Code GmbH
+# Copyright (C) 2016, 2018 g10 Code GmbH
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-
"""A decryption filter
This demonstrates decryption using gpg3 in three lines of code. To
be used like this:
./decryption-filter.py message.plain
"""
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+
+del absolute_import, print_function, unicode_literals
+
gpg.Context().decrypt(sys.stdin, sink=sys.stdout)
diff --git a/lang/python/examples/delkey.py b/lang/python/examples/delkey.py
index 12510f3e..30b3145a 100755
--- a/lang/python/examples/delkey.py
+++ b/lang/python/examples/delkey.py
@@ -1,33 +1,34 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
# Sample of key deletion
# It deletes keys for joe@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import gpg
+del absolute_import, print_function, unicode_literals
+
with gpg.Context() as c:
# Note: We must not modify the key store during iteration,
# therefore, we explicitly make a list.
keys = list(c.keylist("joe+gpg@example.org"))
for k in keys:
c.op_delete(k, True)
diff --git a/lang/python/examples/exportimport.py b/lang/python/examples/exportimport.py
index d84a01c3..36ced579 100755
--- a/lang/python/examples/exportimport.py
+++ b/lang/python/examples/exportimport.py
@@ -1,61 +1,62 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
# Sample of export and import of keys
# It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import os
import gpg
+del absolute_import, print_function, unicode_literals
+
user = "joe+gpg@example.org"
with gpg.Context(armor=True) as c, gpg.Data() as expkey:
print(" - Export %s's public keys - " % user)
c.op_export(user, 0, expkey)
# print out exported data to see how it looks in armor.
expkey.seek(0, os.SEEK_SET)
expstring = expkey.read()
if expstring:
sys.stdout.buffer.write(expstring)
else:
sys.exit("No %s's keys to export!" % user)
# delete keys to ensure that they came from our imported data. Note
# that if joe's key has private part as well we can only delete both
# of them.
with gpg.Context() as c:
# Note: We must not modify the key store during iteration,
# therefore, we explicitly make a list.
keys = list(c.keylist(user))
for k in keys:
c.op_delete(k, True)
with gpg.Context() as c:
print(" - Import exported keys - ")
c.op_import(expstring)
result = c.op_import_result()
if result:
print(result)
else:
sys.exit(" - No import result - ")
diff --git a/lang/python/examples/genkey.py b/lang/python/examples/genkey.py
index a043500e..710a530a 100755
--- a/lang/python/examples/genkey.py
+++ b/lang/python/examples/genkey.py
@@ -1,44 +1,45 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004 Igor Belyi
# Copyright (C) 2002 John Goerzen
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import gpg
+del absolute_import, print_function, unicode_literals
+
# This is the example from the GPGME manual.
parms = """
Key-Type: RSA
Key-Length: 2048
Subkey-Type: RSA
Subkey-Length: 2048
Name-Real: Joe Tester
Name-Comment: with stupid passphrase
Name-Email: joe+gpg@example.org
Passphrase: Crypt0R0cks
Expire-Date: 2020-12-31
"""
with gpg.Context() as c:
c.set_progress_cb(gpg.callbacks.progress_stdout)
c.op_genkey(parms, None, None)
print("Generated key with fingerprint {0}.".format(
c.op_genkey_result().fpr))
diff --git a/lang/python/examples/inter-edit.py b/lang/python/examples/inter-edit.py
index ed0d8c42..5b58c97b 100644
--- a/lang/python/examples/inter-edit.py
+++ b/lang/python/examples/inter-edit.py
@@ -1,56 +1,58 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2005 Igor Belyi
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-
"""Simple interactive editor to test editor scripts"""
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+del absolute_import, print_function, unicode_literals
+
if len(sys.argv) != 2:
sys.exit("Usage: %s \n" % sys.argv[0])
name = sys.argv[1]
with gpg.Context() as c:
keys = list(c.keylist(name))
if len(keys) == 0:
sys.exit("No key matching {}.".format(name))
if len(keys) > 1:
sys.exit("More than one key matching {}.".format(name))
key = keys[0]
print("Editing key {} ({}):".format(key.uids[0].uid, key.subkeys[0].fpr))
def edit_fnc(keyword, args):
- print("Status: {}, args: {} > ".format(
- keyword, args), end='', flush=True)
+ print(
+ "Status: {}, args: {} > ".format(keyword, args),
+ end='',
+ flush=True)
- if not 'GET' in keyword:
+ if 'GET' not in keyword:
# no prompt
print()
return None
try:
return input()
except EOFError:
return "quit"
c.interact(key, edit_fnc, sink=sys.stdout)
diff --git a/lang/python/examples/low_level-encrypt_to_all.py b/lang/python/examples/low_level-encrypt_to_all.py
index bad4220c..5c10d3df 100755
--- a/lang/python/examples/low_level-encrypt_to_all.py
+++ b/lang/python/examples/low_level-encrypt_to_all.py
@@ -1,53 +1,54 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2008 Igor Belyi
# Copyright (C) 2002 John Goerzen
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-
"""
This program will try to encrypt a simple message to each key on your
keyring. If your keyring has any invalid keys on it, those keys will
be skipped and it will re-try the encryption."""
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+del absolute_import, print_function, unicode_literals
+
with gpg.Context(armor=True) as c:
recipients = list()
for key in c.keylist():
valid = 0
if any(sk.can_encrypt for sk in key.subkeys):
recipients.append(key)
print("Adding recipient {0}.".format(key.uids[0].uid))
ciphertext = None
while not ciphertext:
print("Encrypting to %d recipients" % len(recipients))
try:
- ciphertext, _, _ = c.encrypt(b'This is my message.',
- recipients=recipients)
+ ciphertext, _, _ = c.encrypt(
+ b'This is my message.', recipients=recipients)
except gpg.errors.InvalidRecipients as e:
print("Encryption failed for these keys:\n{0!s}".format(e))
# filter out the bad keys
bad_keys = {bad.fpr for bad in e.recipients}
- recipients = [r for r in recipients
- if not r.subkeys[0].fpr in bad_keys]
+ recipients = [
+ r for r in recipients if not r.subkeys[0].fpr in bad_keys
+ ]
sys.stdout.buffer.write(ciphertext)
diff --git a/lang/python/examples/sign.py b/lang/python/examples/sign.py
index 16c2256a..5b90b4b8 100755
--- a/lang/python/examples/sign.py
+++ b/lang/python/examples/sign.py
@@ -1,28 +1,29 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2002 John Goerzen
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
from gpg.constants.sig import mode
+del absolute_import, print_function, unicode_literals
+
with gpg.Context() as c:
signed, _ = c.sign(b"Test message", mode=mode.CLEAR)
sys.stdout.buffer.write(signed)
diff --git a/lang/python/examples/signverify.py b/lang/python/examples/signverify.py
index 5870ca95..2df72758 100755
--- a/lang/python/examples/signverify.py
+++ b/lang/python/examples/signverify.py
@@ -1,41 +1,42 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
# Sample of unattended signing/verifying of a message.
# It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
from gpg.constants.sig import mode
+del absolute_import, print_function, unicode_literals
+
user = "joe+gpg"
with gpg.Context(pinentry_mode=gpg.constants.PINENTRY_MODE_LOOPBACK) as c:
keys = list(c.keylist(user))
if len(keys) == 0:
sys.exit("No key matching {}.".format(user))
c.signers = keys[:1]
c.set_passphrase_cb(lambda *args: "Crypt0R0cks")
signed_data, _ = c.sign(b"Test message", mode=mode.CLEAR)
data, result = c.verify(signed_data, verify=keys[:1])
print("Data: {!r}\nSignature: {!s}".format(data, result.signatures[0]))
diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py
index 8f451d7c..17c3eba0 100755
--- a/lang/python/examples/simple.py
+++ b/lang/python/examples/simple.py
@@ -1,47 +1,48 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2005 Igor Belyi
# Copyright (C) 2002 John Goerzen
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+del absolute_import, print_function, unicode_literals
+
with gpg.Context(armor=True) as c:
recipients = []
print("Enter name of your recipient(s), end with a blank line.")
while True:
line = input()
if not line:
break
new = list(c.keylist(line))
if not new:
print("Matched no known keys.")
else:
print("Adding {}.".format(", ".join(k.uids[0].name for k in new)))
recipients.extend(new)
if not recipients:
sys.exit("No recipients.")
- print("Encrypting for {}.".format(", ".join(k.uids[0].name
- for k in recipients)))
+ print("Encrypting for {}.".format(", ".join(
+ k.uids[0].name for k in recipients)))
ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
sys.stdout.buffer.write(ciphertext)
diff --git a/lang/python/examples/testCMSgetkey.py b/lang/python/examples/testCMSgetkey.py
index d4c08840..f1cdb2ce 100644
--- a/lang/python/examples/testCMSgetkey.py
+++ b/lang/python/examples/testCMSgetkey.py
@@ -1,35 +1,35 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2008 Bernhard Reiter
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-
"""A test applicaton for the CMS protocol."""
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+del absolute_import, print_function, unicode_literals
+
if len(sys.argv) != 2:
sys.exit("fingerprint or unique key ID for gpgme_get_key()")
with gpg.Context(protocol=gpg.constants.protocol.CMS) as c:
key = c.get_key(sys.argv[1])
print("got key: ", key.subkeys[0].fpr)
for uid in key.uids:
print(uid.uid)
diff --git a/lang/python/examples/verifydetails.py b/lang/python/examples/verifydetails.py
index b3ca1339..dc0e7d38 100755
--- a/lang/python/examples/verifydetails.py
+++ b/lang/python/examples/verifydetails.py
@@ -1,78 +1,82 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi
# Copyright (c) 2008 Bernhard Reiter
#
# 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 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
from __future__ import absolute_import, print_function, unicode_literals
-del absolute_import, print_function, unicode_literals
import sys
import gpg
+del absolute_import, print_function, unicode_literals
+
+
def print_engine_infos():
print("gpgme version:", gpg.core.check_version(None))
print("engines:")
for engine in gpg.core.get_engine_info():
print(engine.file_name, engine.version)
for proto in [gpg.constants.protocol.OpenPGP, gpg.constants.protocol.CMS]:
- print("Have {}? {}".format(gpg.core.get_protocol_name(proto),
- gpg.core.engine_check_version(proto)))
+ print("Have {}? {}".format(
+ gpg.core.get_protocol_name(proto),
+ gpg.core.engine_check_version(proto)))
def verifyprintdetails(filename, detached_sig_filename=None):
"""Verify a signature, print a lot of details."""
with gpg.Context() as c:
# Verify.
- data, result = c.verify(open(filename),
- open(detached_sig_filename)
- if detached_sig_filename else None)
+ data, result = c.verify(
+ open(filename),
+ open(detached_sig_filename) if detached_sig_filename else None)
# List results for all signatures. Status equal 0 means "Ok".
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(" fingerprint:", sign.fpr)
print(" uid: ", c.get_key(sign.fpr).uids[0].uid)
# Print "unsigned" text if inline signature
if data:
sys.stdout.buffer.write(data)
+
def main():
print_engine_infos()
print()
argc = len(sys.argv)
if argc < 2 or argc > 3:
- sys.exit(
- "Usage: {} [ ]".format(
- sys.argv[0]))
+ sys.exit("Usage: {} [ ]".format(
+ sys.argv[0]))
if argc == 2:
print("trying to verify file {}.".format(sys.argv[1]))
verifyprintdetails(sys.argv[1])
if argc == 3:
print("trying to verify signature {1} for file {0}.".format(*sys.argv))
verifyprintdetails(sys.argv[1], sys.argv[2])
+
if __name__ == "__main__":
main()