Implement GetArmoredWithCustomHeaders (#48)

* 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>
This commit is contained in:
zugzwang 2020-05-06 18:50:18 +02:00 committed by GitHub
parent b1e005fec3
commit dcc82c9fc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 176 additions and 31 deletions

View file

@ -26,9 +26,35 @@ func ArmorWithTypeBuffered(w io.Writer, armorType string) (io.WriteCloser, error
// 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, internal.ArmorHeaders)
w, err := armor.Encode(&b, armorType, headers)
if err != nil {
return "", err
@ -41,12 +67,3 @@ func ArmorWithType(input []byte, armorType string) (string, error) {
}
return b.String(), nil
}
// Unarmor unarmors an armored key.
func Unarmor(input string) ([]byte, error) {
b, err := internal.Unarmor(input)
if err != nil {
return nil, err
}
return ioutil.ReadAll(b.Body)
}