hkp

package module
Version: v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2021 License: MIT Imports: 12 Imported by: 0

README

hkp

Package hkp provides a hkp (http keyserver protocol) client.

Example

See example_test.go for examples.

Documentation

Overview

Package hkp provides a hkp (http keyserver protocol) client.

Index

Examples

Constants

View Source
const DefaultKeyserver = "keyserver.ubuntu.com"

DefaultKeyserver is the default keyserver used by hkp clients.

Variables

This section is empty.

Functions

func GetKey

func GetKey(ctx context.Context, id string, opts ...Option) ([]byte, error)

GetKey retrieves the specified key id from a hkp keyserver using the provided options.

Example

ExampleGetKey shows retrieving a specified key id.

package main

import (
	"context"
	"crypto/md5"
	"fmt"
	"log"

	"github.com/kenshaw/hkp"
)

func main() {
	id := "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
	buf, err := hkp.GetKey(context.Background(), id)
	if err != nil {
		log.Fatal(err)
	}
	hash := fmt.Sprintf("%x", md5.Sum(buf))
	fmt.Println("hash:", hash)
}
Output:

hash: 6ba146bef75ec72d419a2395b663039a
Example (Verify)

ExampleGetKey_verify shows retrieving the fingerprint from the retrieved key.

package main

import (
	"bytes"
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/kenshaw/hkp"
	"golang.org/x/crypto/openpgp"
)

func main() {
	id := "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
	buf, err := hkp.GetKey(context.Background(), id)
	if err != nil {
		log.Fatal(err)
	}
	keys, err := openpgp.ReadArmoredKeyRing(bytes.NewReader(buf))
	if err != nil {
		log.Fatal(err)
	}
	if len(keys) != 1 {
		log.Fatal("expected keys length to be 1")
	}
	fingerprint := fmt.Sprintf("%x\n", keys[0].PrimaryKey.Fingerprint)
	fmt.Println("fingerprint:", strings.ToUpper(fingerprint))
}
Output:

fingerprint: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88

func GetKeys

func GetKeys(ctx context.Context, ids ...string) ([]byte, error)

GetKeys retrieves the specified key ids from the default hkp keyserver.

Example

ExampleGetKeys shows retrieve retrieving multiple keys.

package main

import (
	"context"
	"crypto/md5"
	"fmt"
	"log"

	"github.com/kenshaw/hkp"
)

func main() {
	ids := []string{
		// nodejs team keys
		"4ED778F539E3634C779C87C6D7062848A1AB005C", // Beth Griggs <bgriggs@redhat.com>
		"94AE36675C464D64BAFA68DD7434390BDBE9B9C5", // Colin Ihrig <cjihrig@gmail.com>
		"74F12602B6F1C4E913FAA37AD3A89613643B6201", // Danielle Adams <adamzdanielle@gmail.com>
		"71DCFD284A79C3B38668286BC97EC7A07EDE3FC1", // James M Snell <jasnell@keybase.io>
		"8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600", // Michaël Zasso <targos@protonmail.com>
		"C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8", // Myles Borins <myles.borins@gmail.com>
		"C82FA3AE1CBEDC6BE46B9360C43CEC45C17AB93C", // Richard Lau <rlau@redhat.com>
		"DD8F2338BAE7501E3DD5AC78C273792F7D83545D", // Rod Vagg <rod@vagg.org>
		"A48C2BEE680E841632CD4E44F07496B3EB3C1762", // Ruben Bridgewater <ruben@bridgewater.de>
		"108F52B48DB57BB0CC439B2997B01419BD92F80A", // Ruy Adorno <ruyadorno@hotmail.com>
		"B9E2F5981AA6E0CD28160D9FF13993A75599653C", // Shelley Vohr <shelley.vohr@gmail.com>
	}
	cl := hkp.New()
	buf, err := cl.GetKeys(context.Background(), ids...)
	if err != nil {
		log.Fatal(err)
	}
	hash := fmt.Sprintf("%x", md5.Sum(buf))
	fmt.Println("hash:", hash)
}
Output:

hash: f2f6645fe14368105bd90ae834a88eaf

func ParseURL

func ParseURL(urlstr string, params ...string) (string, error)

ParseURL parses a keyserver url, adding the specified params as query values.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a hkp (http keyserver protocol) client.

func New

func New(opts ...Option) *Client

New creates a new hkp client.

func (*Client) GetKey

func (cl *Client) GetKey(ctx context.Context, id string) ([]byte, error)

GetKey returns the specified key id from a hkp keyserver.

func (*Client) GetKeys

func (cl *Client) GetKeys(ctx context.Context, ids ...string) ([]byte, error)

GetKeys retrieves the key ids.

type Error

type Error string

Error is a package error.

const (
	// ErrInvalidKeyID is the invalid key id error.
	ErrInvalidKeyID Error = "invalid key id"
	// ErrInvalidParams is the invalid params error.
	ErrInvalidParams Error = "invalid params"
	// ErrInvalidScheme is the invalid scheme error.
	ErrInvalidScheme Error = "invalid scheme"
	// ErrKeyNotFound is the key not found error.
	ErrKeyNotFound Error = "key not found"
)

Error values.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Option

type Option func(*Client)

Option is a hkp client option.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient is a hkp client option to set the underlying http client.

func WithKeyserver

func WithKeyserver(keyserver string) Option

WithKeyserver is a hkp client option to set the keyserver.

func WithTransport

func WithTransport(transport http.RoundTripper) Option

WithTransport is a hkp client option to set the underlying http client transport.

Source Files

Directories

Path Synopsis
cmd
hkp

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL