-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
decrypt.go
58 lines (53 loc) · 1.39 KB
/
decrypt.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
47
48
49
50
51
52
53
54
55
56
57
58
package age
import (
"bytes"
"context"
"fmt"
"io"
"os"
"filippo.io/age"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
)
// Decrypt will attempt to decrypt the given payload
func (a *Age) Decrypt(ctx context.Context, ciphertext []byte) ([]byte, error) {
if !ctxutil.HasPasswordCallback(ctx) {
debug.Log("no password callback found, redirecting to askPass")
ctx = ctxutil.WithPasswordCallback(ctx, func(prompt string, _ bool) ([]byte, error) {
pw, err := a.askPass.Passphrase(prompt, fmt.Sprintf("to load the keyring at %s", a.keyring), false)
return []byte(pw), err
})
}
ids, err := a.getAllIds(ctx)
if err != nil {
return nil, err
}
return a.decrypt(ciphertext, ids...)
}
func (a *Age) decrypt(ciphertext []byte, ids ...age.Identity) ([]byte, error) {
out := &bytes.Buffer{}
f := bytes.NewReader(ciphertext)
r, err := age.Decrypt(f, ids...)
if err != nil {
return nil, err
}
if _, err := io.Copy(out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func (a *Age) decryptFile(ctx context.Context, filename string) ([]byte, error) {
ciphertext, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
pw, err := ctxutil.GetPasswordCallback(ctx)(filename, false)
if err != nil {
return nil, err
}
id, err := age.NewScryptIdentity(string(pw))
if err != nil {
return nil, err
}
return a.decrypt(ciphertext, id)
}