blob: 1fcd83e3156d845e8d20d2128c3bbef6366f5dc8 (
plain)
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
59
60
|
package commands
import (
"context"
"encoding/gob"
"log/slog"
"os"
"path/filepath"
"github.com/nerdsec/goaes/internal"
"github.com/urfave/cli/v3"
)
func Decrypt(ctx context.Context, cmd *cli.Command) error {
source := cmd.StringArg("source")
destination := cmd.StringArg("destination")
if source == "" {
return cli.Exit("missing source", 2)
}
if destination == "" {
return cli.Exit("missing destination", 2)
}
source = filepath.Clean(source)
file, err := os.Open(source)
if err != nil {
return err
}
defer func() {
err := file.Close()
if err != nil {
slog.Error("failed to close file", "error", err)
}
}()
enc := gob.NewDecoder(file)
var encryptedPayload internal.EncryptedDataPayload
err = enc.Decode(&encryptedPayload)
if err != nil {
return err
}
passphrase := os.Getenv(PassphraseEnvVar)
plaintext, err := internal.Decrypt(passphrase, encryptedPayload.DEK, encryptedPayload.Payload, encryptedPayload.Salt)
if err != nil {
return err
}
err = os.WriteFile(destination, plaintext, fileMode)
if err != nil {
return err
}
return nil
}
|