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 /internal/crud | |
| parent | 388971e7a86be97f6a7412cd1d526016bc4e14ce (diff) | |
better
Diffstat (limited to 'internal/crud')
| -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 |
3 files changed, 162 insertions, 61 deletions
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 -} |