pubkeyhash

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:    "pubkeyhash",
	Usage:   "Dump pubkeyhash of the specified public key/certificate",
	Aliases: []string{"hash"},
	Flags: []cli.Flag{
		&cli.PathFlag{
			Name:    "file",
			Aliases: []string{"f"},
			Usage:   "PEM file containing public key/certificates",
			Value:   "-",
		},
	},
	Action: func(c *cli.Context) error {
		env := action.GlobalEnvironment
		slog := env.Logger.Sugar()

		var r io.Reader

		path := c.String("file")
		if path == "-" {
			r = os.Stdin
		} else {
			f, err := os.Open(path)
			if err != nil {
				return err
			}
			defer f.Close()

			r = f
		}

		bs, err := ioutil.ReadAll(r)
		if err != nil {
			return err
		}
		err = pemparser.ForeachPemBlock(bs, func(b *pem.Block) error {
			var pub crypto.PublicKey

			switch b.Type {
			case pemparser.CertificatePemType:
				cert, err := x509.ParseCertificate(b.Bytes)
				if err != nil {

					slog.Errorf("Failed to parse a %s block: %v", b.Type, err)
					return nil
				}

				pub = cert.PublicKey
			case pemparser.PublicKeyPemType:
				pubi, err := x509.ParsePKIXPublicKey(b.Bytes)
				if err != nil {

					slog.Errorf("Failed to parse a %s block: %v", b.Type, err)
					return nil
				}

				var ok bool
				pub, ok = pubi.(crypto.PublicKey)
				if !ok {
					slog.Errorf("Unknown public key type: %v", reflect.TypeOf(pubi))
					return nil
				}
			}
			if pub == nil {
				return nil
			}

			hash, err := wcrypto.PubKeyPinString(pub)
			if err != nil {
				slog.Errorf("Failed to generate pubkeyhash: %v", err)
				return nil
			}

			fmt.Printf("%s\n", hash)
			return nil
		})
		if err != nil {
			return err
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL