forked from mozilla-services/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
34 lines (28 loc) · 864 Bytes
/
sign.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
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))
}
}