diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-13 11:09:34 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-13 11:09:34 +0530 |
| commit | d433b1af731ff901b9c14a647678471b27fdfe8a (patch) | |
| tree | b101b59e89d826386de8bbada4c405a3cfbf6b3f | |
| parent | 388971e7a86be97f6a7412cd1d526016bc4e14ce (diff) | |
better
| -rw-r--r-- | cmd/eno/cobra.go | 70 | ||||
| -rw-r--r-- | cmd/eno/cobra_new.go | 76 | ||||
| -rw-r--r-- | internal/crud/cli_edit.go (renamed from internal/crud/handler_edit.go) | 8 | ||||
| -rw-r--r-- | internal/crud/cli_new.go | 158 | ||||
| -rw-r--r-- | internal/crud/handler_new.go | 57 | ||||
| -rw-r--r-- | internal/crypt/headers.go (renamed from internal/utils/headers.go) | 19 | ||||
| -rw-r--r-- | internal/crypt/mem.go | 16 | ||||
| -rw-r--r-- | internal/crypt/rawDecrypt.go | 5 | ||||
| -rw-r--r-- | internal/crypt/rawEncrypt.go | 9 | ||||
| -rw-r--r-- | internal/utils/utils.go | 9 |
10 files changed, 278 insertions, 149 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go index b988dab..9e4d4d2 100644 --- a/cmd/eno/cobra.go +++ b/cmd/eno/cobra.go @@ -45,7 +45,7 @@ var encryptCmd = &cobra.Command{ if err != nil { return fmt.Errorf("failed to read input file: %v", err) } - utils.TryMlock(fileBytes) + crypt.TryMlock(fileBytes) defer clear(fileBytes) fmt.Printf("Enter a passphrase: ") @@ -53,10 +53,10 @@ var encryptCmd = &cobra.Command{ if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } - utils.TryMlock(password) + crypt.TryMlock(password) defer clear(password) - encrypted, err := crypt.EncryptRawBytes(fileBytes, password) + encrypted, err := crypt.EncryptRawBytes(fileBytes, password, crypt.Pass) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) @@ -104,7 +104,7 @@ var decryptCmd = &cobra.Command{ if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } - utils.TryMlock(password) + crypt.TryMlock(password) defer clear(password) plaintext, err := crypt.DecryptRawBytes(fileBytes, password) @@ -112,7 +112,7 @@ var decryptCmd = &cobra.Command{ cmd.PrintErrln("Error:", err) os.Exit(1) } - utils.TryMlock(plaintext) + crypt.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0644); err != nil { @@ -138,65 +138,6 @@ var initCmd = &cobra.Command{ }, } -var newCmd = &cobra.Command{ - Use: "new <note>", - Short: "create a new encrypted note", - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - output, err := cmd.Flags().GetString("output") - if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) - } - - editFlag, err := cmd.Flags().GetBool("edit") - if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) - } - - noteName := strings.Join(args, " ") - - if output == "" { - if strings.ContainsRune(noteName, filepath.Separator) { - output = noteName + ".eno" - } else { - output = filepath.Join(Conf.DataDir, noteName+".eno") - } - } - - if _, err := os.Stat(output); err == nil { - ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) - if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) - } - if !ok { - cmd.PrintErrln("Error: write aborted") - os.Exit(1) - } - } - - err = crud.NewNote(output) - if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) - } - - if editFlag { - target := filepath.Clean(output) - target = strings.TrimSuffix(target, ".eno") - target += ".eno" - - err = crud.EditNote(target, Conf.EditorString) - if err != nil { - cmd.PrintErrln("Error:", err) - os.Exit(1) - } - } - }, -} - var editCmd = &cobra.Command{ Use: "edit <note>", Short: "edit an existing encrypted note", @@ -235,6 +176,7 @@ func init() { newCmd.Flags().StringP("output", "o", "", "output file") newCmd.Flags().BoolP("edit", "e", false, "edit after creation") + newCmd.Flags().BoolP("ssh", "ssh", false, "use your ssh for auth") cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd) } diff --git a/cmd/eno/cobra_new.go b/cmd/eno/cobra_new.go new file mode 100644 index 0000000..4a7caeb --- /dev/null +++ b/cmd/eno/cobra_new.go @@ -0,0 +1,76 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + + "github.com/spf13/cobra" + "gitlab.com/whiteboardguy/eno/internal/crud" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +var newCmd = &cobra.Command{ + Use: "new <note>", + Short: "create a new encrypted note", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + + output, err := cmd.Flags().GetString("output") + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + editFlag, err := cmd.Flags().GetBool("edit") + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + sshFlag, err := cmd.Flags().GetBool("ssh") + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + + + noteName := strings.Join(args, " ") + if output == "" { + if strings.ContainsRune(noteName, filepath.Separator) { + output = noteName + ".eno" + } else { + output = filepath.Join(Conf.DataDir, noteName+".eno") + } + } + + if _, err := os.Stat(output); err == nil { + ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + if !ok { + cmd.PrintErrln("Error: write aborted") + os.Exit(1) + } + } + + if editFlag { + err = crud.NewNoteEdit(output, Conf.EditorString) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + } + + _ = sshFlag + + err = crud.NewNote(output) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + }, +} diff --git a/internal/crud/handler_edit.go b/internal/crud/cli_edit.go index 0e13be3..5e2cb8d 100644 --- a/internal/crud/handler_edit.go +++ b/internal/crud/cli_edit.go @@ -20,7 +20,7 @@ func EditNote(notePath, editorString string) error { if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } - utils.TryMlock(password) + crypt.TryMlock(password) defer clear(password) ciphertext, err := os.ReadFile(notePath) @@ -31,7 +31,7 @@ func EditNote(notePath, editorString string) error { plaintext, err := crypt.DecryptRawBytes(ciphertext, password) if err != nil { return err } - utils.TryMlock(plaintext) + crypt.TryMlock(plaintext) defer clear(plaintext) defer deferredCleanTmpFile(tmpFile, len(plaintext)) @@ -46,12 +46,12 @@ func EditNote(notePath, editorString string) error { newContent, err := os.ReadFile(tmpFile.Name()) if err != nil { return err } - utils.TryMlock(newContent) + crypt.TryMlock(newContent) defer clear(newContent) os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) - encrypted, err := crypt.EncryptRawBytes(newContent, password) + encrypted, err := crypt.EncryptRawBytes(newContent, password, crypt.Pass) if err != nil { return err } err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go new file mode 100644 index 0000000..2b6e507 --- /dev/null +++ b/internal/crud/cli_new.go @@ -0,0 +1,158 @@ +package crud + +import ( + "fmt" + "os" + + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/editor" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +func NewNote(output string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if string(password) != string(passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + encrypted, err := crypt.EncryptRawBytes( + []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), + password, + crypt.Pass, + ) + if err != nil { + return err + } + + fmt.Println(output) + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + + // utils.ClearTerm() + fmt.Println("New note saved at:", file.Name()) + + return nil +} + +func NewNoteEdit(output, editorString string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if string(password) != string(passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + noteInfo, err := os.Stat(output) + preExisting := err == nil + + initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + encrypted, err := crypt.EncryptRawBytes(initialContent, password, crypt.Pass) + if err != nil { + return err + } + + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + file.Close() + + ciphertext, err := os.ReadFile(output) + if err != nil { + return err + } + + tmpFile, err := os.CreateTemp("", "eno-*") + if err != nil { + return err + } + + plaintext, err := crypt.DecryptRawBytes(ciphertext, password) + if err != nil { + return err + } + crypt.TryMlock(plaintext) + defer clear(plaintext) + + defer deferredCleanTmpFile(tmpFile, len(plaintext)) + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(plaintext) + if err != nil { + return err + } + + err = editor.OpenWithEditor(tmpFile.Name(), editorString) + if err != nil { + return err + } + + newContent, err := os.ReadFile(tmpFile.Name()) + if err != nil { + return err + } + crypt.TryMlock(newContent) + defer clear(newContent) + + os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + + encrypted, err = crypt.EncryptRawBytes(newContent, password, crypt.Pass) + if err != nil { + return err + } + + perm := os.FileMode(0600) + if preExisting { + perm = noteInfo.Mode().Perm() + } + + err = os.WriteFile(output, encrypted, perm) + + fmt.Println("New note saved at:", output) + + return err +} diff --git a/internal/crud/handler_new.go b/internal/crud/handler_new.go deleted file mode 100644 index f949969..0000000 --- a/internal/crud/handler_new.go +++ /dev/null @@ -1,57 +0,0 @@ -package crud - -import ( - "fmt" - "os" - - "gitlab.com/whiteboardguy/eno/internal/crypt" - "gitlab.com/whiteboardguy/eno/internal/utils" -) - -func NewNote(output string) error { - fmt.Printf("Enter the passphrase: ") - password, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - utils.TryMlock(password) - defer clear(password) - - fmt.Printf("Confirm the passphrase: ") - passwordConfirm, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - utils.TryMlock(passwordConfirm) - defer clear(passwordConfirm) - - if string(password) != string(passwordConfirm) { - return fmt.Errorf("passphrases do not match") - } - - encrypted, err := crypt.EncryptRawBytes( - []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), - password, - ) - if err != nil { - return err - } - - fmt.Println(output) - file, err := os.Create(output) - if err != nil { - return err - } - - _, err = file.Write(encrypted) - if err != nil { - return err - } - - file.Sync() - - // utils.ClearTerm() - fmt.Println("New note saved at:", file.Name()) - - return nil -} diff --git a/internal/utils/headers.go b/internal/crypt/headers.go index a4c8e10..b5addb2 100644 --- a/internal/utils/headers.go +++ b/internal/crypt/headers.go @@ -1,4 +1,4 @@ -package utils +package crypt import ( "bytes" @@ -7,19 +7,24 @@ import ( ) type DataType string +type AuthMode string + const ( File DataType = "File" Dir DataType = "Dir" + + Pass AuthMode = "Pass" + SSH AuthMode = "SSH" ) type FileHeaders struct { - Salt [16]byte - Nonce [12]byte - Type DataType // `Type` for file vs directory storage + Salt [16]byte + Nonce [12]byte + Type DataType + Auth AuthMode + Parts int - // TODO: `Parts (int)` for the number of encrypted parts the file is divided into. - // TODO: `Version (string)` for the version of .eno file. - // TODO: `EncryptMethod (string)` for the mode of encryption (gpg or passphrase). Maybe even stdout command. + // TODO: Version (string) for the version of .eno file. } func GetFileHeaders(b []byte) (FileHeaders, error) { diff --git a/internal/crypt/mem.go b/internal/crypt/mem.go new file mode 100644 index 0000000..9c1d21c --- /dev/null +++ b/internal/crypt/mem.go @@ -0,0 +1,16 @@ +package crypt + +import ( + "fmt" + "os" + "syscall" +) + +func TryMlock(b []byte) { + if len(b) == 0 { + return + } + if err := syscall.Mlock(b); err != nil { + fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err) + } +} diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go index fe190a5..28d7856 100644 --- a/internal/crypt/rawDecrypt.go +++ b/internal/crypt/rawDecrypt.go @@ -4,7 +4,6 @@ import ( "fmt" "strconv" - "gitlab.com/whiteboardguy/eno/internal/utils" "golang.org/x/crypto/argon2" "golang.org/x/crypto/chacha20poly1305" ) @@ -31,13 +30,13 @@ func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { } headerBytes := inputBytes[idx+1 : idx+1+headerLen] - headers, err := utils.GetFileHeaders(headerBytes) + headers, err := GetFileHeaders(headerBytes) if err != nil { return nil, fmt.Errorf("decrypt: %w", err) } key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32) - utils.TryMlock(key) + TryMlock(key) defer clear(key) aead, err := chacha20poly1305.New(key) if err != nil { diff --git a/internal/crypt/rawEncrypt.go b/internal/crypt/rawEncrypt.go index f197650..135efdf 100644 --- a/internal/crypt/rawEncrypt.go +++ b/internal/crypt/rawEncrypt.go @@ -4,12 +4,11 @@ import ( "fmt" "strconv" - "gitlab.com/whiteboardguy/eno/internal/utils" "golang.org/x/crypto/argon2" "golang.org/x/crypto/chacha20poly1305" ) -func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { +func EncryptRawBytes(inputBytes []byte, password []byte, authMode AuthMode) ([]byte, error) { salt, err := NewSalt() if err != nil { return nil, fmt.Errorf("encrypt: %w", err) @@ -20,10 +19,10 @@ func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { return nil, fmt.Errorf("encrypt: %w", err) } - headers := utils.FileHeaders{Salt: salt, Nonce: nonce} + headers := FileHeaders{Salt: salt, Nonce: nonce, Type: File, Parts: 1, Auth: authMode} key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32) - utils.TryMlock(key) + TryMlock(key) defer clear(key) aead, err := chacha20poly1305.New(key) if err != nil { @@ -32,7 +31,7 @@ func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { encrypted := aead.Seal(nil, headers.Nonce[:], inputBytes, nil) - headerBytes, err := utils.FileHeadersBytes(headers) + headerBytes, err := FileHeadersBytes(headers) if err != nil { return nil, fmt.Errorf("encrypt: %w", err) } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 96ca0f0..a8437b9 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -10,15 +10,6 @@ import ( "github.com/charmbracelet/x/term" ) -func TryMlock(b []byte) { - if len(b) == 0 { - return - } - if err := syscall.Mlock(b); err != nil { - fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err) - } -} - func TakeSecureUserInput() ([]byte, error) { fd := os.Stdin.Fd() |