Skip to content

Commit

Permalink
Use a symmetric key for example purposes (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat authored Apr 29, 2023
1 parent 0d936c8 commit 4959f78
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions examples/jwk_example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package examples_test

import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"fmt"
"log"

Expand Down Expand Up @@ -69,22 +66,23 @@ func ExampleJWK_Usage() {

//nolint:govet
func ExampleJWK_MarshalJSON() {
// to get the same values every time, we need to create a static source
// of "randomness"
rdr := bytes.NewReader([]byte("01234567890123456789012345678901234567890123456789ABCDEF"))
raw, err := ecdsa.GenerateKey(elliptic.P384(), rdr)
if err != nil {
fmt.Printf("failed to generate new ECDSA private key: %s\n", err)
return
}
// JWKs that inherently involve randomness such as RSA and EC keys are
// not used in this example, because they may produce different results
// depending on the environment.
//
// (In fact, even if you use a static source of randomness, tests may fail
// because of internal changes in the Go runtime).

raw := []byte("01234567890123456789012345678901234567890123456789ABCDEF")

// This would create a symmetric key
key, err := jwk.FromRaw(raw)
if err != nil {
fmt.Printf("failed to create ECDSA key: %s\n", err)
fmt.Printf("failed to create symmetric key: %s\n", err)
return
}
if _, ok := key.(jwk.ECDSAPrivateKey); !ok {
fmt.Printf("expected jwk.ECDSAPrivateKey, got %T\n", key)
if _, ok := key.(jwk.SymmetricKey); !ok {
fmt.Printf("expected jwk.SymmetricKey, got %T\n", key)
return
}

Expand All @@ -99,11 +97,8 @@ func ExampleJWK_MarshalJSON() {

// OUTPUT:
// {
// "crv": "P-384",
// "d": "ODkwMTIzNDU2Nzg5MDEyMz7deMbyLt8g4cjcxozuIoygLLlAeoQ1AfM9TSvxkFHJ",
// "k": "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODlBQkNERUY",
// "kid": "mykey",
// "kty": "EC",
// "x": "gvvRMqm1w5aHn7sVNA2QUJeOVcedUnmiug6VhU834gzS9k87crVwu9dz7uLOdoQl",
// "y": "7fVF7b6J_6_g6Wu9RuJw8geWxEi5ja9Gp2TSdELm5u2E-M7IF-bsxqcdOj3n1n7N"
// "kty": "oct"
// }
}

0 comments on commit 4959f78

Please sign in to comment.