Debian Codegraph @

command/crypto/jwk/thumbprint.go

raw symbols SCIP
1 package jwk
2
3 import (
4 "crypto"
5 "encoding/base64"
6 "encoding/json"
7 "fmt"
8 "io/ioutil"
9 "os"
10
11 "github.com/pkg/errors"
12 "github.com/smallstep/cli/jose"
13 "github.com/urfave/cli"
14 )
15
16 func thumbprintCommand() cli.Command {
17 return cli.Command{
18 Name: "thumbprint",
19 Action: cli.ActionFunc(thumbprintAction),
20 Usage: "compute thumbprint for a JWK",
21 UsageText: `**step crypto jwk thumbprint**`,
22 Description: `**step crypto jwk thumbprint** reads a JWK from STDINT, derives the
23 corresponding JWK Thumbprint (RFC7638), and prints the base64-urlencoded
24 thumbprint to STDOUT.
25
26 For examples, see **step help crypto jwk**.`,
27 }
28 }
29
30 func thumbprintAction(ctx *cli.Context) error {
31 b, err := ioutil.ReadAll(os.Stdin)
32 if err != nil {
33 return errors.Wrap(err, "error reading from STDIN")
34 }
35
36 jwk := new(jose.JSONWebKey)
37 // Attempt to decrypt if encrypted
38 if b, err = jose.Decrypt("Please enter the password to decrypt your private JWK", b); err != nil {
39 return err
40 }
41
42 // Unmarshal the plain (or decrypted JWK)
43 if err = json.Unmarshal(b, jwk); err != nil {
44 return errors.New("error reading JWK: unsupported format")
45 }
46
47 hash, err := jwk.Thumbprint(crypto.SHA256)
48 if err != nil {
49 return errors.Wrap(err, "error generating JWK thumbprint")
50 }
51 fmt.Println(base64.RawURLEncoding.EncodeToString(hash))
52 return nil
53 }