From 9d47de4979aea1c4ac869ef74ed4ae95d42ed1cb Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Thu, 1 Jun 2023 23:01:15 +0200 Subject: [PATCH] Use pysequoia for data encryption --- GnuPG/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/GnuPG/__init__.py b/GnuPG/__init__.py index 920f83f..7a8dd47 100644 --- a/GnuPG/__init__.py +++ b/GnuPG/__init__.py @@ -27,7 +27,7 @@ import random import string import sys import logging - +from pysequoia import Cert, Store LINE_FINGERPRINT = 'fpr' LINE_USER_ID = 'uid' @@ -142,11 +142,15 @@ class GPGEncryptor: def __init__(self, keyhome, recipients=None, charset=None): """Initialise the wrapper.""" self._keyhome = keyhome + self._store = Store(keyhome) self._message = b'' self._recipients = list() + self._keys = list() self._charset = charset if recipients is not None: self._recipients.extend(recipients) + for recipient in recipients: + self._keys.append(store.get(recipient)) def update(self, message): """Append MESSAGE to buffer about to be encrypted.""" @@ -155,8 +159,11 @@ class GPGEncryptor: def encrypt(self): """Feed GnuPG with the message.""" p = subprocess.Popen(self._command(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - encdata = p.communicate(input=self._message)[0] - return (encdata, p.returncode) + try: + encrypted = pysequoia.encrypt(recipients = self._keys, bytes = self._message) + return (str(encrypted), 0) + except: + return ("", 1) def _command(self): cmd = _build_command(self._keyhome, "--trust-model", "always", "--batch", "--yes", "--pgp7", "--no-secmem-warning", "-a", "-e")