* Implement GetArmoredWithCustomHeaders ArmorWithTypeAndCustomHeaders can be reused by other PGP armoured objects. * Update linting, and lint accordingly `godot` has been improved and `goerr113` has been added (and ignored here). * Add custom headers for keys * Minor comment changes Co-authored-by: Aron Wussler <aron@wussler.it>
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
// Package armor contains a set of helper methods for armoring and unarmoring
|
|
// data.
|
|
package armor
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/constants"
|
|
"github.com/ProtonMail/gopenpgp/v2/internal"
|
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
)
|
|
|
|
// ArmorKey armors input as a public key.
|
|
func ArmorKey(input []byte) (string, error) {
|
|
return ArmorWithType(input, constants.PublicKeyHeader)
|
|
}
|
|
|
|
// ArmorWithTypeBuffered returns a io.WriteCloser which, when written to, writes
|
|
// armored data to w with the given armorType.
|
|
func ArmorWithTypeBuffered(w io.Writer, armorType string) (io.WriteCloser, error) {
|
|
return armor.Encode(w, armorType, nil)
|
|
}
|
|
|
|
// ArmorWithType armors input with the given armorType.
|
|
func ArmorWithType(input []byte, armorType string) (string, error) {
|
|
return armorWithTypeAndHeaders(input, armorType, internal.ArmorHeaders)
|
|
}
|
|
|
|
// ArmorWithTypeAndCustomHeaders armors input with the given armorType and
|
|
// headers.
|
|
func ArmorWithTypeAndCustomHeaders(input []byte, armorType, version, comment string) (string, error) {
|
|
headers := make(map[string]string)
|
|
if version != "" {
|
|
headers["Version"] = version
|
|
}
|
|
if comment != "" {
|
|
headers["Comment"] = comment
|
|
}
|
|
return armorWithTypeAndHeaders(input, armorType, headers)
|
|
}
|
|
|
|
// Unarmor unarmors an armored input into a byte array.
|
|
func Unarmor(input string) ([]byte, error) {
|
|
b, err := internal.Unarmor(input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ioutil.ReadAll(b.Body)
|
|
}
|
|
|
|
func armorWithTypeAndHeaders(input []byte, armorType string, headers map[string]string) (string, error) {
|
|
var b bytes.Buffer
|
|
|
|
w, err := armor.Encode(&b, armorType, headers)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err = w.Write(input); err != nil {
|
|
return "", err
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
return "", err
|
|
}
|
|
return b.String(), nil
|
|
}
|