@@ -88,6 +88,41 @@ print(f"Clear signed: {clear}")
8888assert " PGP SIGNED MESSAGE" in str (clear)
8989```
9090
91+ ### sign_file
92+
93+ Signs data from a file and writes the signed output to another file:
94+
95+ ``` python
96+ from pysequoia import sign_file, SignatureMode
97+ import tempfile, os
98+
99+ s = Cert.from_file(" signing-key.asc" )
100+
101+ # create a file with data to sign
102+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .txt" ) as inp:
103+ inp.write(" data to be signed" .encode(" utf8" ))
104+ input_path = inp.name
105+
106+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .pgp" ) as out:
107+ output_path = out.name
108+
109+ sign_file(s.secrets.signer(), input_path, output_path)
110+ signed = open (output_path, " rb" ).read()
111+ assert b " PGP MESSAGE" in signed
112+
113+ # detached signature to file
114+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .sig" ) as out:
115+ detached_path = out.name
116+
117+ sign_file(s.secrets.signer(), input_path, detached_path, mode = SignatureMode.DETACHED )
118+ detached = open (detached_path, " rb" ).read()
119+ assert b " PGP SIGNATURE" in detached
120+
121+ os.unlink(input_path)
122+ os.unlink(output_path)
123+ os.unlink(detached_path)
124+ ```
125+
91126### verify
92127
93128Verifies signed data and returns verified data:
@@ -167,6 +202,33 @@ print(f"Encrypted data: {encrypted}")
167202
168203The ` signer ` argument is optional and when omitted the function will return an unsigned (but encrypted) message.
169204
205+ ### encrypt_file
206+
207+ Encrypts data from a file and writes the encrypted output to another file:
208+
209+ ``` python
210+ from pysequoia import encrypt_file
211+ import tempfile, os
212+
213+ s = Cert.from_file(" passwd.pgp" )
214+ r = Cert.from_bytes(open (" wiktor.asc" , " rb" ).read())
215+
216+ # create a file with content to encrypt
217+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .txt" ) as inp:
218+ inp.write(" content to encrypt" .encode(" utf8" ))
219+ input_path = inp.name
220+
221+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .pgp" ) as out:
222+ output_path = out.name
223+
224+ encrypt_file(signer = s.secrets.signer(" hunter22" ), recipients = [r], input = input_path, output = output_path)
225+ encrypted = open (output_path, " rb" ).read()
226+ assert b " PGP MESSAGE" in encrypted
227+
228+ os.unlink(input_path)
229+ os.unlink(output_path)
230+ ```
231+
170232### decrypt
171233
172234Decrypts plain data:
@@ -216,6 +278,81 @@ assert decrypted.valid_sigs[0].signing_key == sender.fingerprint
216278
217279Here, the same remarks as to [ ` verify ` ] ( #verify ) also apply.
218280
281+ ### decrypt_file
282+
283+ Decrypts data from a file and writes the decrypted output to another file:
284+
285+ ``` python
286+ from pysequoia import decrypt_file
287+ import tempfile, os
288+
289+ sender = Cert.from_file(" no-passwd.pgp" )
290+ receiver = Cert.from_file(" passwd.pgp" )
291+
292+ content = " Red Green Blue"
293+
294+ encrypted = encrypt(recipients = [receiver], bytes = content.encode(" utf8" ))
295+
296+ # write encrypted data to a file
297+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .pgp" ) as inp:
298+ inp.write(encrypted)
299+ input_path = inp.name
300+
301+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .txt" ) as out:
302+ output_path = out.name
303+
304+ decrypted = decrypt_file(decryptor = receiver.secrets.decryptor(" hunter22" ), input = input_path, output = output_path)
305+
306+ # content is written to the output file, not returned in memory
307+ assert decrypted.bytes is None
308+
309+ # read decrypted content from the output file
310+ assert open (output_path, " rb" ).read().decode(" utf8" ) == content
311+
312+ # this message did not contain any valid signatures
313+ assert len (decrypted.valid_sigs) == 0
314+
315+ os.unlink(input_path)
316+ os.unlink(output_path)
317+ ```
318+
319+ Decrypt file can also verify signatures while decrypting:
320+
321+ ``` python
322+ from pysequoia import decrypt_file
323+ import tempfile, os
324+
325+ sender = Cert.from_file(" no-passwd.pgp" )
326+ receiver = Cert.from_file(" passwd.pgp" )
327+
328+ content = " Red Green Blue"
329+
330+ encrypted = encrypt(signer = sender.secrets.signer(), recipients = [receiver], bytes = content.encode(" utf8" ))
331+
332+ # write encrypted data to a file
333+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .pgp" ) as inp:
334+ inp.write(encrypted)
335+ input_path = inp.name
336+
337+ with tempfile.NamedTemporaryFile(delete = False , suffix = " .txt" ) as out:
338+ output_path = out.name
339+
340+ def get_certs (key_ids ):
341+ print (f " For verification after decryption, we need these keys: { key_ids} " )
342+ return [sender]
343+
344+ decrypted = decrypt_file(decryptor = receiver.secrets.decryptor(" hunter22" ), input = input_path, output = output_path, store = get_certs)
345+
346+ assert open (output_path, " rb" ).read().decode(" utf8" ) == content
347+
348+ # let's check the valid signature's certificate and signing subkey fingerprints
349+ assert decrypted.valid_sigs[0 ].certificate == sender.fingerprint
350+ assert decrypted.valid_sigs[0 ].signing_key == sender.fingerprint
351+
352+ os.unlink(input_path)
353+ os.unlink(output_path)
354+ ```
355+
219356## Certificates
220357
221358The ` Cert ` class represents one OpenPGP certificate (commonly called a
0 commit comments