Blame lang/python/examples/low_level-encrypt_to_all.py

Packit Service 30b792
#!/usr/bin/env python
Packit Service 30b792
#
Packit Service 30b792
# Copyright (C) 2016 g10 Code GmbH
Packit Service 30b792
# Copyright (C) 2008 Igor Belyi <belyi@users.sourceforge.net>
Packit Service 30b792
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify
Packit Service 30b792
# it under the terms of the GNU General Public License as published by
Packit Service 30b792
# the Free Software Foundation; either version 2 of the License, or
Packit Service 30b792
# (at your option) any later version.
Packit Service 30b792
#
Packit Service 30b792
# This program is distributed in the hope that it will be useful, but
Packit Service 30b792
# WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 30b792
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 30b792
# General Public License for more details.
Packit Service 30b792
#
Packit Service 30b792
# You should have received a copy of the GNU General Public License
Packit Service 30b792
# along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit Service 30b792
"""
Packit Service 30b792
This program will try to encrypt a simple message to each key on your
Packit Service 30b792
keyring.  If your keyring has any invalid keys on it, those keys will
Packit Service 30b792
be skipped and it will re-try the encryption."""
Packit Service 30b792
Packit Service 30b792
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 30b792
Packit Service 30b792
import sys
Packit Service 30b792
import gpg
Packit Service 30b792
Packit Service 30b792
del absolute_import, print_function, unicode_literals
Packit Service 30b792
Packit Service 30b792
with gpg.Context(armor=True) as c:
Packit Service 30b792
    recipients = list()
Packit Service 30b792
    for key in c.keylist():
Packit Service 30b792
        valid = 0
Packit Service 30b792
        if any(sk.can_encrypt for sk in key.subkeys):
Packit Service 30b792
            recipients.append(key)
Packit Service 30b792
            print("Adding recipient {0}.".format(key.uids[0].uid))
Packit Service 30b792
Packit Service 30b792
    ciphertext = None
Packit Service 30b792
    while not ciphertext:
Packit Service 30b792
        print("Encrypting to %d recipients" % len(recipients))
Packit Service 30b792
        try:
Packit Service 30b792
            ciphertext, _, _ = c.encrypt(
Packit Service 30b792
                b'This is my message.', recipients=recipients)
Packit Service 30b792
        except gpg.errors.InvalidRecipients as e:
Packit Service 30b792
            print("Encryption failed for these keys:\n{0!s}".format(e))
Packit Service 30b792
Packit Service 30b792
            # filter out the bad keys
Packit Service 30b792
            bad_keys = {bad.fpr for bad in e.recipients}
Packit Service 30b792
            recipients = [
Packit Service 30b792
                r for r in recipients if not r.subkeys[0].fpr in bad_keys
Packit Service 30b792
            ]
Packit Service 30b792
Packit Service 30b792
    sys.stdout.buffer.write(ciphertext)