Skip to content

Commit

Permalink
Drop regex for canonicalization (#102)
Browse files Browse the repository at this point in the history
* Drop regex for canonicalization

* Fix CI
  • Loading branch information
wussler authored Dec 1, 2020
1 parent 5b1a42c commit 385e6d2
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ linters:
- gci # Enforce blank lines check
- nlreturn # Enforce blank lines for return statements
- 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/),
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
### Changed
- Session key decryption now considers multiple key packets
Expand Down
2 changes: 1 addition & 1 deletion crypto/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewPlainMessageFromFile(data []byte, filename string, time uint32) *PlainMe
// ready for encryption, signature, or verification from an unencrypted string.
func NewPlainMessageFromString(text string) *PlainMessage {
return &PlainMessage{
Data: []byte(strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")),
Data: []byte(internal.CanonicalizeAndTrim(text)),
TextType: true,
Filename: "",
Time: uint32(GetUnixTime()),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ require (

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
2 changes: 0 additions & 2 deletions helper/cleartext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package helper

import (
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/internal"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -49,7 +48,6 @@ 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 = internal.TrimWhitespace(text)
message := crypto.NewPlainMessageFromString(text)

signature, err := keyRing.SignDetached(message)
Expand Down
12 changes: 3 additions & 9 deletions helper/cleartext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package helper

import (
"regexp"
"strings"
"testing"

"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/internal"

"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -45,11 +45,5 @@ func TestSignClearText(t *testing.T) {
if err != nil {
t.Fatal("Cannot parse message:", err)
}
assert.Exactly(t, 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
assert.Exactly(t, internal.CanonicalizeAndTrim(inputPlainText), string(clearTextMessage.GetBinary()))
}
15 changes: 9 additions & 6 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
package internal

import (
"regexp"
"strings"

"github.com/ProtonMail/gopenpgp/v2/constants"
)

// TrimWhitespace removes whitespace from the end of each line of the input
// string.
func TrimWhitespace(input string) string {
var re = regexp.MustCompile(`(?m)[ \t]*$`)
return re.ReplaceAllString(input, "")
func CanonicalizeAndTrim(text string) string {
lines := strings.Split(text, "\n")

for i := range lines {
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
Expand Down

0 comments on commit 385e6d2

Please sign in to comment.