Skip to content

Commit 68d7c65

Browse files
committed
feat: jwt verify sign
1 parent ce08d75 commit 68d7c65

File tree

5 files changed

+676
-3
lines changed

5 files changed

+676
-3
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ linters-settings:
154154

155155
funlen:
156156
lines: 200
157-
statements: 66
157+
statements: 100
158158

159159
gci:
160160
# put imports beginning with prefix after 3rd-party packages;
@@ -224,7 +224,7 @@ linters-settings:
224224

225225
gocyclo:
226226
# minimal code complexity to report, 30 by default (but we recommend 10-20)
227-
min-complexity: 30
227+
min-complexity: 50
228228

229229
godot:
230230
# comments to be checked: `declarations`, `toplevel`, or `all`

codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func jwtHS(alg string) (func(key []byte) hash.Hash, error) {
229229
return hmac.New(crypto.SHA512.New, key)
230230
}, nil
231231
default:
232-
return nil, errors.New("the algorithm is not supported")
232+
return nil, fmt.Errorf("the %s algorithm is not supported", alg)
233233
}
234234
}
235235

crypto_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ package gone
22

33
import (
44
bytes2 "bytes"
5+
"crypto/ecdsa"
6+
"crypto/elliptic"
57
"crypto/rand"
8+
"crypto/sha256"
9+
"crypto/x509"
610
"encoding/base64"
11+
"encoding/pem"
12+
"errors"
13+
"fmt"
14+
"strings"
715
"testing"
816

917
"golang.org/x/crypto/ssh"
@@ -188,3 +196,34 @@ func TestGenerateSSHKey(t *testing.T) {
188196
t.Fatal(err)
189197
}
190198
}
199+
200+
func TestGenerateECDSAKeyToMemory(t *testing.T) {
201+
privateBytes, publicBytes, err := GenerateECDSAKeyToMemory(elliptic.P256())
202+
if err != nil {
203+
t.Fatal(err)
204+
}
205+
block, _ := pem.Decode(privateBytes)
206+
privateKey, err := x509.ParseECPrivateKey(block.Bytes)
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
pbkBlock, _ := pem.Decode(publicBytes)
211+
pbk, err := x509.ParsePKIXPublicKey(pbkBlock.Bytes)
212+
if err != nil {
213+
t.Fatal(err)
214+
}
215+
publicKey, ok := pbk.(*ecdsa.PublicKey)
216+
if !ok {
217+
t.Fatal(errors.New("ecdsa publicKey error"))
218+
}
219+
220+
text := "hello world"
221+
randSign := "12345678901234567"
222+
hashText := sha256.Sum256([]byte(text))
223+
r, s, err := ecdsa.Sign(strings.NewReader(randSign), privateKey, hashText[:])
224+
if err != nil {
225+
t.Fatal(err)
226+
}
227+
b := ecdsa.Verify(publicKey, hashText[:], r, s)
228+
fmt.Println(b)
229+
}

0 commit comments

Comments
 (0)