2018-11-05 22:55:45 +01:00
|
|
|
// This package contains a set of helper methods for armoring and unarmoring
|
2018-09-11 11:09:28 +02:00
|
|
|
package armor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2018-09-19 11:52:14 +02:00
|
|
|
"errors"
|
2019-05-13 14:07:18 +02:00
|
|
|
"github.com/ProtonMail/gopenpgp/constants"
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/internal"
|
2018-09-11 11:09:28 +02:00
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
|
|
|
"golang.org/x/crypto/openpgp/clearsign"
|
|
|
|
|
"io"
|
2018-09-19 11:52:14 +02:00
|
|
|
"io/ioutil"
|
2018-09-11 11:09:28 +02:00
|
|
|
)
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// ArmorKey makes bytes input key to armor format
|
2018-09-11 11:09:28 +02:00
|
|
|
func ArmorKey(input []byte) (string, error) {
|
2019-03-07 16:56:12 +01:00
|
|
|
return ArmorWithType(input, constants.PublicKeyHeader)
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// ArmorWithTypeBuffered takes input from io.Writer and returns io.WriteCloser which can be read for armored code
|
2018-11-05 22:55:45 +01:00
|
|
|
func ArmorWithTypeBuffered(w io.Writer, armorType string) (io.WriteCloser, error) {
|
|
|
|
|
return armor.Encode(w, armorType, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// ArmorWithType makes bytes input to armor format
|
2018-09-11 11:09:28 +02:00
|
|
|
func ArmorWithType(input []byte, armorType string) (string, error) {
|
|
|
|
|
var b bytes.Buffer
|
2018-11-05 22:55:45 +01:00
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
w, err := armor.Encode(&b, armorType, internal.ArmorHeaders)
|
2018-11-05 22:55:45 +01:00
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
_, err = w.Write(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
w.Close()
|
|
|
|
|
return b.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 11:52:14 +02:00
|
|
|
// Unarmor an armored key to bytes key
|
|
|
|
|
func Unarmor(input string) ([]byte, error) {
|
|
|
|
|
b, err := internal.Unarmor(input)
|
2018-09-11 11:09:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ioutil.ReadAll(b.Body)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:01 +00:00
|
|
|
// ReadClearSignedMessage reads clear message from a clearsign package (package containing cleartext and signature)
|
2018-09-11 11:09:28 +02:00
|
|
|
func ReadClearSignedMessage(signedMessage string) (string, error) {
|
|
|
|
|
modulusBlock, rest := clearsign.Decode([]byte(signedMessage))
|
|
|
|
|
if len(rest) != 0 {
|
|
|
|
|
return "", errors.New("pmapi: extra data after modulus")
|
|
|
|
|
}
|
|
|
|
|
return string(modulusBlock.Bytes), nil
|
|
|
|
|
}
|