Blame lang/python/examples/howto/export-secret-key.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 os
Packit Service 30b792
import os.path
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
print("""
Packit Service 30b792
This script exports one or more secret keys.
Packit Service 30b792
Packit Service 30b792
The gpg-agent and pinentry are invoked to authorise the export.
Packit Service 30b792
""")
Packit Service 30b792
Packit Service 30b792
c = gpg.Context(armor=True)
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) >= 4:
Packit Service 30b792
    keyfile = sys.argv[1]
Packit Service 30b792
    logrus = sys.argv[2]
Packit Service 30b792
    homedir = sys.argv[3]
Packit Service 30b792
elif len(sys.argv) == 3:
Packit Service 30b792
    keyfile = sys.argv[1]
Packit Service 30b792
    logrus = sys.argv[2]
Packit Service 30b792
    homedir = input("Enter the GPG configuration directory path (optional): ")
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    keyfile = sys.argv[1]
Packit Service 30b792
    logrus = input("Enter the UID matching the secret key(s) to export: ")
Packit Service 30b792
    homedir = input("Enter the GPG configuration directory path (optional): ")
Packit Service 30b792
else:
Packit Service 30b792
    keyfile = input("Enter the path and filename to save the secret key to: ")
Packit Service 30b792
    logrus = input("Enter the UID matching the secret key(s) to export: ")
Packit Service 30b792
    homedir = input("Enter the GPG configuration directory path (optional): ")
Packit Service 30b792
Packit Service 30b792
if len(homedir) == 0:
Packit Service 30b792
    homedir = None
Packit Service 30b792
elif homedir.startswith("~"):
Packit Service 30b792
    userdir = os.path.expanduser(homedir)
Packit Service 30b792
    if os.path.exists(userdir) is True:
Packit Service 30b792
        homedir = os.path.realpath(userdir)
Packit Service 30b792
    else:
Packit Service 30b792
        homedir = None
Packit Service 30b792
else:
Packit Service 30b792
    homedir = os.path.realpath(homedir)
Packit Service 30b792
Packit Service 30b792
if homedir is not None and os.path.exists(homedir) is False:
Packit Service 30b792
    homedir = None
Packit Service 30b792
elif homedir is not None and os.path.exists(homedir) is True:
Packit Service 30b792
    if os.path.isdir(homedir) is False:
Packit Service 30b792
        homedir = None
Packit Service 30b792
    else:
Packit Service 30b792
        pass
Packit Service 30b792
Packit Service 30b792
if homedir is not None:
Packit Service 30b792
    c.home_dir = homedir
Packit Service 30b792
else:
Packit Service 30b792
    pass
Packit Service 30b792
Packit Service 30b792
try:
Packit Service 30b792
    result = c.key_export_secret(pattern=logrus)
Packit Service 30b792
except:
Packit Service 30b792
    result = c.key_export_secret(pattern=None)
Packit Service 30b792
Packit Service 30b792
if result is not None:
Packit Service 30b792
    with open(keyfile, "wb") as f:
Packit Service 30b792
        f.write(result)
Packit Service 30b792
    os.chmod(keyfile, 0o600)
Packit Service 30b792
else:
Packit Service 30b792
    pass