From c36e47e7a19d837928d63b30a8bc38bf9be56ea5 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Wed, 19 Jul 2023 16:14:03 +0200 Subject: [PATCH] add DigestSigner example Signed-off-by: qmuntal --- example_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/example_test.go b/example_test.go index 07ea624..afdf128 100644 --- a/example_test.go +++ b/example_test.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" + "crypto/sha512" _ "crypto/sha512" "fmt" @@ -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 +}