Skip to content

Commit

Permalink
Use the provided ParseRecipient func
Browse files Browse the repository at this point in the history
Less code and accurately handles comments. Also fail gracefully if the
.strongbox_idenitity file doesn't exist, we can just copy over
ciphertext quietly.
  • Loading branch information
george-angel committed Jul 24, 2024
1 parent 0d6a5a9 commit 5729bad
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions age.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5729bad

Please sign in to comment.