diff --git a/common.go b/common.go index 17fa6ae..ae610a6 100644 --- a/common.go +++ b/common.go @@ -1,5 +1,9 @@ package pm +import ( + "regexp" +) + var armorHeader = map[string]string{ "Version": "OpenPGP Golang 0.0.1 (" + Version() + ")", "Comment": "https://protonmail.com", @@ -21,3 +25,8 @@ type Address struct { // address_name : string; keys []Key } + +func trimNewlines(input string) string { + var re = regexp.MustCompile(`(?m)[ \t]*$`) + return re.ReplaceAllString(input, "") +} diff --git a/message.go b/message.go index 2a17e39..2c2166e 100644 --- a/message.go +++ b/message.go @@ -227,6 +227,9 @@ func (o *OpenPGP) EncryptMessage(plainText string, publicKey string, privateKey // passphrase : optional required when you pass the private key and this passphrase must could decrypt the private key func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, privateKey string, passphrase string, trim bool) (string, error) { + if trim { + plainText = trimNewlines(plainText) + } var outBuf bytes.Buffer w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader) if err != nil { diff --git a/sign_detached.go b/sign_detached.go index a65e969..e05bbe0 100644 --- a/sign_detached.go +++ b/sign_detached.go @@ -25,6 +25,10 @@ func (o *OpenPGP) SignTextDetached(plainText string, privateKey string, passphra //sign with 0x01 text var signEntity *openpgp.Entity + if trim { + plainText = trimNewlines(plainText) + } + signerReader := strings.NewReader(privateKey) signerEntries, err := openpgp.ReadArmoredKeyRing(signerReader) if err != nil { @@ -66,6 +70,10 @@ func (o *OpenPGP) SignTextDetachedBinKey(plainText string, privateKey []byte, pa //sign with 0x01 var signEntity *openpgp.Entity + if trim { + plainText = trimNewlines(plainText) + } + signerReader := bytes.NewReader(privateKey) signerEntries, err := openpgp.ReadKeyRing(signerReader) if err != nil { @@ -196,6 +204,8 @@ func (o *OpenPGP) VerifyTextSignDetached(signature string, plainText string, pub signatureReader := strings.NewReader(signature) + plainText = trimNewlines(plainText) + origText := bytes.NewReader(bytes.NewBufferString(plainText).Bytes()) config := &packet.Config{} @@ -230,7 +240,7 @@ func (o *OpenPGP) VerifyTextSignDetachedBinKey(signature string, plainText strin } signatureReader := strings.NewReader(signature) - + plainText = trimNewlines(plainText) origText := bytes.NewReader(bytes.NewBufferString(plainText).Bytes()) config := &packet.Config{} if verifyTime > 0 {