forked from mozilla-services/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
45 lines (37 loc) · 1.15 KB
/
verify.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
package main
import (
"crypto/rand"
"fmt"
cose "go.mozilla.org/cose"
)
func main() {
// create a signer with a new private key
signer, err := cose.NewSigner(cose.ES256, nil)
if err != nil {
panic(fmt.Sprintf(fmt.Sprintf("Error creating signer %s", err)))
}
// create a signature
sig := cose.NewSignature()
sig.Headers.Unprotected["kid"] = 1
sig.Headers.Protected["alg"] = "ES256"
// create a message
external := []byte("") // optional external data see https://tools.ietf.org/html/rfc8152#section-4.3
msg := cose.NewSignMessage()
msg.Payload = []byte("payload to sign")
msg.AddSignature(sig)
err = msg.Sign(rand.Reader, external, []cose.Signer{*signer})
if err == nil {
fmt.Println(fmt.Sprintf("Message signature (ES256): %x", msg.Signatures[0].SignatureBytes))
} else {
panic(fmt.Sprintf("Error signing the message %+v", err))
}
// derive a verifier using the signer's public key and COSE algorithm
verifier := signer.Verifier()
// Verify
err = msg.Verify(external, []cose.Verifier{*verifier})
if err == nil {
fmt.Println("Message signature verified")
} else {
fmt.Println(fmt.Sprintf("Error verifying the message %+v", err))
}
}