aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/eno/cobra.go61
-rw-r--r--internal/crypt/rawDecrypt.go4
-rw-r--r--internal/crypt/rawEncrypt.go4
3 files changed, 44 insertions, 25 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index 179c610..69f621f 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -1,6 +1,7 @@
package main
import (
+ "bufio"
"fmt"
"os"
@@ -18,31 +19,36 @@ var encryptCmd = &cobra.Command{
Use: "encrypt <file>",
Short: "encrypt a file",
Args: cobra.ExactArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- output, _ := cmd.Flags().GetString("output")
+ 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 _, err := os.ReadFile(output); err == nil {
- var overwriteConfirm string
+ if _, err := os.Stat(output); err == nil {
+ scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- fmt.Scanln(&overwriteConfirm)
+ scanner.Scan()
+ overwriteConfirm := scanner.Text()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error:", "write aborted")
+ cmd.PrintErrln("Error: write aborted")
os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read input file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read input file: %v", err)
}
fmt.Printf("Enter a super secure passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read passphrase: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read passphrase: %v", err)
}
encrypted, err := crypt.EncryptRawBytes(fileBytes, password)
@@ -52,9 +58,10 @@ var encryptCmd = &cobra.Command{
}
if err := os.WriteFile(output, encrypted, 0644); err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to write output file: %v", err)
}
+
+ return nil
},
}
@@ -62,31 +69,36 @@ var decryptCmd = &cobra.Command{
Use: "decrypt <file>",
Short: "decrypt a file",
Args: cobra.ExactArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- output, _ := cmd.Flags().GetString("output")
+ 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 _, err := os.ReadFile(output); err == nil {
- var overwriteConfirm string
+ if _, err := os.Stat(output); err == nil {
+ scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", output)
- fmt.Scanln(&overwriteConfirm)
+ scanner.Scan()
+ overwriteConfirm := scanner.Text()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- cmd.PrintErrln("Error:", "write aborted")
+ cmd.PrintErrln("Error: write aborted")
os.Exit(1)
}
}
fileBytes, err := os.ReadFile(args[0])
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read input file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read input file: %v", err)
}
fmt.Printf("Enter the super secure passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to read passphrase: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to read passphrase: %v", err)
}
plaintext, err := crypt.DecryptRawBytes(fileBytes, password)
@@ -96,9 +108,10 @@ var decryptCmd = &cobra.Command{
}
if err := os.WriteFile(output, plaintext, 0644); err != nil {
- cmd.PrintErrln("Error:", fmt.Sprintf("failed to write output file: %v", err))
- os.Exit(1)
+ return fmt.Errorf("failed to write output file: %v", err)
}
+
+ return nil
},
}
diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go
index 30a202f..b82f0fb 100644
--- a/internal/crypt/rawDecrypt.go
+++ b/internal/crypt/rawDecrypt.go
@@ -26,6 +26,10 @@ func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) {
return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err)
}
+ if headerLen < 0 || idx+1+headerLen > len(inputBytes) {
+ return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen)
+ }
+
headerBytes := inputBytes[idx+1 : idx+1+headerLen]
headers, err := utils.GetFileHeaders(headerBytes)
if err != nil {
diff --git a/internal/crypt/rawEncrypt.go b/internal/crypt/rawEncrypt.go
index 79a1a54..77803e0 100644
--- a/internal/crypt/rawEncrypt.go
+++ b/internal/crypt/rawEncrypt.go
@@ -2,6 +2,7 @@ package crypt
import (
"fmt"
+ "strconv"
"gitlab.com/whiteboardguy/eno/internal/utils"
"golang.org/x/crypto/argon2"
@@ -34,7 +35,8 @@ func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) {
return nil, fmt.Errorf("encrypt: %w", err)
}
- target := make([]byte, 0, len(headerBytes)+len(encrypted)+20)
+ lenLen := len(strconv.Itoa(len(headerBytes)))
+ target := make([]byte, 0, lenLen+1+len(headerBytes)+len(encrypted))
target = fmt.Appendf(target, "%d:", len(headerBytes))
target = append(target, headerBytes...)
target = append(target, encrypted...)