Blame lang/python/examples/howto/detach-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
# 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
import gpg
Packit Service 30b792
import sys
Packit Service 30b792
Packit Service 30b792
"""
Packit Service 30b792
Signs a file with a specified key.  If entering both the key and the filename
Packit Service 30b792
on the command line, the key must be entered first.
Packit Service 30b792
Packit Service 30b792
Will produce both an ASCII armoured and GPG binary format copy of the detached
Packit Service 30b792
signature file.
Packit Service 30b792
"""
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) > 3:
Packit Service 30b792
    logrus = sys.argv[1]
Packit Service 30b792
    filename = " ".join(sys.argv[2:])
Packit Service 30b792
elif len(sys.argv) == 3:
Packit Service 30b792
    logrus = sys.argv[1]
Packit Service 30b792
    filename = sys.argv[2]
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    logrus = sys.argv[1]
Packit Service 30b792
    filename = input("Enter the path and filename to sign: ")
Packit Service 30b792
else:
Packit Service 30b792
    logrus = input("Enter the fingerprint or key ID to sign with: ")
Packit Service 30b792
    filename = input("Enter the path and filename to sign: ")
Packit Service 30b792
Packit Service 30b792
with open(filename, "rb") as f:
Packit Service 30b792
    text = f.read()
Packit Service 30b792
Packit Service 30b792
key = list(gpg.Context().keylist(pattern=logrus))
Packit Service 30b792
Packit Service 30b792
with gpg.Context(armor=True, signers=key) as ca:
Packit Service 30b792
    signed_data, result = ca.sign(text, mode=gpg.constants.sig.mode.DETACH)
Packit Service 30b792
    with open("{0}.asc".format(filename), "wb") as fa:
Packit Service 30b792
        fa.write(signed_data)
Packit Service 30b792
Packit Service 30b792
with gpg.Context(signers=key) as cb:
Packit Service 30b792
    signed_data, result = cb.sign(text, mode=gpg.constants.sig.mode.DETACH)
Packit Service 30b792
    with open("{0}.sig".format(filename), "wb") as fb:
Packit Service 30b792
        fb.write(signed_data)