passforios-gopenpgp/armor/armor.go

53 lines
1.2 KiB
Go
Raw Normal View History

2019-05-15 14:57:35 +02:00
// 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"
)
2019-05-15 14:57:35 +02:00
// ArmorKey armors input as a public key.
func ArmorKey(input []byte) (string, error) {
2019-03-07 16:56:12 +01:00
return ArmorWithType(input, constants.PublicKeyHeader)
}
2019-05-15 14:57:35 +02:00
// 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)
}
2019-05-15 14:57:35 +02:00
// ArmorWithType armors input with the given armorType.
func ArmorWithType(input []byte, armorType string) (string, error) {
var b bytes.Buffer
w, err := armor.Encode(&b, armorType, internal.ArmorHeaders)
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
}
2019-05-15 14:57:35 +02:00
// Unarmor unarmors an armored key.
2018-09-19 11:52:14 +02:00
func Unarmor(input string) ([]byte, error) {
b, err := internal.Unarmor(input)
if err != nil {
return nil, err
}
return ioutil.ReadAll(b.Body)
}