this repo has no description
1package crypto
2
3import (
4 "bytes"
5 "fmt"
6 "io"
7 "os"
8
9 "filippo.io/age"
10)
11
12// EncryptFile encrypts the given file using the given identities
13func EncryptFile(path string, recipients []age.Recipient) ([]byte, error) {
14 data, err := os.ReadFile(path)
15 if err != nil {
16 return nil, fmt.Errorf("failed to read file: %w", err)
17 }
18 return EncryptBytes(data, recipients)
19}
20
21func EncryptBytes(data []byte, recipients []age.Recipient) ([]byte, error) {
22
23 src := bytes.NewReader(data)
24 dst := new(bytes.Buffer)
25
26 enc, err := age.Encrypt(dst, recipients...)
27 if err != nil {
28 return nil, fmt.Errorf("initializing age enc failed: %w", err)
29 }
30
31 if _, err := io.Copy(enc, src); err != nil {
32 enc.Close()
33 return nil, fmt.Errorf("encryption failed: %w", err)
34 }
35
36 if err := enc.Close(); err != nil {
37 return nil, fmt.Errorf("finalizing encryption failed: %w", err)
38 }
39
40 return dst.Bytes(), nil
41}