Imported Upstream version 1.12.0
[platform/upstream/gpgme.git] / lang / python / examples / signverify.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2016 g10 Code GmbH
4 # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19 # Sample of unattended signing/verifying of a message.
20 # It uses keys for joe+gpg@example.org generated by genkey.py script
21
22 from __future__ import absolute_import, print_function, unicode_literals
23
24 import sys
25 import gpg
26 from gpg.constants.sig import mode
27
28 del absolute_import, print_function, unicode_literals
29
30 user = "joe+gpg"
31
32 with gpg.Context(pinentry_mode=gpg.constants.PINENTRY_MODE_LOOPBACK) as c:
33     keys = list(c.keylist(user))
34     if len(keys) == 0:
35         sys.exit("No key matching {}.".format(user))
36
37     c.signers = keys[:1]
38     c.set_passphrase_cb(lambda *args: "Crypt0R0cks")
39     signed_data, _ = c.sign(b"Test message", mode=mode.CLEAR)
40
41     data, result = c.verify(signed_data, verify=keys[:1])
42     print("Data: {!r}\nSignature: {!s}".format(data, result.signatures[0]))