key

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package key provides Ed25519 keys, signatures, and endpoint identifiers for go-iroh.

A SecretKey signs messages and derives a PublicKey. Use PublicKey for cryptographic operations and EndpointID for network-facing endpoint identities. Keys and endpoint IDs render as lowercase hex by default; EndpointID.Z32 returns the z-base-32 form used in pkarr names.

The Go API is not stable before v1 and may change in any v0 release.

Index

Examples

Constants

View Source
const (
	// PublicKeySize is the size of an Ed25519 public key, in bytes.
	PublicKeySize = ed25519.PublicKeySize
	// PrivateKeySize is the size of an Ed25519 private key, in bytes.
	PrivateKeySize = ed25519.PrivateKeySize
	// SeedSize is the size of an Ed25519 private key seed, in bytes.
	SeedSize = ed25519.SeedSize
	// SignatureSize is the size of an Ed25519 signature, in bytes.
	SignatureSize = ed25519.SignatureSize
)

Variables

View Source
var (
	// ErrInvalidKeyData is returned when bytes do not represent a valid
	// Ed25519 curve point.
	ErrInvalidKeyData = errors.New("data is not a valid public key")
	// ErrInvalidKeyLength is returned when key bytes have the wrong length.
	ErrInvalidKeyLength = errors.New("invalid length")
	// ErrDecodeHex is returned when a string cannot be decoded as hex.
	ErrDecodeHex = errors.New("failed to decode hex string")
	// ErrDecodeBase32 is returned when a string cannot be decoded as base32.
	ErrDecodeBase32 = errors.New("failed to decode base32 string")
)

Errors returned when parsing keys.

View Source
var (
	// ErrInvalidSignatureParse is returned when bytes cannot be parsed as an
	// Ed25519 signature.
	ErrInvalidSignatureParse = errors.New("could not parse ed25519 signature")
	// ErrInvalidSignature is returned when signature verification fails.
	ErrInvalidSignature = errors.New("invalid signature")
)

Signature parsing and verification errors.

Functions

This section is empty.

Types

type EndpointID

type EndpointID PublicKey

EndpointID is a network-facing identifier for an endpoint.

Use EndpointID in network-facing APIs and PublicKey when performing cryptographic operations.

func EndpointIDFromSlice

func EndpointIDFromSlice(b []byte) (EndpointID, error)

EndpointIDFromSlice constructs an EndpointID from a byte slice.

func NewEndpointID

func NewEndpointID(b [PublicKeySize]byte) (EndpointID, error)

NewEndpointID constructs an EndpointID from a 32-byte array.

func ParseEndpointID

func ParseEndpointID(s string) (EndpointID, error)

ParseEndpointID parses an EndpointID from its hex or RFC 4648 base32 string form: the forms EndpointID.String and upstream iroh produce.

It does not accept the z-base-32 form produced by EndpointID.Z32. Both base32 flavours encode an endpoint id in 52 characters but with different alphabets, so a z-base-32 id cannot be recognized reliably — some decode under RFC 4648 to a different, equally valid-looking id. Use ParseEndpointIDZ32 for that form. When the input fails to parse but is well-formed z-base-32, the returned error says so.

func ParseEndpointIDZ32

func ParseEndpointIDZ32(s string) (EndpointID, error)

ParseEndpointIDZ32 parses an endpoint id from the z-base-32 encoding produced by EndpointID.Z32.

func UncheckedEndpointID added in v0.1.1

func UncheckedEndpointID(b [PublicKeySize]byte) EndpointID

UncheckedEndpointID skips curve-point validation; b must have passed NewEndpointID before.

func (EndpointID) Bytes

func (id EndpointID) Bytes() [PublicKeySize]byte

Bytes returns the endpoint id as a 32-byte array.

func (EndpointID) Compare

func (id EndpointID) Compare(other EndpointID) int

Compare returns -1, 0, or +1 comparing id and other by their raw bytes. It gives EndpointID a total order suitable for sorting and map-free ordered use.

func (EndpointID) Equal

func (id EndpointID) Equal(other EndpointID) bool

Equal reports whether id and other are the same endpoint id.

func (EndpointID) IsZero

func (id EndpointID) IsZero() bool

IsZero reports whether id is the unusable zero value.

func (EndpointID) MarshalBinary

func (id EndpointID) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler, producing the 32 raw bytes.

func (EndpointID) MarshalText

func (id EndpointID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler, producing the hex form.

func (EndpointID) PublicKey

func (id EndpointID) PublicKey() PublicKey

PublicKey returns id as a public key for cryptographic operations.

func (EndpointID) Short

func (id EndpointID) Short() string

Short returns a short, friendly hex string of the first 5 bytes of the endpoint id, for logging. It is not a complete or parseable representation.

func (EndpointID) String

func (id EndpointID) String() string

String returns the lowercase-hex encoding of the endpoint id. It is the canonical human-readable form and round-trips through ParseEndpointID.

func (*EndpointID) UnmarshalBinary

func (id *EndpointID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler from 32 raw bytes.

func (*EndpointID) UnmarshalText

func (id *EndpointID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler, parsing the hex or base32 form.

func (EndpointID) Z32

func (id EndpointID) Z32() string

Z32 encodes the endpoint id in z-base-32, the encoding used by pkarr domain names. It uses a different alphabet from the RFC 4648 base32 accepted by ParseEndpointID and is the same length, so the two cannot be told apart: parse the result with ParseEndpointIDZ32, not ParseEndpointID.

type PublicKey

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

PublicKey is a public Ed25519 key. It is verified to be a valid curve point when created.

The zero value is not usable; construct a PublicKey with NewPublicKey, ParsePublicKey, or SecretKey.Public.

func NewPublicKey

func NewPublicKey(b [PublicKeySize]byte) (PublicKey, error)

NewPublicKey constructs a PublicKey from a 32-byte array. It returns ErrInvalidKeyData if the bytes do not decompress to a valid Ed25519 curve point. It never fails for bytes returned from PublicKey.Bytes.

func ParsePublicKey

func ParsePublicKey(s string) (PublicKey, error)

ParsePublicKey parses a PublicKey from its hex or base32 string form. A string of exactly 64 characters is decoded as lowercase hex; otherwise it is decoded as RFC 4648 base32 (no padding, case-insensitive). PublicKey.String always produces the hex form. The z-base-32 form is not accepted; see ParseEndpointID.

func PublicKeyFromEd25519

func PublicKeyFromEd25519(k ed25519.PublicKey) (PublicKey, error)

PublicKeyFromEd25519 constructs a PublicKey from a crypto/ed25519 public key.

func PublicKeyFromSlice

func PublicKeyFromSlice(b []byte) (PublicKey, error)

PublicKeyFromSlice constructs a PublicKey from a byte slice. It returns ErrInvalidKeyLength if the slice is not 32 bytes and ErrInvalidKeyData if the bytes are not a valid curve point.

func (PublicKey) Bytes

func (k PublicKey) Bytes() [PublicKeySize]byte

Bytes returns the public key as a 32-byte array.

func (PublicKey) Compare

func (k PublicKey) Compare(other PublicKey) int

Compare returns -1, 0, or +1 comparing k and other by their raw bytes. It gives PublicKey a total order suitable for sorting and map-free ordered use.

func (PublicKey) Ed25519

func (k PublicKey) Ed25519() ed25519.PublicKey

Ed25519 returns the key as a crypto/ed25519 public key. The returned slice is a copy and may be modified by the caller.

func (PublicKey) EndpointID

func (k PublicKey) EndpointID() EndpointID

EndpointID returns the endpoint identifier for k.

func (PublicKey) Equal

func (k PublicKey) Equal(other PublicKey) bool

Equal reports whether k and other are the same key.

func (PublicKey) IsZero

func (k PublicKey) IsZero() bool

IsZero reports whether k is the unusable zero value.

func (PublicKey) MarshalBinary

func (k PublicKey) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler, producing the 32 raw bytes.

func (PublicKey) MarshalText

func (k PublicKey) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler, producing the hex form.

func (PublicKey) Short

func (k PublicKey) Short() string

Short returns a short, friendly hex string of the first 5 bytes of the key, for logging. It is not a complete or parseable representation.

func (PublicKey) String

func (k PublicKey) String() string

String returns the lowercase-hex encoding of the key. It is the canonical human-readable form and round-trips through ParsePublicKey.

func (*PublicKey) UnmarshalBinary

func (k *PublicKey) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler from 32 raw bytes.

func (*PublicKey) UnmarshalText

func (k *PublicKey) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler, parsing the hex or base32 form.

func (PublicKey) Verify

func (k PublicKey) Verify(message []byte, sig Signature) error

Verify reports whether sig is a valid signature of message by k. It returns nil on success and ErrInvalidSignature otherwise.

Verification uses crypto/ed25519 (cofactored, RFC 8032). The Rust reference uses ed25519-dalek's verify_strict (cofactorless). The two agree for every signature an honest iroh peer produces; they differ only for adversarially malleable signatures, which iroh drops anyway. This divergence is benign for iroh's drop-on-failure model (relay handshake, TLS raw-key, and pkarr packet verification all reject on failure).

type SecretKey

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

SecretKey is a secret endpoint identity key. Its public part can always be recovered.

Go has no destructors, so unlike the Rust original this type is not cleared automatically. Call SecretKey.Clear to overwrite the key material when a long-lived secret is no longer needed.

The zero value is not usable; construct with GenerateSecretKey, NewSecretKey, or ParseSecretKey.

func GenerateSecretKey

func GenerateSecretKey() (SecretKey, error)

GenerateSecretKey generates a new SecretKey using crypto/ed25519.

func NewSecretKey

func NewSecretKey(seed [SeedSize]byte) SecretKey

NewSecretKey constructs a SecretKey from its 32-byte seed.

func ParseSecretKey

func ParseSecretKey(s string) (SecretKey, error)

ParseSecretKey parses a SecretKey from its hex or base32 string form, matching the rules of ParsePublicKey.

func SecretKeyFromEd25519

func SecretKeyFromEd25519(k ed25519.PrivateKey) (SecretKey, error)

SecretKeyFromEd25519 constructs a SecretKey from a crypto/ed25519 private key. The private key is copied.

func SecretKeyFromSlice

func SecretKeyFromSlice(b []byte) (SecretKey, error)

SecretKeyFromSlice constructs a SecretKey from a 32-byte seed slice. It returns ErrInvalidKeyLength if the slice is not 32 bytes.

func (SecretKey) Bytes

func (k SecretKey) Bytes() [SeedSize]byte

Bytes returns the 32-byte seed of the secret key. The public part can be recovered from it.

func (*SecretKey) Clear

func (k *SecretKey) Clear()

Clear overwrites k's key material and resets k to the zero value. It does not clear copies already made by value or by SecretKey.Bytes, SecretKey.Ed25519, or SecretKey.MarshalBinary.

func (SecretKey) Ed25519

func (k SecretKey) Ed25519() ed25519.PrivateKey

Ed25519 returns the key as a crypto/ed25519 private key. The returned key is a copy and satisfies crypto.Signer.

func (SecretKey) IsZero

func (k SecretKey) IsZero() bool

IsZero reports whether k is the unusable zero value.

func (SecretKey) MarshalBinary

func (k SecretKey) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler, producing the 32-byte seed.

func (SecretKey) Public

func (k SecretKey) Public() PublicKey

Public returns the public key of this secret key.

func (SecretKey) Sign

func (k SecretKey) Sign(msg []byte) Signature

Sign signs msg and returns the signature.

Example
package main

import (
	"fmt"

	"github.com/tmc/go-iroh/key"
)

func main() {
	secret, err := key.GenerateSecretKey()
	if err != nil {
		panic(err)
	}
	message := []byte("hello")
	signature := secret.Sign(message)
	fmt.Println(secret.Public().Verify(message, signature) == nil)
}
Output:
true

func (*SecretKey) UnmarshalBinary

func (k *SecretKey) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler from a 32-byte seed.

type Signature

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

Signature is a signature produced by a SecretKey.

func NewSignature

func NewSignature(b [SignatureSize]byte) Signature

NewSignature constructs a Signature from its 64 raw bytes.

func SignatureFromEd25519

func SignatureFromEd25519(sig []byte) (Signature, error)

SignatureFromEd25519 constructs a Signature from a crypto/ed25519 signature.

func SignatureFromSlice

func SignatureFromSlice(b []byte) (Signature, error)

SignatureFromSlice constructs a Signature from a byte slice. It returns ErrInvalidSignatureParse if the slice is not 64 bytes.

func (Signature) Bytes

func (s Signature) Bytes() [SignatureSize]byte

Bytes returns the signature as a 64-byte array.

func (Signature) Ed25519

func (s Signature) Ed25519() []byte

Ed25519 returns the signature bytes used by crypto/ed25519. The returned slice is a copy and may be modified by the caller.

func (Signature) Equal

func (s Signature) Equal(other Signature) bool

Equal reports whether s and other are the same signature.

func (Signature) MarshalBinary

func (s Signature) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler, producing the 64 raw signature bytes.

func (Signature) MarshalText

func (s Signature) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler, producing the hex form.

func (Signature) String

func (s Signature) String() string

String returns the lowercase-hex encoding of the signature.

func (*Signature) UnmarshalBinary

func (s *Signature) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler from 64 raw bytes.

func (*Signature) UnmarshalText

func (s *Signature) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler, parsing the hex form.

Jump to

Keyboard shortcuts

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