From 5729bad5b23bc7fbfa7a2d19a8adfc2e34c74e82 Mon Sep 17 00:00:00 2001 From: George Angel Date: Wed, 24 Jul 2024 21:31:10 +1000 Subject: [PATCH] Use the provided ParseRecipient func Less code and accurately handles comments. Also fail gracefully if the .strongbox_idenitity file doesn't exist, we can just copy over ciphertext quietly. --- age.go | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/age.go b/age.go index 32e6bca..d815af7 100644 --- a/age.go +++ b/age.go @@ -42,24 +42,12 @@ func ageGenIdentity(desc string) { } func ageFileToRecipient(filename string) ([]age.Recipient, error) { - var recipients []age.Recipient - publicKeys, err := os.ReadFile(filename) + file, err := os.Open(filename) if err != nil { return nil, err } - lines := bytes.Split(publicKeys, []byte("\n")) - for _, line := range lines { - line = bytes.TrimSpace(line) - if len(line) == 0 { - continue - } - recipient, err := age.ParseX25519Recipient(string(line)) - if err != nil { - return nil, err - } - recipients = append(recipients, recipient) - } - return recipients, nil + defer file.Close() + return age.ParseRecipients(file) } func ageEncrypt(w io.Writer, r []age.Recipient, in []byte, f string) { @@ -96,7 +84,11 @@ func ageEncrypt(w io.Writer, r []age.Recipient, in []byte, f string) { func ageDecrypt(w io.Writer, in []byte) { identityFile, err := os.Open(*flagIdentityFile) if err != nil { - log.Fatalf("Failed to open private keys file: %v", err) + // identity file doesn't exist, copy as is and return + if _, err = io.Copy(w, bytes.NewReader(in)); err != nil { + log.Println(err) + } + return } defer identityFile.Close() identities, err := age.ParseIdentities(identityFile)