8f451d7cb5b78da7bb0d4fa3da517eaf6251e9e5
[platform/upstream/gpgme.git] / lang / python / examples / simple.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2016 g10 Code GmbH
4 # Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
5 # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it 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.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20 from __future__ import absolute_import, print_function, unicode_literals
21 del absolute_import, print_function, unicode_literals
22
23 import sys
24 import gpg
25
26 with gpg.Context(armor=True) as c:
27     recipients = []
28     print("Enter name of your recipient(s), end with a blank line.")
29     while True:
30         line = input()
31         if not line:
32             break
33         new = list(c.keylist(line))
34         if not new:
35             print("Matched no known keys.")
36         else:
37             print("Adding {}.".format(", ".join(k.uids[0].name for k in new)))
38             recipients.extend(new)
39
40     if not recipients:
41         sys.exit("No recipients.")
42
43     print("Encrypting for {}.".format(", ".join(k.uids[0].name
44                                                 for k in recipients)))
45
46     ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
47     sys.stdout.buffer.write(ciphertext)