Skip to content

Commit

Permalink
Use the provided ParseRecipient func (#80)
Browse files Browse the repository at this point in the history
* 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.

* More defensive age decrypt

Err on the side of "copy ciphertext on error" rather then exiting.
  • Loading branch information
george-angel authored Jul 26, 2024
1 parent 0d6a5a9 commit 1be4e49
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
32 changes: 14 additions & 18 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,17 +84,25 @@ 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)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
// could not parse identity file, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
log.Println(err)
}
return
}
armorReader := armor.NewReader(bytes.NewReader(in))
ar, err := age.Decrypt(armorReader, identities...)
if err != nil {
// Couldn't find the key, just copy as is and return
// couldn't find the key, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
log.Println(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require filippo.io/age v1.2.0
require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
c2sp.org/CCTV/age v0.0.0-20240306222714-3ec4d716e805 h1:u2qwJeEvnypw+OCPUHmoZE3IqwfuN5kgDfo5MLzpNM0=
c2sp.org/CCTV/age v0.0.0-20240306222714-3ec4d716e805/go.mod h1:FomMrUJ2Lxt5jCLmZkG3FHa72zUprnhd3v/Z18Snm4w=
filippo.io/age v1.2.0 h1:vRDp7pUMaAJzXNIWJVAZnEf/Dyi4Vu4wI8S1LBzufhE=
filippo.io/age v1.2.0/go.mod h1:JL9ew2lTN+Pyft4RiNGguFfOpewKwSHm5ayKD/A4004=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand All @@ -17,12 +19,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down

0 comments on commit 1be4e49

Please sign in to comment.