python: Clean up and modernize examples.

* lang/python/examples/Examples.rst: Delete file.
* lang/python/examples/t-edit.py: Likewise.  This is actually a test
case and has been moved to 'tests'.
* lang/python/examples/assuan.py: New file.
* lang/python/examples/decryption-filter.py: Likewise.
* lang/python/examples/delkey.py: Modernize.
* lang/python/examples/encrypt-to-all.py: Likewise.
* lang/python/examples/exportimport.py: Likewise.
* lang/python/examples/genkey.py: Likewise.
* lang/python/examples/inter-edit.py: Likewise.
* lang/python/examples/sign.py: Likewise.
* lang/python/examples/signverify.py: Likewise.
* lang/python/examples/simple.py: Likewise.
* lang/python/examples/testCMSgetkey.py: Likewise.
* lang/python/examples/verifydetails.py: Likewise.

Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
Justus Winter 2016-08-05 14:03:15 +02:00
parent 6f3dc66634
commit 2a613e8715
14 changed files with 344 additions and 484 deletions

View File

@ -1,7 +0,0 @@
===============
Example Scripts
===============
Most of the examples have been converted to work with Python 3, just as the original versions worked with Python 2. A small number produce errors on OS X, but may behave differently on other POSIX systems. The GTK based scripts (PyGtkGpgKeys.py and pygpa.py) have not been modified at all.
When using or referring to the example scripts here, the most common change has been the byte encoded strings, so if something does not work then chances are that it will be related to the encoding or decoding of UTF-8.

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
#
# 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 <http://www.gnu.org/licenses/>.
"""Demonstrate the use of the Assuan protocol engine"""
import pyme
with pyme.Context(protocol=pyme.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"))

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
#
# 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 <http://www.gnu.org/licenses/>.
"""A decryption filter
This demonstrates decryption using pyme3 in three lines of code. To
be used like this:
./decryption-filter.py <message.gpg >message.plain
"""
import sys
import pyme
pyme.Context().decrypt(sys.stdin, sink=sys.stdout)

View File

@ -1,35 +1,30 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or modify
# modify it under the terms of the GNU General Public License as # it under the terms of the GNU General Public License as published by
# published by the Free Software Foundation; either version 2 of # the Free Software Foundation; either version 2 of the License, or
# the License, or (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
# Sample of key deletion # Sample of key deletion
# It deletes keys for joe@example.org generated by genkey.pl script # It deletes keys for joe@example.org generated by genkey.py script
from pyme import core import pyme
core.check_version(None) with pyme.Context() as c:
# Note: We must not modify the key store during iteration,
# therefore, we explicitly make a list.
keys = list(c.keylist("joe+pyme@example.org"))
# Note that we need to collect all keys out of the iterator return by for k in keys:
# c.op_keylist_all() method before starting to delete them. If you c.op_delete(k, True)
# delete a key in the middle of iteration c.op_keylist_next() will
# raise INV_VALUE exception
c = core.Context()
# 0 in keylist means to list not only public but secret keys as well.
for thekey in [x for x in c.op_keylist_all("joe+pyme@example.org", 0)]:
# 1 in delete means to delete not only public but secret keys as well.
c.op_delete(thekey, 1)

View File

@ -1,21 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2008 Igor Belyi <belyi@users.sourceforge.net>
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org> # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or modify
# modify it under the terms of the GNU General Public License as # it under the terms of the GNU General Public License as published by
# published by the Free Software Foundation; either version 2 of # the Free Software Foundation; either version 2 of the License, or
# the License, or (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
""" """
This program will try to encrypt a simple message to each key on your This program will try to encrypt a simple message to each key on your
@ -24,44 +24,28 @@ be skipped and it will re-try the encryption."""
import sys import sys
import os import os
from pyme import core import pyme
from pyme.core import Data, Context
core.check_version(None) with pyme.Context(armor=True) as c:
recipients = list()
plain = Data('This is my message.') for key in c.keylist():
c = Context()
c.set_armor(1)
def sendto(keylist):
cipher = Data()
c.op_encrypt(keylist, 1, plain, cipher)
cipher.seek(0, os.SEEK_SET)
return cipher.read()
names = []
for key in c.op_keylist_all(None, 0):
try:
print(" *** Found key for %s" % key.uids[0].uid)
valid = 0 valid = 0
for subkey in key.subkeys: if any(sk.can_encrypt for sk in key.subkeys):
keyid = subkey.keyid recipients.append(key)
if keyid is None: print("Adding recipient {0}.".format(key.uids[0].uid))
break
can_encrypt = subkey.can_encrypt
valid += can_encrypt
print(" Subkey %s: encryption %s" %
(keyid, can_encrypt and "enabled" or "disabled"))
except UnicodeEncodeError as e:
print(e)
if valid: ciphertext = None
names.append(key) while not ciphertext:
else: print("Encrypting to %d recipients" % len(recipients))
print(" This key cannot be used for encryption; skipping.") try:
ciphertext, _, _ = c.encrypt(b'This is my message.',
recipients=recipients)
except pyme.errors.InvalidRecipients as e:
print("Encryption failed for these keys:\n{0!s}".format(e))
passno = 0 # 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]
print("Encrypting to %d recipients" % len(names)) sys.stdout.buffer.write(ciphertext)
sys.stdout.buffer.write(sendto(names))

View File

@ -1,75 +1,58 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or modify
# modify it under the terms of the GNU General Public License as # it under the terms of the GNU General Public License as published by
# published by the Free Software Foundation; either version 2 of # the Free Software Foundation; either version 2 of the License, or
# the License, or (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
# Sample of export and import of keys # Sample of export and import of keys
# It uses keys for joe@example.org generated by genkey.pl script # It uses keys for joe+pyme@example.org generated by genkey.py script
import sys import sys
from pyme import core import os
import pyme
core.check_version(None) user = "joe+pyme@example.org"
expkey = core.Data() with pyme.Context(armor=True) as c, pyme.Data() as expkey:
c = core.Context() print(" - Export %s's public keys - " % user)
c.set_armor(1) c.op_export(user, 0, expkey)
user = b"joe@example.org"
print(" - Export %s's public keys - " % user) # print out exported data to see how it looks in armor.
c.op_export(user, 0, expkey) 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)
# print out exported data to see how it looks in armor. # delete keys to ensure that they came from our imported data. Note
expkey.seek(0, 0) # that if joe's key has private part as well we can only delete both
expstring = expkey.read() # of them.
if expstring: with pyme.Context() as c:
print(expstring) # Note: We must not modify the key store during iteration,
else: # therfore, we explicitly make a list.
print("No %s's keys to export!" % user) keys = list(c.keylist(user))
sys.exit(0)
for k in keys:
c.op_delete(k, True)
# delete keys to ensure that they came from our imported data. with pyme.Context() as c:
# Note that since joe's key has private part as well we can only delete print(" - Import exported keys - ")
# both of them. As a side effect joe won't have private key for this c.op_import(expstring)
# exported public one. If it's Ok with you uncomment the next 4 lines. result = c.op_import_result()
# print " - Delete %s's public keys - " % user if result:
# for thekey in [x for x in c.op_keylist_all(user, 0)]: print(result)
# if not thekey.secret: else:
# c.op_delete(thekey, 1) sys.exit(" - No import result - ")
# initialize import data from a string as if it was read from a file.
newkey = core.Data(expstring)
print(" - Import exported keys - ")
c.op_import(newkey)
result = c.op_import_result()
# show the import result
if result:
print(" - Result of the import - ")
for k in dir(result):
if k not in result.__dict__ and not k.startswith("_"):
if k == "imports":
print(k, ":")
for impkey in result.__getattr__(k):
print(" fpr=%s result=%d status=%x" %
(impkey.fpr, impkey.result, impkey.status))
else:
print(k, ":", result.__getattr__(k))
else:
print(" - No import result - ")

View File

@ -1,31 +1,25 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org> # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from pyme import core, callbacks import pyme
# Initialize our context. # This is the example from the GPGME manual.
core.check_version(None)
c = core.Context()
c.set_armor(1)
c.set_progress_cb(callbacks.progress_stdout, None)
# This example from the GPGME manual
parms = """<GnupgKeyParms format="internal"> parms = """<GnupgKeyParms format="internal">
Key-Type: RSA Key-Type: RSA
@ -40,5 +34,8 @@ Expire-Date: 2020-12-31
</GnupgKeyParms> </GnupgKeyParms>
""" """
c.op_genkey(parms, None, None) with pyme.Context() as c:
print(c.op_genkey_result().fpr) c.set_progress_cb(pyme.callbacks.progress_stdout)
c.op_genkey(parms, None, None)
print("Generated key with fingerprint {0}.".format(
c.op_genkey_result().fpr))

View File

@ -1,58 +1,60 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA """Simple interactive editor to test editor scripts"""
import sys import sys
from pyme import core import pyme
from pyme.core import Data, Context import pyme.constants.status
from pyme.constants import status
core.check_version(None)
# Get names for the status codes # Get names for the status codes
stat2str = {} status2str = {}
for name in dir(status): for name in dir(pyme.constants.status):
if not name.startswith('__') and name != "util": if not name.startswith('__') and name != "util":
stat2str[getattr(status, name)] = name status2str[getattr(pyme.constants.status, name)] = name
# Print the output received since the last prompt before giving the new prompt
def edit_fnc(stat, args, helper):
global stat_strings
try:
while True:
helper["data"].seek(helper["skip"], 0)
data = helper["data"].read()
helper["skip"] += len(data)
sys.stdout.buffer.write(data)
return input("(%s) %s > " % (stat2str[stat], args))
except EOFError:
pass
# Simple interactive editor to test editor scripts
if len(sys.argv) != 2: if len(sys.argv) != 2:
sys.stderr.write("Usage: %s <Gpg key pattern>\n" % sys.argv[0]) sys.exit("Usage: %s <Gpg key pattern>\n" % sys.argv[0])
else:
c = Context() name = sys.argv[1]
out = Data()
c.op_keylist_start(sys.argv[1], 0) with pyme.Context() as c:
key = c.op_keylist_next() keys = list(c.keylist(name))
helper = {"skip": 0, "data": out} if len(keys) == 0:
c.op_edit(key, edit_fnc, helper, out) sys.exit("No key matching {}.".format(name))
print("[-- Final output --]") if len(keys) > 1:
out.seek(helper["skip"], 0) sys.exit("More than one key matching {}.".format(name))
sys.stdout.buffer.write(out.read())
key = keys[0]
print("Editing key {} ({}):".format(key.uids[0].uid, key.subkeys[0].fpr))
def edit_fnc(status, args):
print("Status: {} ({}), args: {} > ".format(
status2str[status], status, args), end='', flush=True)
if not 'GET' in status2str[status]:
# no prompt
print()
return None
try:
return input()
except EOFError:
return "quit"
c.op_edit(key, edit_fnc, None, sys.stdout)

View File

@ -1,32 +1,25 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright (C) 2002 John Goerzen
# <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or modify # Copyright (C) 2016 g10 Code GmbH
# it under the terms of the GNU General Public License as published by # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
# 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, # This program is free software; you can redistribute it and/or modify
# but WITHOUT ANY WARRANTY; without even the implied warranty of # it under the terms of the GNU General Public License as published by
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # the Free Software Foundation; either version 2 of the License, or
# GNU General Public License for more details. # (at your option) any later version.
# #
# You should have received a copy of the GNU General Public License # This program is distributed in the hope that it will be useful, but
# along with this program; if not, write to the Free Software # WITHOUT ANY WARRANTY; without even the implied warranty of
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # 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 <http://www.gnu.org/licenses/>.
import sys import sys
import os import pyme
from pyme import core, callbacks
from pyme.constants.sig import mode from pyme.constants.sig import mode
core.check_version(None) with pyme.Context() as c:
signed, _ = c.sign(b"Test message", mode=mode.CLEAR)
plain = core.Data(b"Test message") sys.stdout.buffer.write(signed)
sig = core.Data()
c = core.Context()
c.set_passphrase_cb(callbacks.passphrase_stdin, b'for signing')
c.op_sign(plain, sig, mode.CLEAR)
sig.seek(0, os.SEEK_SET)
sys.stdout.buffer.write(sig.read())

View File

@ -1,77 +1,39 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Sample of unattended signing/verifying of a message. # Sample of unattended signing/verifying of a message.
# It uses keys for joe@example.org generated by genkey.pl script # It uses keys for joe+pyme@example.org generated by genkey.py script
import sys import sys
import os import os
from pyme import core import pyme
from pyme.constants.sig import mode from pyme.constants.sig import mode
core.check_version(None) user = "joe+pyme"
plain = core.Data(b"Test message") with pyme.Context(pinentry_mode=pyme.constants.PINENTRY_MODE_LOOPBACK) as c:
sig = core.Data() keys = list(c.keylist(user))
c = core.Context() if len(keys) == 0:
user = "joe" sys.exit("No key matching {}.".format(user))
c.signers_clear() c.signers = keys[:1]
# Add joe@example.org's keys in the list of signers c.set_passphrase_cb(lambda *args: "Crypt0R0cks")
for sigkey in c.op_keylist_all(user, 1): signed_data, _ = c.sign(b"Test message", mode=mode.CLEAR)
if sigkey.can_sign:
c.signers_add(sigkey)
if not c.signers_enum(0):
print("No secret %s's keys suitable for signing!" % user)
sys.exit(0)
# This is a map between signer e-mail and its password data, result = c.verify(signed_data, verify=keys[:1])
passlist = { print("Data: {!r}\nSignature: {!s}".format(data, result.signatures[0]))
b"<joe@example.org>": b"Crypt0R0cks"
}
# callback will return password based on the e-mail listed in the hint.
c.set_passphrase_cb(lambda x,y,z: passlist[x[x.rindex("<"):]])
c.op_sign(plain, sig, mode.CLEAR)
# Print out the signature (don't forget to rewind since signing put sig at EOF)
sig.seek(0, os.SEEK_SET)
signedtext = sig.read()
sys.stdout.buffer.write(signedtext)
# Create Data with signed text.
sig2 = core.Data(signedtext)
plain2 = core.Data()
# Verify.
c.op_verify(sig2, None, plain2)
result = c.op_verify_result()
# List results for all signatures. Status equal 0 means "Ok".
for index, sign in enumerate(result.signatures):
print("signature", index, ":")
print(" summary: ", sign.summary)
print(" status: ", sign.status)
print(" timestamp: ", sign.timestamp)
print(" fingerprint:", sign.fpr)
print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid)
# Print "unsigned" text. Rewind since verify put plain2 at EOF.
plain2.seek(0, os.SEEK_SET)
print("\n")
sys.stdout.buffer.write(plain2.read())

View File

@ -1,52 +1,45 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org> # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, see <http://www.gnu.org/licenses/>.
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys import sys
import os import os
from pyme import core, errors import pyme
core.check_version(None) with pyme.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)
# Set up our input and output buffers. if not recipients:
sys.exit("No recipients.")
plain = core.Data('This is my message.') print("Encrypting for {}.".format(", ".join(k.uids[0].name
cipher = core.Data() for k in recipients)))
# Initialize our context. ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
sys.stdout.buffer.write(ciphertext)
c = core.Context()
c.set_armor(1)
# Set up the recipients.
sys.stdout.write("Enter name of your recipient: ")
sys.stdout.flush()
name = sys.stdin.readline().strip()
c.op_keylist_start(name, 0)
r = c.op_keylist_next()
if r == None:
print("The key for user \"%s\" was not found" % name)
else:
# Do the encryption.
try:
c.op_encrypt([r], 1, plain, cipher)
cipher.seek(0, os.SEEK_SET)
sys.stdout.buffer.write(cipher.read())
except errors.GPGMEError as ex:
print(ex.getstring())

View File

@ -1,62 +0,0 @@
#!/usr/bin/env python3
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys
import os
from pyme import core
from pyme.core import Data, Context
core.check_version(None)
class KeyEditor:
def __init__(self):
self.steps = ["fpr", "expire", "1", "primary", "quit"]
self.step = 0
def edit_fnc(self, status, args, out):
print("[-- Response --]")
out.seek(0, os.SEEK_SET)
sys.stdout.buffer.write(out.read())
print("[-- Code: %d, %s --]" % (status, args))
if args == "keyedit.prompt":
result = self.steps[self.step]
self.step += 1
elif args == "keyedit.save.okay":
result = "Y"
elif args == "keygen.valid":
result = "0"
else:
result = None
return result
if not os.getenv("GNUPGHOME"):
print("Please, set GNUPGHOME env.var. pointing to GPGME's tests/gpg dir")
else:
c = Context()
c.set_passphrase_cb(lambda x,y,z: "abc")
out = Data()
c.op_keylist_start(b"Alpha", 0)
key = c.op_keylist_next()
if not key:
sys.exit("Key Alpha not found. " +
"Did you point GNUPGHOME to GPGME's tests/gpg dir?")
c.op_edit(key, KeyEditor().edit_fnc, out, out)
print("[-- Last response --]")
out.seek(0, os.SEEK_SET)
sys.stdout.buffer.write(out.read())

View File

@ -1,47 +1,32 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# initial 20080124 bernhard@intevation.de
# 20080124-2: removed some superflous imports
# 20080703: adapted for pyme-0.8.0
# This script is Free Software under GNU GPL v>=2.
# #
# No modification made for python3 port, Bernhard can field this one # Copyright (C) 2016 g10 Code GmbH
# if it is still required. -- Ben McGinnes # Copyright (C) 2008 Bernhard Reiter <bernhard@intevation.de>
# #
"""A test applicaton for gpg_get_key() protocol.CMS. # 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 <http://www.gnu.org/licenses/>.
Tested on Debian Etch with """A test applicaton for the CMS protocol."""
pyme 0.8.0 (manually compiled)
libgpgme11 1.1.6-0kk2
gpgsm 2.0.9-0kk2
"""
import sys import sys
from pyme import core import pyme
from pyme.constants import protocol
def printgetkeyresults(keyfpr): if len(sys.argv) != 2:
"""Run gpgme_get_key().""" sys.exit("fingerprint or unique key ID for gpgme_get_key()")
# gpgme_check_version() necessary for initialisation according to with pyme.Context(protocol=pyme.constants.PROTOCOL_CMS) as c:
# gogme 1.1.6 and this is not done automatically in pyme-0.7.0 key = c.get_key(sys.argv[1], False)
print("gpgme version:", core.check_version(None))
c = core.Context()
c.set_protocol(protocol.CMS)
key = c.get_key(keyfpr, False)
print("got key: ", key.subkeys[0].fpr) print("got key: ", key.subkeys[0].fpr)
for uid in key.uids: for uid in key.uids:
print(uid.uid) print(uid.uid)
def main():
if len(sys.argv) < 2:
print("fingerprint or unique key ID for gpgme_get_key()")
sys.exit(1)
printgetkeyresults(sys.argv[1])
if __name__ == "__main__":
main()

View File

@ -1,27 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# initial 20080123 build from the example:
# very simple - probably INCOMPLETE
# 20080703 Bernhard
# added second usage for detached signatures.
# added output of signature.summary (another bitfield)
# printing signature bitfield in hex format
#
# #
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
# Copyright (c) 2008 Bernhard Reiter <bernhard@intevation.de> # Copyright (c) 2008 Bernhard Reiter <bernhard@intevation.de>
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful, but
# but WITHOUT ANY WARRANTY; without even the implied warranty of # WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# GNU General Public License for more details. # General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program; if not, see <http://www.gnu.org/licenses/>.
import sys import sys
import os import os
@ -36,60 +30,47 @@ def print_engine_infos():
print(engine.file_name, engine.version) print(engine.file_name, engine.version)
for proto in [protocol.OpenPGP, protocol.CMS]: for proto in [protocol.OpenPGP, protocol.CMS]:
print(core.get_protocol_name(proto), core.engine_check_version(proto)) print("Have {}? {}".format(core.get_protocol_name(proto),
core.engine_check_version(proto)))
def verifyprintdetails(sigfilename, filefilename=None): def verifyprintdetails(filename, detached_sig_filename=None):
"""Verify a signature, print a lot of details.""" """Verify a signature, print a lot of details."""
c = core.Context() with core.Context() as c:
# Create Data with signed text. # Verify.
sig2 = core.Data(file=sigfilename) data, result = c.verify(open(filename),
if filefilename: open(detached_sig_filename)
file2 = core.Data(file=filefilename) if detached_sig_filename else None)
plain2 = None
else:
file2 = None
plain2 = core.Data()
# Verify. # List results for all signatures. Status equal 0 means "Ok".
c.op_verify(sig2, file2, plain2) for index, sign in enumerate(result.signatures):
result = c.op_verify_result() print("signature", index, ":")
print(" summary: %#0x" % (sign.summary))
# List results for all signatures. Status equal 0 means "Ok". print(" status: %#0x" % (sign.status))
for index, sign in enumerate(result.signatures): print(" timestamp: ", sign.timestamp)
print("signature", index, ":") print(" fingerprint:", sign.fpr)
print(" summary: %#0x" % (sign.summary)) print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid)
print(" status: %#0x" % (sign.status))
print(" timestamp: ", sign.timestamp)
print(" fingerprint:", sign.fpr)
print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid)
# Print "unsigned" text if inline signature # Print "unsigned" text if inline signature
if plain2: if data:
#Rewind since verify put plain2 at EOF. sys.stdout.buffer.write(data)
plain2.seek(0, os.SEEK_SET)
print("\n")
sys.stdout.buffer.write(plain2.read())
def main(): def main():
print_engine_infos() print_engine_infos()
print() print()
argc= len(sys.argv) argc = len(sys.argv)
if argc < 2 or argc > 3: if argc < 2 or argc > 3:
print("need a filename for inline signature") sys.exit(
print("or two filename for detached signature and file to check") "Usage: {} <filename>[ <detached_signature_filename>]".format(
sys.exit(1) sys.argv[0]))
if argc == 2: if argc == 2:
print("trying to verify file: " + sys.argv[1]) print("trying to verify file {}.".format(sys.argv[1]))
verifyprintdetails(sys.argv[1]) verifyprintdetails(sys.argv[1])
if argc == 3: if argc == 3:
print("trying to verify signature %s for file %s" \ print("trying to verify signature {1} for file {0}.".format(*sys.argv))
% (sys.argv[1], sys.argv[2]))
verifyprintdetails(sys.argv[1], sys.argv[2]) verifyprintdetails(sys.argv[1], sys.argv[2])
if __name__ == "__main__": if __name__ == "__main__":