Skip to content

Commit 8b046e5

Browse files
committed
fix: do not decode the signature in the plain key from base64
As it looks like it already comes decoded. Signed-off-by: Artem Chernyshev <[email protected]>
1 parent 7e98556 commit 8b046e5

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

pkg/plain/ecdsa.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,14 @@ func (p *EcdsaKey) ID() string {
4646
func (p *EcdsaKey) Verify(data, signature []byte) error {
4747
hash := sha256.Sum256(data)
4848

49-
sigBytes, err := base64.StdEncoding.DecodeString(string(signature))
50-
if err != nil {
51-
return errors.New("missing valid signature")
52-
}
53-
54-
if len(sigBytes)%2 != 0 {
49+
if len(signature)%2 != 0 {
5550
return errors.New("missing valid signature")
5651
}
5752

58-
half := len(sigBytes) / 2
53+
half := len(signature) / 2
5954

60-
r := new(big.Int).SetBytes(sigBytes[:half])
61-
s := new(big.Int).SetBytes(sigBytes[half:])
55+
r := new(big.Int).SetBytes(signature[:half])
56+
s := new(big.Int).SetBytes(signature[half:])
6257

6358
if !ecdsa.Verify(p.key, hash[:], r, s) {
6459
return errors.New("missing valid signature")

pkg/plain/plain_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package plain_test
66

77
import (
8+
"encoding/base64"
89
"testing"
910

1011
"github.com/stretchr/testify/require"
@@ -24,7 +25,11 @@ func TestECDSASignature(t *testing.T) {
2425

2526
require.NoError(t, err)
2627

27-
require.NoError(t, key.Verify([]byte("hi there"), []byte(signature)))
28+
signatureBytes, err := base64.StdEncoding.DecodeString(signature)
2829

29-
require.ErrorContains(t, key.Verify([]byte("hi there?"), []byte(signature)), "missing valid signature")
30+
require.NoError(t, err)
31+
32+
require.NoError(t, key.Verify([]byte("hi there"), signatureBytes))
33+
34+
require.ErrorContains(t, key.Verify([]byte("hi there?"), signatureBytes), "missing valid signature")
3035
}

0 commit comments

Comments
 (0)