Canonicalize line endings for text messages (#86)
* Canonicalize line endings for text messages * Improve cleartext messages
This commit is contained in:
parent
a4d89bce32
commit
ce607e0fa8
6 changed files with 25 additions and 25 deletions
|
|
@ -1,8 +1,6 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
"github.com/ProtonMail/gopenpgp/v2/internal"
|
||||
)
|
||||
|
|
@ -50,7 +48,7 @@ func VerifyCleartextMessageArmored(publicKey, armored string, verifyTime int64)
|
|||
// SignCleartextMessage signs text given a private keyring, canonicalizes and
|
||||
// trims the newlines, and returns the PGP-compliant special armoring.
|
||||
func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) {
|
||||
text = canonicalizeAndTrim(text)
|
||||
text = internal.TrimWhitespace(text)
|
||||
message := crypto.NewPlainMessageFromString(text)
|
||||
|
||||
signature, err := keyRing.SignDetached(message)
|
||||
|
|
@ -79,12 +77,3 @@ func VerifyCleartextMessage(keyRing *crypto.KeyRing, armored string, verifyTime
|
|||
|
||||
return message.GetString(), nil
|
||||
}
|
||||
|
||||
// ----- INTERNAL FUNCTIONS -----
|
||||
|
||||
// canonicalizeAndTrim alters a string canonicalizing and trimming the newlines.
|
||||
func canonicalizeAndTrim(text string) string {
|
||||
text = internal.TrimNewlines(text)
|
||||
text = strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")
|
||||
return text
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,16 @@ package helper
|
|||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
"github.com/ProtonMail/gopenpgp/v2/internal"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const signedPlainText = "Signed message\n"
|
||||
const inputPlainText = " Signed message\n \n "
|
||||
const signedPlainText = " Signed message\n\n"
|
||||
|
||||
var signedMessageTest = regexp.MustCompile(
|
||||
"(?s)^-----BEGIN PGP SIGNED MESSAGE-----.*-----BEGIN PGP SIGNATURE-----.*-----END PGP SIGNATURE-----$")
|
||||
|
|
@ -18,7 +21,7 @@ func TestSignClearText(t *testing.T) {
|
|||
armored, err := SignCleartextMessageArmored(
|
||||
readTestFile("keyring_privateKey", false),
|
||||
testMailboxPassword,
|
||||
signedPlainText,
|
||||
inputPlainText,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -36,11 +39,17 @@ func TestSignClearText(t *testing.T) {
|
|||
t.Fatal("Cannot verify message:", err)
|
||||
}
|
||||
|
||||
assert.Exactly(t, canonicalizeAndTrim(signedPlainText), verified)
|
||||
assert.Exactly(t, signedPlainText, verified)
|
||||
|
||||
clearTextMessage, err := crypto.NewClearTextMessageFromArmored(armored)
|
||||
if err != nil {
|
||||
t.Fatal("Cannot parse message:", err)
|
||||
}
|
||||
assert.Exactly(t, canonicalizeAndTrim(inputPlainText), string(clearTextMessage.GetBinary()))
|
||||
}
|
||||
|
||||
func TestMessageCanonicalizeAndTrim(t *testing.T) {
|
||||
text := "Hi \ntest!\r\n\n"
|
||||
canon := canonicalizeAndTrim(text)
|
||||
assert.Exactly(t, "Hi\r\ntest!\r\n\r\n", canon)
|
||||
func canonicalizeAndTrim(text string) string {
|
||||
text = internal.TrimWhitespace(text)
|
||||
text = strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")
|
||||
return text
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue