-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencrypt.go
46 lines (41 loc) · 1.04 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dolores
import (
"bytes"
"fmt"
"filippo.io/age"
"filippo.io/age/armor"
)
type Encryptor struct {
recipients []age.Recipient
}
func (e *Encryptor) Encrypt(vars []Variable) ([]byte, error) {
buf := &bytes.Buffer{}
arm := armor.NewWriter(buf)
w, err := age.Encrypt(arm, e.recipients...)
if err != nil {
return nil, fmt.Errorf("error encrypting: %w", err)
}
for _, v := range vars {
if _, err := w.Write(v.Data()); err != nil {
return nil, fmt.Errorf("error writing data: %w", err)
}
}
if err := w.Close(); err != nil {
return nil, fmt.Errorf("error closing writer: %w", err)
}
if err := arm.Close(); err != nil {
return nil, fmt.Errorf("error closing arm writer: %w", err)
}
return buf.Bytes(), nil
}
func NewEncryptor(keys ...string) (*Encryptor, error) {
recps := make([]age.Recipient, len(keys))
for i, key := range keys {
recp, err := age.ParseX25519Recipient(key)
if err != nil {
return nil, fmt.Errorf("error parsing %d key: %w", i, err)
}
recps[i] = recp
}
return &Encryptor{recipients: recps}, nil
}