|
Packit |
d7e8d0 |
#!/usr/bin/env python
|
|
Packit |
d7e8d0 |
#
|
|
Packit |
d7e8d0 |
# Copyright (C) 2016 g10 Code GmbH
|
|
Packit |
d7e8d0 |
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
|
|
Packit |
d7e8d0 |
#
|
|
Packit |
d7e8d0 |
# This program is free software; you can redistribute it and/or modify
|
|
Packit |
d7e8d0 |
# it under the terms of the GNU General Public License as published by
|
|
Packit |
d7e8d0 |
# the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
d7e8d0 |
# (at your option) any later version.
|
|
Packit |
d7e8d0 |
#
|
|
Packit |
d7e8d0 |
# This program is distributed in the hope that it will be useful, but
|
|
Packit |
d7e8d0 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
d7e8d0 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
d7e8d0 |
# General Public License for more details.
|
|
Packit |
d7e8d0 |
#
|
|
Packit |
d7e8d0 |
# You should have received a copy of the GNU General Public License
|
|
Packit |
d7e8d0 |
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
# Sample of export and import of keys
|
|
Packit |
d7e8d0 |
# It uses keys for joe+gpg@example.org generated by genkey.py script
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
from __future__ import absolute_import, print_function, unicode_literals
|
|
Packit |
d7e8d0 |
del absolute_import, print_function, unicode_literals
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
import sys
|
|
Packit |
d7e8d0 |
import os
|
|
Packit |
d7e8d0 |
import gpg
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
user = "joe+gpg@example.org"
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
with gpg.Context(armor=True) as c, gpg.Data() as expkey:
|
|
Packit |
d7e8d0 |
print(" - Export %s's public keys - " % user)
|
|
Packit |
d7e8d0 |
c.op_export(user, 0, expkey)
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
# print out exported data to see how it looks in armor.
|
|
Packit |
d7e8d0 |
expkey.seek(0, os.SEEK_SET)
|
|
Packit |
d7e8d0 |
expstring = expkey.read()
|
|
Packit |
d7e8d0 |
if expstring:
|
|
Packit |
d7e8d0 |
sys.stdout.buffer.write(expstring)
|
|
Packit |
d7e8d0 |
else:
|
|
Packit |
d7e8d0 |
sys.exit("No %s's keys to export!" % user)
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
# delete keys to ensure that they came from our imported data. Note
|
|
Packit |
d7e8d0 |
# that if joe's key has private part as well we can only delete both
|
|
Packit |
d7e8d0 |
# of them.
|
|
Packit |
d7e8d0 |
with gpg.Context() as c:
|
|
Packit |
d7e8d0 |
# Note: We must not modify the key store during iteration,
|
|
Packit |
d7e8d0 |
# therefore, we explicitly make a list.
|
|
Packit |
d7e8d0 |
keys = list(c.keylist(user))
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
for k in keys:
|
|
Packit |
d7e8d0 |
c.op_delete(k, True)
|
|
Packit |
d7e8d0 |
|
|
Packit |
d7e8d0 |
with gpg.Context() as c:
|
|
Packit |
d7e8d0 |
print(" - Import exported keys - ")
|
|
Packit |
d7e8d0 |
c.op_import(expstring)
|
|
Packit |
d7e8d0 |
result = c.op_import_result()
|
|
Packit |
d7e8d0 |
if result:
|
|
Packit |
d7e8d0 |
print(result)
|
|
Packit |
d7e8d0 |
else:
|
|
Packit |
d7e8d0 |
sys.exit(" - No import result - ")
|