Skip to content

Commit

Permalink
add DigestSigner example
Browse files Browse the repository at this point in the history
Signed-off-by: qmuntal <[email protected]>
  • Loading branch information
qmuntal committed Jul 19, 2023
1 parent 82a1648 commit c36e47e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha512"
_ "crypto/sha512"
"fmt"

Expand Down Expand Up @@ -202,3 +203,29 @@ func ExampleSign1Untagged() {
// Output:
// message signed
}

func ExampleDigestSigner() {
// create a signer
privateKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
if err != nil {
panic(err)
}
signer, err := cose.NewSigner(cose.AlgorithmES256, privateKey)
if err != nil {
panic(err)
}
digestSigner, ok := signer.(cose.DigestSigner)
if !ok {
panic("signer does not support digest signing")
}

// hash payload outside go-cose.
payload := []byte("hello world")
digested := sha512.Sum512(payload)
sig, err := digestSigner.SignDigest(rand.Reader, digested[:])

fmt.Println("digest signed")
_ = sig // further process on sig
// Output:
// digest signed
}

0 comments on commit c36e47e

Please sign in to comment.