aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-12 06:40:42 +0530
committerishaan <contact@ishaan.cc>2026-07-12 06:40:42 +0530
commit65017dfaff1376a16f4d11e76a8aa2d625e05aec (patch)
treeab099d2b5d037e2670e6761c99a506accaef78d8 /cmd
parentcfbacac3872a98303481a7930b8d497058499b30 (diff)
no step on snek
Diffstat (limited to 'cmd')
-rw-r--r--cmd/eno/cobra.go50
-rw-r--r--cmd/eno/main.go9
-rw-r--r--cmd/eno/rawCmds.go (renamed from cmd/rawCmds.go)85
-rw-r--r--cmd/flags.go35
-rw-r--r--cmd/main.go35
-rw-r--r--cmd/utils.go20
6 files changed, 106 insertions, 128 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go
new file mode 100644
index 0000000..1199aa0
--- /dev/null
+++ b/cmd/eno/cobra.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "fmt"
+
+ "github.com/spf13/cobra"
+)
+
+var cobraCmd = &cobra.Command{
+ Use: "eno",
+ Short: "file encryption tool",
+}
+
+var encryptCmd = &cobra.Command{
+ Use: "encrypt <file>",
+ Short: "encrypt a file",
+ Args: cobra.ExactArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ output, _ := cmd.Flags().GetString("output")
+ if output == "" {
+ return fmt.Errorf("output file required (-o)")
+ }
+ rawEncryptFile(args[0], output)
+ return nil
+ },
+}
+
+var decryptCmd = &cobra.Command{
+ Use: "decrypt <file>",
+ Short: "decrypt a file",
+ Args: cobra.ExactArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ output, _ := cmd.Flags().GetString("output")
+ if output == "" {
+ return fmt.Errorf("output file required (-o)")
+ }
+ rawDecryptFile(args[0], output)
+ return nil
+ },
+}
+
+func init() {
+ encryptCmd.Flags().StringP("output", "o", "", "output file (required)")
+ encryptCmd.MarkFlagRequired("output")
+
+ decryptCmd.Flags().StringP("output", "o", "", "output file (required)")
+ decryptCmd.MarkFlagRequired("output")
+
+ cobraCmd.AddCommand(encryptCmd, decryptCmd)
+}
diff --git a/cmd/eno/main.go b/cmd/eno/main.go
new file mode 100644
index 0000000..52aadf4
--- /dev/null
+++ b/cmd/eno/main.go
@@ -0,0 +1,9 @@
+package main
+
+import "os"
+
+func main() {
+ if err := cobraCmd.Execute(); err != nil {
+ os.Exit(1)
+ }
+}
diff --git a/cmd/rawCmds.go b/cmd/eno/rawCmds.go
index ff00cab..b589eef 100644
--- a/cmd/rawCmds.go
+++ b/cmd/eno/rawCmds.go
@@ -5,6 +5,7 @@ import (
"os"
"strings"
+ "gitlab.com/whiteboardguy/eno/internal/utils"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/chacha20poly1305"
)
@@ -14,53 +15,78 @@ var salt = []byte{
0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
}
-func rawEncryptFile(file string) {
- fileBytes, err := os.ReadFile(file)
+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 {
- panic(err)
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
}
fmt.Printf("Enter a super secure passphrase: ")
- password, err := takeSecureUserInput()
+ password, err := utils.TakeSecureUserInput()
if err != nil {
- panic(err)
+ 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 {
- panic(err)
+ 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)
- fmt.Print("output path: ")
- var outPath string
- _, err = fmt.Scanln(&outPath)
- if err != nil {
- panic(err)
+ if err := os.WriteFile(outputFile, encrypted, 0644); err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
}
- os.WriteFile(outPath, encrypted, 0644)
}
-func rawDecryptFile(file string) {
- fileBytes, err := os.ReadFile(file)
+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 {
- panic(err)
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
}
fmt.Printf("Enter the super secure passphrase: ")
- password, err := takeSecureUserInput()
+ password, err := utils.TakeSecureUserInput()
if err != nil {
- panic(err)
+ 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 {
- panic(err)
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
}
nonce, ciphertext := fileBytes[:aead.NonceSize()], fileBytes[aead.NonceSize():]
@@ -74,25 +100,8 @@ func rawDecryptFile(file string) {
panic(err)
}
- fmt.Print("output path: ")
- var outPath string
- _, err = fmt.Scanln(&outPath)
- if err != nil {
- panic(err)
- }
-
- if _, err := os.ReadFile(outPath); err == nil {
- var overwriteConfirm string
- fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", outPath)
- fmt.Scanln(&overwriteConfirm)
-
- if overwriteConfirm != "y" && overwriteConfirm != "Y" {
- fmt.Fprintln(os.Stderr, "write aborted")
- os.Exit(1)
- }
- }
-
- if err := os.WriteFile(outPath, plaintext, 0644); err != nil {
- panic(err)
+ if err := os.WriteFile(outputFile, plaintext, 0644); err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
}
}
diff --git a/cmd/flags.go b/cmd/flags.go
deleted file mode 100644
index af4964b..0000000
--- a/cmd/flags.go
+++ /dev/null
@@ -1,35 +0,0 @@
-package main
-
-import (
- "flag"
- "os"
- "fmt"
-)
-
-var (
- rawEncrypt bool
- rawDecrypt bool
-)
-
-var commands = map[string]*flag.FlagSet{}
-
-func initFlags() {
- // Raw Encrypt
- encryptCmd := flag.NewFlagSet("encrypt", flag.ExitOnError)
- encryptCmd.BoolVar(&rawEncrypt, "encrypt", true, "encrypt a UTF8 file")
- encryptCmd.BoolVar(&rawEncrypt, "e", true, "encrypt a UTF-8 file")
- commands["encrypt"] = encryptCmd
-
- // Raw Decrypt
- decryptCmd := flag.NewFlagSet("decrypt", flag.ExitOnError)
- decryptCmd.BoolVar(&rawDecrypt, "decrypt", false, "decrypt a UTF8 file")
- decryptCmd.BoolVar(&rawDecrypt, "d", false, "decrypt a UTF-8 file")
- commands["decrypt"] = decryptCmd
-}
-
-func usage() {
- fmt.Fprintf(os.Stderr, "Usage:\n")
- fmt.Fprintf(os.Stderr, " encrypt | e <file> encrypt a file\n")
- fmt.Fprintf(os.Stderr, " decrypt | d <file> decrypt a file\n")
- os.Exit(1)
-}
diff --git a/cmd/main.go b/cmd/main.go
deleted file mode 100644
index 9fa7a10..0000000
--- a/cmd/main.go
+++ /dev/null
@@ -1,35 +0,0 @@
-package main
-
-import "os"
-
-func main() {
- initFlags()
-
- if len(os.Args) < 2 {
- usage()
- }
-
- opt := os.Args[1]
-
- cmd, ok := commands[opt]
- if !ok {
- usage()
- }
-
- cmd.Parse(os.Args[2:])
- args := cmd.Args()
-
- if len(args) > 2 {
- usage()
- }
-
- file := args[0]
-
- switch opt {
- case "encrypt":
- rawEncryptFile(file)
-
- case "decrypt":
- rawDecryptFile(file)
- }
-}
diff --git a/cmd/utils.go b/cmd/utils.go
deleted file mode 100644
index d8e54fe..0000000
--- a/cmd/utils.go
+++ /dev/null
@@ -1,20 +0,0 @@
-package main
-
-import (
- "fmt"
-
- "github.com/charmbracelet/x/term"
-)
-
-func takeSecureUserInput() ([]byte, error) {
- sensInfo, err := term.ReadPassword(0)
- fmt.Println()
- if err != nil {
- return nil, err
- }
- if len(sensInfo) == 0 {
- return nil, fmt.Errorf("passphrase cannot be empty")
- }
-
- return sensInfo, nil
-}