internal/age: remove EncryptWithArmor and armor support in Decrypt

The caller can take care of the armor. For consistency move the
responsibility to close the armor to the caller, and make the stream
Writer not propagate Close.

This also will also allow us to spin the armor implementation out into
its won package that imports format, without getting an import loop from
format.Parse magically invoking armor decoding.

Less magic in the API, more magic in the CLI.
This commit is contained in:
Filippo Valsorda
2020-05-18 00:11:21 -04:00
parent 7088a73234
commit a7c4274d23
5 changed files with 26 additions and 49 deletions
+17 -1
View File
@@ -7,6 +7,7 @@
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
@@ -16,6 +17,7 @@ import (
"strings"
"filippo.io/age/internal/age"
"filippo.io/age/internal/format"
"golang.org/x/crypto/ssh/terminal"
)
@@ -208,7 +210,13 @@ func encryptPass(pass string, in io.Reader, out io.Writer, armor bool) {
func encrypt(recipients []age.Recipient, in io.Reader, out io.Writer, armor bool) {
ageEncrypt := age.Encrypt
if armor {
ageEncrypt = age.EncryptWithArmor
a := format.ArmoredWriter(out)
defer func() {
if err := a.Close(); err != nil {
logFatalf("Error: %v", err)
}
}()
out = a
}
w, err := ageEncrypt(out, recipients...)
if err != nil {
@@ -239,6 +247,14 @@ func decrypt(keys []string, in io.Reader, out io.Writer) {
identities = append(identities, ids...)
}
rr := bufio.NewReader(in)
armorHeader := "-----BEGIN AGE ENCRYPTED FILE-----"
if start, _ := rr.Peek(len(armorHeader)); string(start) == armorHeader {
in = format.ArmoredReader(rr)
} else {
in = rr
}
r, err := age.Decrypt(in, identities...)
if err != nil {
logFatalf("Error: %v", err)