summaryrefslogtreecommitdiff
path: root/cmd/goaes/commands/encrypt.go
blob: 47809c4a7a4d8ab0cc923f130aa2af8768eb5e56 (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
package commands

import (
	"bytes"
	"context"
	"encoding/gob"
	"os"

	"github.com/nerdsec/goaes/internal"
	"github.com/urfave/cli/v3"
)

func Encrypt(ctx context.Context, cmd *cli.Command) error {
	source := cmd.String("source")
	destination := cmd.String("destination")

	plaintext, err := os.ReadFile(source)
	if err != nil {
		return err
	}

	payload, err := internal.Encrypt(plaintext)
	if err != nil {
		return err
	}

	var dataBuffer bytes.Buffer
	enc := gob.NewEncoder(&dataBuffer)

	err = enc.Encode(payload)
	if err != nil {
		return err
	}

	err = os.WriteFile(destination, dataBuffer.Bytes(), 0666)
	if err != nil {
		return err
	}

	return nil
}