aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-14 22:05:23 +0530
committerishaan <contact@ishaan.cc>2026-07-14 22:05:23 +0530
commit6fc45951e42b0a7d4760e7b2eb09c647d1f75402 (patch)
treed37a12add7bb092f200eea7c7f4a27d225cbc8d1 /internal
parent6b33b7a8abf03d15a8164ff207547f45475e737e (diff)
read me
Diffstat (limited to 'internal')
-rw-r--r--internal/crud/cli_new.go84
-rw-r--r--internal/crypt/sshKey.go2
2 files changed, 17 insertions, 69 deletions
diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go
index cd06455..664d655 100644
--- a/internal/crud/cli_new.go
+++ b/internal/crud/cli_new.go
@@ -159,10 +159,10 @@ func NewNotePassEdit(output, editorString string) error {
return err
}
-func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
+func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, 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)
+ return nil, false, 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.")
@@ -171,7 +171,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
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)
+ return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
}
pubKeys := [][]byte{}
@@ -186,7 +186,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if err := scanner.Err(); err != nil {
- return fmt.Errorf("failed to read public key: %v", err)
+ return nil, false, fmt.Errorf("failed to read public key: %v", err)
}
input := strings.TrimSpace(scanner.Text())
@@ -207,7 +207,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
var err error
keyBytes, err = os.ReadFile(part)
if err != nil {
- return fmt.Errorf("failed to read public key file %s: %v", part, err)
+ return nil, false, fmt.Errorf("failed to read public key file %s: %v", part, err)
}
}
@@ -216,7 +216,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
more, err := utils.AskUserYesNo("Add more public keys?", false)
if err != nil {
- return fmt.Errorf("failed to read confirmation: %v", err)
+ return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
}
if !more {
break
@@ -224,6 +224,14 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
}
}
+ return pubKeys, useDefault, nil
+}
+
+func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
+ pubKeys, _, err := PromptRecipientKeys(defaultPubKeyBytes)
+ if err != nil {
+ return err
+ }
if len(pubKeys) == 0 {
return fmt.Errorf("at least one recipient public key is required")
}
@@ -255,70 +263,10 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
}
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)
+ pubKeys, useDefault, err := PromptRecipientKeys(defaultPubKeyBytes)
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
- }
- }
+ return err
}
-
if len(pubKeys) == 0 {
return fmt.Errorf("at least one recipient public key is required")
}
diff --git a/internal/crypt/sshKey.go b/internal/crypt/sshKey.go
index 3b0b344..e36d6fd 100644
--- a/internal/crypt/sshKey.go
+++ b/internal/crypt/sshKey.go
@@ -178,7 +178,7 @@ func FindAndUnwrapNoteKey(recipients []WrappedKey, privKeyPath string) ([]byte,
return nil, fmt.Errorf("read SSH key: %w", err)
}
TryMlock(pemBytes)
- clear(pemBytes)
+ defer clear(pemBytes)
parsed, err := ssh.ParseRawPrivateKey(pemBytes)
if err != nil {