Use pysequoia for data encryption

This commit is contained in:
Wiktor Kwapisiewicz 2023-06-01 23:01:15 +02:00
parent 9071f5d56f
commit 9d47de4979
1 changed files with 10 additions and 3 deletions

View File

@ -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")