aboutsummaryrefslogtreecommitdiff
path: root/internal/crud/cli_new.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-14 19:26:55 +0530
committerishaan <contact@ishaan.cc>2026-07-14 19:26:55 +0530
commit6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (patch)
tree4c2c85b78b07e877ca18e833d07e8624712e8a4c /internal/crud/cli_new.go
parent9eb2b774e9f0522a3547644453bf929dd560f05f (diff)
ssh
Diffstat (limited to 'internal/crud/cli_new.go')
-rw-r--r--internal/crud/cli_new.go258
1 files changed, 251 insertions, 7 deletions
diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go
index 2b6e507..c8303a0 100644
--- a/internal/crud/cli_new.go
+++ b/internal/crud/cli_new.go
@@ -1,15 +1,18 @@
package crud
import (
+ "bufio"
+ "bytes"
"fmt"
"os"
+ "strings"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/editor"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
-func NewNote(output string) error {
+func NewNotePass(output string) error {
fmt.Printf("Enter the passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
@@ -30,10 +33,9 @@ func NewNote(output string) error {
return fmt.Errorf("passphrases do not match")
}
- encrypted, err := crypt.EncryptRawBytes(
+ encrypted, err := crypt.EncryptBytesPass(
[]byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"),
password,
- crypt.Pass,
)
if err != nil {
return err
@@ -58,7 +60,7 @@ func NewNote(output string) error {
return nil
}
-func NewNoteEdit(output, editorString string) error {
+func NewNotePassEdit(output, editorString string) error {
fmt.Printf("Enter the passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
@@ -83,7 +85,7 @@ func NewNoteEdit(output, editorString string) error {
preExisting := err == nil
initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^")
- encrypted, err := crypt.EncryptRawBytes(initialContent, password, crypt.Pass)
+ encrypted, err := crypt.EncryptBytesPass(initialContent, password)
if err != nil {
return err
}
@@ -111,7 +113,7 @@ func NewNoteEdit(output, editorString string) error {
return err
}
- plaintext, err := crypt.DecryptRawBytes(ciphertext, password)
+ plaintext, err := crypt.DecryptBytesPass(ciphertext, password)
if err != nil {
return err
}
@@ -140,7 +142,249 @@ func NewNoteEdit(output, editorString string) error {
os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600)
- encrypted, err = crypt.EncryptRawBytes(newContent, password, crypt.Pass)
+ encrypted, err = crypt.EncryptBytesPass(newContent, password)
+ 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
+}
+
+func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
+ useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !useDefault {
+ fmt.Println("Warning: you will not be able to open or edit this note again.")
+ fmt.Println(" make sure you've used the '--edit' flag.")
+ }
+
+ addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+
+ pubKeys := [][]byte{}
+
+ if useDefault {
+ pubKeys = append(pubKeys, defaultPubKeyBytes)
+ }
+
+ if addMore {
+ for {
+ fmt.Print("Enter a public key, or path to a public key file (comma-separated for multiple): ")
+ scanner := bufio.NewScanner(os.Stdin)
+ scanner.Scan()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read public key: %v", err)
+ }
+
+ input := strings.TrimSpace(scanner.Text())
+ if input == "" {
+ continue
+ }
+
+ for part := range strings.SplitSeq(input, ",") {
+ part = strings.TrimSpace(part)
+ if part == "" {
+ continue
+ }
+
+ var keyBytes []byte
+ if strings.HasPrefix(part, "ssh-") {
+ keyBytes = []byte(part)
+ } else {
+ var err error
+ keyBytes, err = os.ReadFile(part)
+ if err != nil {
+ return fmt.Errorf("failed to read public key file %s: %v", part, err)
+ }
+ }
+
+ pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes))
+ }
+
+ more, err := utils.AskUserYesNo("Add more public keys?", false)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !more {
+ break
+ }
+ }
+ }
+
+ if len(pubKeys) == 0 {
+ return fmt.Errorf("at least one recipient public key is required")
+ }
+
+ tmptxt := []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^")
+
+ encrypted, err := crypt.EncryptBytesSSH(tmptxt, pubKeys)
+ 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 NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath string, editorString string) error {
+ useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !useDefault {
+ fmt.Println("Warning: you will not be able to open or edit this note again.")
+ fmt.Println(" make sure you've used the '--edit' flag.")
+ }
+
+ addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+
+ pubKeys := [][]byte{}
+
+ if useDefault {
+ pubKeys = append(pubKeys, defaultPubKeyBytes)
+ }
+
+ if addMore {
+ for {
+ fmt.Print("Enter a public key, or path to a public key file (comma-separated for multiple): ")
+ scanner := bufio.NewScanner(os.Stdin)
+ scanner.Scan()
+ if err := scanner.Err(); err != nil {
+ return fmt.Errorf("failed to read public key: %v", err)
+ }
+
+ input := strings.TrimSpace(scanner.Text())
+ if input == "" {
+ continue
+ }
+
+ for part := range strings.SplitSeq(input, ",") {
+ part = strings.TrimSpace(part)
+ if part == "" {
+ continue
+ }
+
+ var keyBytes []byte
+ if strings.HasPrefix(part, "ssh-") {
+ keyBytes = []byte(part)
+ } else {
+ var err error
+ keyBytes, err = os.ReadFile(part)
+ if err != nil {
+ return fmt.Errorf("failed to read public key file %s: %v", part, err)
+ }
+ }
+
+ pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes))
+ }
+
+ more, err := utils.AskUserYesNo("Add more public keys?", false)
+ if err != nil {
+ return fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !more {
+ break
+ }
+ }
+ }
+
+ if len(pubKeys) == 0 {
+ return fmt.Errorf("at least one recipient public key is required")
+ }
+
+ noteInfo, err := os.Stat(output)
+ preExisting := err == nil
+
+ initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^")
+ encrypted, err := crypt.EncryptBytesSSH(initialContent, pubKeys)
+ 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.DecryptBytesSSH(ciphertext, defaultPrivKeyPath)
+ 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.EncryptBytesSSH(newContent, pubKeys)
if err != nil {
return err
}