add trim leading function

This commit is contained in:
zhj4478 2018-06-05 15:26:55 -07:00
parent efc003288c
commit 701f1bc60a
3 changed files with 23 additions and 1 deletions

View file

@ -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, "")
}

View file

@ -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 {

View file

@ -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 {