Blame lang/python/examples/howto/encrypt-sign-file.py

Packit Service 30b792
#!/usr/bin/env python3
Packit Service 30b792
# -*- coding: utf-8 -*-
Packit Service 30b792
Packit Service 30b792
from __future__ import absolute_import, division, unicode_literals
Packit Service 30b792
Packit Service 30b792
import gpg
Packit Service 30b792
import sys
Packit Service 30b792
Packit Service 30b792
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU General Public License as published by the Free Software
Packit Service 30b792
# Foundation; either version 2 of the License, or (at your option) any later
Packit Service 30b792
# version.
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU Lesser General Public License as published by the Free
Packit Service 30b792
# Software Foundation; either version 2.1 of the License, or (at your option)
Packit Service 30b792
# any later version.
Packit Service 30b792
#
Packit Service 30b792
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 30b792
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Packit Service 30b792
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public License for more details.
Packit Service 30b792
#
Packit Service 30b792
# You should have received a copy of the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public along with this program; if not, see
Packit Service 30b792
# <https://www.gnu.org/licenses/>.
Packit Service 30b792
Packit Service 30b792
"""
Packit Service 30b792
Signs and encrypts a file to a specified key.  If entering both the key and the
Packit Service 30b792
filename on the command line, the key must be entered first.
Packit Service 30b792
Packit Service 30b792
Signs with and also encrypts to the default key of the user invoking the
Packit Service 30b792
script.  Will treat all recipients as trusted to permit encryption.
Packit Service 30b792
Packit Service 30b792
Will produce both an ASCII armoured and GPG binary format copy of the signed
Packit Service 30b792
and encrypted file.
Packit Service 30b792
"""
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) > 3:
Packit Service 30b792
    a_key = sys.argv[1]
Packit Service 30b792
    filename = " ".join(sys.argv[2:])
Packit Service 30b792
elif len(sys.argv) == 3:
Packit Service 30b792
    a_key = sys.argv[1]
Packit Service 30b792
    filename = sys.argv[2]
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    a_key = sys.argv[1]
Packit Service 30b792
    filename = input("Enter the path and filename to encrypt: ")
Packit Service 30b792
else:
Packit Service 30b792
    a_key = input("Enter the fingerprint or key ID to encrypt to: ")
Packit Service 30b792
    filename = input("Enter the path and filename to encrypt: ")
Packit Service 30b792
Packit Service 30b792
rkey = list(gpg.Context().keylist(pattern=a_key, secret=False))
Packit Service 30b792
with open(filename, "rb") as f:
Packit Service 30b792
    text = f.read()
Packit Service 30b792
Packit Service 30b792
with gpg.Context(armor=True) as ca:
Packit Service 30b792
    ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey,
Packit Service 30b792
                                                 always_trust=True,
Packit Service 30b792
                                                 add_encrypt_to=True)
Packit Service 30b792
    with open("{0}.asc".format(filename), "wb") as fa:
Packit Service 30b792
        fa.write(ciphertext)
Packit Service 30b792
Packit Service 30b792
with gpg.Context() as cg:
Packit Service 30b792
    ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey,
Packit Service 30b792
                                                 always_trust=True,
Packit Service 30b792
                                                 add_encrypt_to=True)
Packit Service 30b792
    with open("{0}.gpg".format(filename), "wb") as fg:
Packit Service 30b792
        fg.write(ciphertext)