Drop regex for canonicalization (#102)

* Drop regex for canonicalization

* Fix CI
This commit is contained in:
wussler 2020-12-01 18:09:25 +01:00 committed by GitHub
parent 5b1a42c2cd
commit 385e6d21d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 19 deletions

View file

@ -30,3 +30,4 @@ linters:
- gci # Enforce blank lines check - gci # Enforce blank lines check
- nlreturn # Enforce blank lines for return statements - nlreturn # Enforce blank lines for return statements
- exhaustivestruct # Enforce structures to be fully filled on instantiation - terrible with openpgp configs - exhaustivestruct # Enforce structures to be fully filled on instantiation - terrible with openpgp configs
- paralleltest # detects missing usage of t.Parallel() method in your Go test

View file

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Changed
- Improved canonicalization performance
## [2.1.1] 2020-11-16 ## [2.1.1] 2020-11-16
### Changed ### Changed
- Session key decryption now considers multiple key packets - Session key decryption now considers multiple key packets

View file

@ -88,7 +88,7 @@ func NewPlainMessageFromFile(data []byte, filename string, time uint32) *PlainMe
// ready for encryption, signature, or verification from an unencrypted string. // ready for encryption, signature, or verification from an unencrypted string.
func NewPlainMessageFromString(text string) *PlainMessage { func NewPlainMessageFromString(text string) *PlainMessage {
return &PlainMessage{ return &PlainMessage{
Data: []byte(strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")), Data: []byte(internal.CanonicalizeAndTrim(text)),
TextType: true, TextType: true,
Filename: "", Filename: "",
Time: uint32(GetUnixTime()), Time: uint32(GetUnixTime()),

2
go.mod
View file

@ -12,4 +12,4 @@ require (
replace golang.org/x/crypto => github.com/ProtonMail/crypto v0.0.0-20201112115411-41db4ea0dd1c replace golang.org/x/crypto => github.com/ProtonMail/crypto v0.0.0-20201112115411-41db4ea0dd1c
replace golang.org/x/mobile => github.com/zhj4478/mobile v0.0.0-20201014085805-7a2d68bf792f replace golang.org/x/mobile => github.com/marinthiercelin/mobile v0.0.0-20201127122539-61ba718dc1d1

View file

@ -2,7 +2,6 @@ package helper
import ( import (
"github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/internal"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -49,7 +48,6 @@ func VerifyCleartextMessageArmored(publicKey, armored string, verifyTime int64)
// SignCleartextMessage signs text given a private keyring, canonicalizes and // SignCleartextMessage signs text given a private keyring, canonicalizes and
// trims the newlines, and returns the PGP-compliant special armoring. // trims the newlines, and returns the PGP-compliant special armoring.
func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) { func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) {
text = internal.TrimWhitespace(text)
message := crypto.NewPlainMessageFromString(text) message := crypto.NewPlainMessageFromString(text)
signature, err := keyRing.SignDetached(message) signature, err := keyRing.SignDetached(message)

View file

@ -2,11 +2,11 @@ package helper
import ( import (
"regexp" "regexp"
"strings"
"testing" "testing"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/internal" "github.com/ProtonMail/gopenpgp/v2/internal"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -45,11 +45,5 @@ func TestSignClearText(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("Cannot parse message:", err) t.Fatal("Cannot parse message:", err)
} }
assert.Exactly(t, canonicalizeAndTrim(inputPlainText), string(clearTextMessage.GetBinary())) assert.Exactly(t, internal.CanonicalizeAndTrim(inputPlainText), string(clearTextMessage.GetBinary()))
}
func canonicalizeAndTrim(text string) string {
text = internal.TrimWhitespace(text)
text = strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")
return text
} }

View file

@ -2,16 +2,19 @@
package internal package internal
import ( import (
"regexp" "strings"
"github.com/ProtonMail/gopenpgp/v2/constants" "github.com/ProtonMail/gopenpgp/v2/constants"
) )
// TrimWhitespace removes whitespace from the end of each line of the input func CanonicalizeAndTrim(text string) string {
// string. lines := strings.Split(text, "\n")
func TrimWhitespace(input string) string {
var re = regexp.MustCompile(`(?m)[ \t]*$`) for i := range lines {
return re.ReplaceAllString(input, "") lines[i] = strings.TrimRight(lines[i], " \t\r")
}
return strings.Join(lines, "\r\n")
} }
// CreationTimeOffset stores the amount of seconds that a signature may be // CreationTimeOffset stores the amount of seconds that a signature may be