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 }