3 # Copyright (C) 2016 g10 Code GmbH
5 # This file is part of GPGME.
7 # GPGME is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # GPGME is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 # Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 from __future__ import absolute_import, print_function, unicode_literals
25 del absolute_import, print_function, unicode_literals
29 raise RuntimeError(msg)
32 def check_result(r, typ):
34 fail("Invalid signer found: {}".format(r.invalid_signers.fpr))
36 if len(r.signatures) != 2:
37 fail("Unexpected number of signatures created")
39 for signature in r.signatures:
40 if signature.type != typ:
41 fail("Wrong type of signature created")
43 if signature.pubkey_algo != gpg.constants.pk.DSA:
44 fail("Wrong pubkey algorithm reported: {}".format(
45 signature.pubkey_algo))
47 if signature.hash_algo != gpg.constants.md.SHA1:
48 fail("Wrong hash algorithm reported: {}".format(
51 if signature.sig_class != 1:
52 fail("Wrong signature class reported: got {}, want {}".format(
53 signature.sig_class, 1))
55 if signature.fpr not in ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
56 "23FD347A419429BACCD5E72D6BC4778054ACD246"):
57 fail("Wrong fingerprint reported: {}".format(signature.fpr))
65 c.op_keylist_start('', True)
66 keys.append(c.op_keylist_next())
67 keys.append(c.op_keylist_next())
70 c.signers_add(keys[0])
71 c.signers_add(keys[1])
73 for mode in (gpg.constants.sig.mode.NORMAL, gpg.constants.sig.mode.DETACH,
74 gpg.constants.sig.mode.CLEAR):
75 source = gpg.Data("Hallo Leute\n")
78 c.op_sign(source, sink, mode)
80 result = c.op_sign_result()
81 check_result(result, mode)
82 support.print_data(sink)
84 # Idiomatic interface.
85 with gpg.Context(armor=True, textmode=True, signers=keys) as c:
86 message = "Hallo Leute\n".encode()
87 signed, result = c.sign(message)
88 check_result(result, gpg.constants.sig.mode.NORMAL)
89 assert signed.find(b'BEGIN PGP MESSAGE') > 0, 'Message not found'
91 signed, result = c.sign(message, mode=gpg.constants.sig.mode.DETACH)
92 check_result(result, gpg.constants.sig.mode.DETACH)
93 assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'
95 signed, result = c.sign(message, mode=gpg.constants.sig.mode.CLEAR)
96 check_result(result, gpg.constants.sig.mode.CLEAR)
97 assert signed.find(b'BEGIN PGP SIGNED MESSAGE') > 0, 'Message not found'
98 assert signed.find(message) > 0, 'Message content not found'
99 assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'