Blame lang/python/examples/simple.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
#
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
Packit Service 672cf4
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
Packit Service 672cf4
#
Packit Service 672cf4
# This program is free software; you can redistribute it and/or modify
Packit Service 672cf4
# it under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# This program is distributed in the hope that it will be useful, but
Packit Service 672cf4
# WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
# General Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU General Public License
Packit Service 6c01f9
# along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 672cf4
Packit Service 672cf4
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 6c01f9
del absolute_import, print_function, unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import sys
Packit Service 672cf4
import gpg
Packit Service 672cf4
Packit Service 672cf4
with gpg.Context(armor=True) as c:
Packit Service 672cf4
    recipients = []
Packit Service 672cf4
    print("Enter name of your recipient(s), end with a blank line.")
Packit Service 672cf4
    while True:
Packit Service 672cf4
        line = input()
Packit Service 672cf4
        if not line:
Packit Service 672cf4
            break
Packit Service 672cf4
        new = list(c.keylist(line))
Packit Service 672cf4
        if not new:
Packit Service 672cf4
            print("Matched no known keys.")
Packit Service 672cf4
        else:
Packit Service 672cf4
            print("Adding {}.".format(", ".join(k.uids[0].name for k in new)))
Packit Service 672cf4
            recipients.extend(new)
Packit Service 672cf4
Packit Service 672cf4
    if not recipients:
Packit Service 672cf4
        sys.exit("No recipients.")
Packit Service 672cf4
Packit Service 6c01f9
    print("Encrypting for {}.".format(", ".join(k.uids[0].name
Packit Service 6c01f9
                                                for k in recipients)))
Packit Service 672cf4
Packit Service 672cf4
    ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
Packit Service 672cf4
    sys.stdout.buffer.write(ciphertext)