Method from go-pmapi merge

This commit is contained in:
Jakub Lehotsky 2019-01-07 12:36:57 +01:00
parent d7f0550a4b
commit 39f5b3af18

View file

@ -8,6 +8,7 @@ import (
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
"regexp"
"strings" "strings"
"time" "time"
@ -32,6 +33,9 @@ type pmKeyObject struct {
Primary int Primary int
} }
// Armored type for PGP encrypted messages.
const PGP_MESSAGE_TYPE = "PGP MESSAGE"
func (ko *pmKeyObject) PrivateKeyReader() io.Reader { func (ko *pmKeyObject) PrivateKeyReader() io.Reader {
return strings.NewReader(ko.PrivateKey) return strings.NewReader(ko.PrivateKey)
} }
@ -266,6 +270,20 @@ func (kr *KeyRing) DecryptString(encrypted string) (SignedString, error) {
return SignedString{String: s, Signed: signed}, nil return SignedString{String: s, Signed: signed}, nil
} }
// Decrypt data if has PGP MESSAGE format, if not return original data.
// If error is errors.ErrSignatureExpired (from golang.org/x/crypto/openpgp/errors),
// contents are still provided if library clients wish to process this message further
func (kr *KeyRing) DecryptStringIfNeeded(data string) (decrypted string, err error) {
if re := regexp.MustCompile("^-----BEGIN " + PGP_MESSAGE_TYPE + "-----(?s:.+)-----END " + PGP_MESSAGE_TYPE + "-----"); re.MatchString(data) {
var signed SignedString
signed, err = kr.DecryptString(data)
decrypted = signed.String
} else {
decrypted = data
}
return
}
// Sign a string message, using this KeyRing. canonicalizeText identifies if newlines are canonicalized // Sign a string message, using this KeyRing. canonicalizeText identifies if newlines are canonicalized
func (kr *KeyRing) SignString(message string, canonicalizeText bool) (signed string, err error) { func (kr *KeyRing) SignString(message string, canonicalizeText bool) (signed string, err error) {