WIP: Use PySequoia for OpenPGP features #127

Draft
wiktor wants to merge 3 commits from wiktor/gpg-lacre:use-pysequoia into main
2 changed files with 16 additions and 5 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")
@ -190,8 +197,11 @@ class GPGDecryptor:
def decrypt(self):
"""Decrypt the message."""
p = subprocess.Popen(self._command(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
decdata = p.communicate(input=self._message)[0]
return (decdata, p.returncode)
try:
decrypted = pysequoia.decrypt(bytes = self._message)
return (decrypted.bytes.decode("utf8"), 0)
except:
return ("", 1)
def _command(self):
return _build_command(self._keyhome, "--trust-model", "always", "--batch", "--yes", "--no-secmem-warning", "-a", "-d")

View File

@ -4,3 +4,4 @@ Markdown==3.4.1
M2Crypto==0.38.0
requests==2.27.1
watchdog==2.1.9
pysequoia=0.1.19