aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/eno/cobra.go5
-rw-r--r--cmd/eno/rawCmds.go107
2 files changed, 3 insertions, 109 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
index 1199aa0..1ee441d 100644
--- a/cmd/eno/cobra.go
+++ b/cmd/eno/cobra.go
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/crud"
)
var cobraCmd = &cobra.Command{
@@ -20,7 +21,7 @@ var encryptCmd = &cobra.Command{
if output == "" {
return fmt.Errorf("output file required (-o)")
}
- rawEncryptFile(args[0], output)
+ crud.RawEncryptFile(args[0], output)
return nil
},
}
@@ -34,7 +35,7 @@ var decryptCmd = &cobra.Command{
if output == "" {
return fmt.Errorf("output file required (-o)")
}
- rawDecryptFile(args[0], output)
+ crud.RawDecryptFile(args[0], output)
return nil
},
}
diff --git a/cmd/eno/rawCmds.go b/cmd/eno/rawCmds.go
deleted file mode 100644
index b589eef..0000000
--- a/cmd/eno/rawCmds.go
+++ /dev/null
@@ -1,107 +0,0 @@
-package main
-
-import (
- "fmt"
- "os"
- "strings"
-
- "gitlab.com/whiteboardguy/eno/internal/utils"
- "golang.org/x/crypto/argon2"
- "golang.org/x/crypto/chacha20poly1305"
-)
-
-var salt = []byte{
- 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
- 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
-}
-
-func rawEncryptFile(inputFile, outputFile string) {
- if _, err := os.ReadFile(outputFile); err == nil {
- var overwriteConfirm string
- fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", outputFile)
- fmt.Scanln(&overwriteConfirm)
-
- if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- fmt.Fprintln(os.Stderr, "write aborted")
- os.Exit(1)
- }
- }
-
- fileBytes, err := os.ReadFile(inputFile)
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- fmt.Printf("Enter a super secure passphrase: ")
- password, err := utils.TakeSecureUserInput()
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- key := argon2.IDKey(password, salt, 1, 2*1024*1024, 2, 32)
- aead, err := chacha20poly1305.New(key)
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(fileBytes)+aead.Overhead())
-
- encrypted := aead.Seal(nonce, nonce, fileBytes, nil)
-
- if err := os.WriteFile(outputFile, encrypted, 0644); err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-}
-
-func rawDecryptFile(inputFile, outputFile string) {
- if _, err := os.ReadFile(outputFile); err == nil {
- var overwriteConfirm string
- fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", outputFile)
- fmt.Scanln(&overwriteConfirm)
-
- if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- fmt.Fprintln(os.Stderr, "write aborted")
- os.Exit(1)
- }
- }
-
- fileBytes, err := os.ReadFile(inputFile)
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- fmt.Printf("Enter the super secure passphrase: ")
- password, err := utils.TakeSecureUserInput()
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- key := argon2.IDKey(password, salt, 1, 2*1024*1024, 2, 32)
- aead, err := chacha20poly1305.New(key)
- if err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-
- nonce, ciphertext := fileBytes[:aead.NonceSize()], fileBytes[aead.NonceSize():]
-
- plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
- if err != nil {
- if strings.Contains(err.Error(), "message authentication failed") {
- fmt.Fprintln(os.Stderr, "wrong password or corrupted data")
- os.Exit(1)
- }
- panic(err)
- }
-
- if err := os.WriteFile(outputFile, plaintext, 0644); err != nil {
- fmt.Fprintln(os.Stderr, err.Error())
- os.Exit(1)
- }
-}