-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
jws_custom_signer_verifier_example_test.go
78 lines (64 loc) · 1.96 KB
/
jws_custom_signer_verifier_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package examples_test
import (
"crypto/rand"
"fmt"
"github.com/cloudflare/circl/sign/ed25519"
"github.com/lestrrat-go/jwx/v3/jwa"
"github.com/lestrrat-go/jwx/v3/jws"
)
type CirclEdDSASignerVerifier struct{}
func NewCirclEdDSASigner() (jws.Signer, error) {
return &CirclEdDSASignerVerifier{}, nil
}
func NewCirclEdDSAVerifier() (jws.Verifier, error) {
return &CirclEdDSASignerVerifier{}, nil
}
func (s CirclEdDSASignerVerifier) Algorithm() jwa.SignatureAlgorithm {
return jwa.EdDSA()
}
func (s CirclEdDSASignerVerifier) Sign(payload []byte, keyif interface{}) ([]byte, error) {
switch key := keyif.(type) {
case ed25519.PrivateKey:
return ed25519.Sign(key, payload), nil
default:
return nil, fmt.Errorf(`invalid key type %T`, keyif)
}
}
func (s CirclEdDSASignerVerifier) Verify(payload []byte, signature []byte, keyif interface{}) error {
switch key := keyif.(type) {
case ed25519.PublicKey:
if ed25519.Verify(key, payload, signature) {
return nil
}
return fmt.Errorf(`failed to verify EdDSA signature`)
default:
return fmt.Errorf(`invalid key type %T`, keyif)
}
}
func ExampleJWS_CustomSignerVerifier() {
// This example shows how to register external jws.Signer / jws.Verifier for
// a given algorithm.
jws.RegisterSigner(jwa.EdDSA(), jws.SignerFactoryFn(NewCirclEdDSASigner))
jws.RegisterVerifier(jwa.EdDSA(), jws.VerifierFactoryFn(NewCirclEdDSAVerifier))
pubkey, privkey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
fmt.Printf(`failed to generate keys: %s`, err)
return
}
const payload = "Lorem Ipsum"
signed, err := jws.Sign([]byte(payload), jws.WithKey(jwa.EdDSA(), privkey))
if err != nil {
fmt.Printf(`failed to generate signed message: %s`, err)
return
}
verified, err := jws.Verify(signed, jws.WithKey(jwa.EdDSA(), pubkey))
if err != nil {
fmt.Printf(`failed to verify signed message: %s`, err)
return
}
if string(verified) != payload {
fmt.Printf(`got invalid payload: %s`, verified)
return
}
// OUTPUT:
}