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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"gitlab.com/whiteboardguy/eno/internal/config"
"gitlab.com/whiteboardguy/eno/internal/crud"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
var encryptCmd = &cobra.Command{
Use: "encrypt <file>",
Short: "encrypt a file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("failed to get output flag: %v", err)
}
if output == "" {
output = args[0] + ".eno"
}
if _, err := os.Stat(output); err == nil {
ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("write aborted")
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("failed to read input file: %v", err)
}
crypt.TryMlock(fileBytes)
defer clear(fileBytes)
sshFlag, err := cmd.Flags().GetBool("ssh")
if err != nil {
return fmt.Errorf("failed to get ssh flag: %v", err)
}
if sshFlag {
if config.Conf.DefaultSSHPubPath == "" {
return fmt.Errorf("SSH not configured. Run 'eno ssh init' first")
}
defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath)
if err != nil {
return fmt.Errorf("failed to read default SSH public key: %v", err)
}
pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes)
if err != nil {
return err
}
if len(pubKeys) == 0 {
return fmt.Errorf("at least one recipient public key is required")
}
encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys)
if err != nil {
clear(fileBytes)
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
if err := os.WriteFile(output, encrypted, 0600); err != nil {
return fmt.Errorf("failed to write output file: %v", err)
}
return nil
}
fmt.Printf("Enter a passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
crypt.TryMlock(password)
defer clear(password)
encrypted, err := crypt.EncryptBytesPass(fileBytes, password)
if err != nil {
clear(fileBytes)
clear(password)
cmd.PrintErrln("Error:", err)
os.Exit(1)
}
if err := os.WriteFile(output, encrypted, 0600); err != nil {
return fmt.Errorf("failed to write output file: %v", err)
}
return nil
},
}
|