|
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 |
from groups import group_lists
|
|
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 |
Uses the groups module to encrypt to multiple recipients.
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
"""
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
c = gpg.Context(armor=True)
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
if len(sys.argv) > 3:
|
|
Packit Service |
30b792 |
group_id = sys.argv[1]
|
|
Packit Service |
30b792 |
filepath = sys.argv[2:]
|
|
Packit Service |
30b792 |
elif len(sys.argv) == 3:
|
|
Packit Service |
30b792 |
group_id = sys.argv[1]
|
|
Packit Service |
30b792 |
filepath = sys.argv[2]
|
|
Packit Service |
30b792 |
elif len(sys.argv) == 2:
|
|
Packit Service |
30b792 |
group_id = sys.argv[1]
|
|
Packit Service |
30b792 |
filepath = input("Enter the filename to encrypt: ")
|
|
Packit Service |
30b792 |
else:
|
|
Packit Service |
30b792 |
group_id = input("Enter the group name to encrypt to: ")
|
|
Packit Service |
30b792 |
filepath = input("Enter the filename to encrypt: ")
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
with open(filepath, "rb") as f:
|
|
Packit Service |
30b792 |
text = f.read()
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
for i in range(len(group_lists)):
|
|
Packit Service |
30b792 |
if group_lists[i][0] == group_id:
|
|
Packit Service |
30b792 |
klist = group_lists[i][1]
|
|
Packit Service |
30b792 |
else:
|
|
Packit Service |
30b792 |
klist = None
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
logrus = []
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
if klist is not None:
|
|
Packit Service |
30b792 |
for i in range(len(klist)):
|
|
Packit Service |
30b792 |
apattern = list(c.keylist(pattern=klist[i], secret=False))
|
|
Packit Service |
30b792 |
if apattern[0].can_encrypt == 1:
|
|
Packit Service |
30b792 |
logrus.append(apattern[0])
|
|
Packit Service |
30b792 |
else:
|
|
Packit Service |
30b792 |
pass
|
|
Packit Service |
30b792 |
try:
|
|
Packit Service |
30b792 |
ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
|
|
Packit Service |
30b792 |
add_encrypt_to=True)
|
|
Packit Service |
30b792 |
except gpg.errors.InvalidRecipients as e:
|
|
Packit Service |
30b792 |
for i in range(len(e.recipients)):
|
|
Packit Service |
30b792 |
for n in range(len(logrus)):
|
|
Packit Service |
30b792 |
if logrus[n].fpr == e.recipients[i].fpr:
|
|
Packit Service |
30b792 |
logrus.remove(logrus[n])
|
|
Packit Service |
30b792 |
else:
|
|
Packit Service |
30b792 |
pass
|
|
Packit Service |
30b792 |
try:
|
|
Packit Service |
30b792 |
ciphertext, result, sign_result = c.encrypt(text,
|
|
Packit Service |
30b792 |
recipients=logrus,
|
|
Packit Service |
30b792 |
add_encrypt_to=True,
|
|
Packit Service |
30b792 |
always_trust=True)
|
|
Packit Service |
30b792 |
except:
|
|
Packit Service |
30b792 |
pass
|
|
Packit Service |
30b792 |
with open("{0}.asc".format(filepath), "wb") as f:
|
|
Packit Service |
30b792 |
f.write(ciphertext)
|
|
Packit Service |
30b792 |
else:
|
|
Packit Service |
30b792 |
pass
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
# EOF
|