passforios-gopenpgp/armor.go

55 lines
1.1 KiB
Go
Raw Normal View History

2018-06-04 16:05:14 -07:00
package pm
import (
"bytes"
"io/ioutil"
"strings"
"golang.org/x/crypto/openpgp/armor"
)
const (
2018-06-04 17:50:26 -07:00
pgpMessageType string = "PGP MESSAGE"
pgpPublicBlockType string = "PGP PUBLIC KEY BLOCK"
pgpPrivateBlockType string = "PGP PRIVATE KEY BLOCK"
2018-06-04 16:05:14 -07:00
)
2018-06-05 11:58:33 -07:00
// ArmorKey make bytes input key to armor format
func ArmorKey(input []byte) (string, error) {
return ArmorWithType(input, pgpPublicBlockType)
}
2018-06-04 16:05:14 -07:00
// ArmorWithType make bytes input to armor format
func ArmorWithType(input []byte, armorType string) (string, error) {
var b bytes.Buffer
2018-06-06 14:05:57 -07:00
w, err := armor.Encode(&b, armorType, armorHeader)
2018-06-04 16:05:14 -07:00
if err != nil {
return "", err
}
_, err = w.Write(input)
if err != nil {
return "", err
}
w.Close()
return b.String(), nil
}
// UnArmor an armored key to bytes key
func UnArmor(input string) ([]byte, error) {
b, err := unArmor(input)
if err != nil {
return nil, err
}
return ioutil.ReadAll(b.Body)
}
func unArmor(input string) (*armor.Block, error) {
io := strings.NewReader(input)
b, err := armor.Decode(io)
if err != nil {
return nil, err
}
return b, nil
}