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.
Index ¶
- Constants
- Variables
- type EndpointID
- func (id EndpointID) Bytes() [PublicKeySize]byte
- func (id EndpointID) Compare(other EndpointID) int
- func (id EndpointID) Equal(other EndpointID) bool
- func (id EndpointID) IsZero() bool
- func (id EndpointID) MarshalBinary() ([]byte, error)
- func (id EndpointID) MarshalText() ([]byte, error)
- func (id EndpointID) PublicKey() PublicKey
- func (id EndpointID) Short() string
- func (id EndpointID) String() string
- func (id *EndpointID) UnmarshalBinary(data []byte) error
- func (id *EndpointID) UnmarshalText(text []byte) error
- func (id EndpointID) Z32() string
- type PublicKey
- func (k PublicKey) Bytes() [PublicKeySize]byte
- func (k PublicKey) Compare(other PublicKey) int
- func (k PublicKey) Ed25519() ed25519.PublicKey
- func (k PublicKey) EndpointID() EndpointID
- func (k PublicKey) Equal(other PublicKey) bool
- func (k PublicKey) IsZero() bool
- func (k PublicKey) MarshalBinary() ([]byte, error)
- func (k PublicKey) MarshalText() ([]byte, error)
- func (k PublicKey) Short() string
- func (k PublicKey) String() string
- func (k *PublicKey) UnmarshalBinary(data []byte) error
- func (k *PublicKey) UnmarshalText(text []byte) error
- func (k PublicKey) Verify(message []byte, sig Signature) error
- type SecretKey
- func (k SecretKey) Bytes() [SeedSize]byte
- func (k *SecretKey) Clear()
- func (k SecretKey) Ed25519() ed25519.PrivateKey
- func (k SecretKey) IsZero() bool
- func (k SecretKey) MarshalBinary() ([]byte, error)
- func (k SecretKey) Public() PublicKey
- func (k SecretKey) Sign(msg []byte) Signature
- func (k *SecretKey) UnmarshalBinary(data []byte) error
- type Signature
- func (s Signature) Bytes() [SignatureSize]byte
- func (s Signature) Ed25519() []byte
- func (s Signature) Equal(other Signature) bool
- func (s Signature) MarshalBinary() ([]byte, error)
- func (s Signature) MarshalText() ([]byte, error)
- func (s Signature) String() string
- func (s *Signature) UnmarshalBinary(data []byte) error
- func (s *Signature) UnmarshalText(text []byte) error
Constants ¶
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 ¶
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.
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 base32 string form.
func ParseEndpointIDZ32 ¶
func ParseEndpointIDZ32(s string) (EndpointID, error)
ParseEndpointIDZ32 parses an endpoint id from its z-base-32 encoding.
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.
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 ¶
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.
func PublicKeyFromEd25519 ¶
PublicKeyFromEd25519 constructs a PublicKey from a crypto/ed25519 public key.
func PublicKeyFromSlice ¶
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 ¶
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 ¶
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) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler, producing the 32 raw bytes.
func (PublicKey) MarshalText ¶
MarshalText implements encoding.TextMarshaler, producing the hex form.
func (PublicKey) Short ¶
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 ¶
String returns the lowercase-hex encoding of the key. It is the canonical human-readable form and round-trips through ParsePublicKey.
func (*PublicKey) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler from 32 raw bytes.
func (*PublicKey) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler, parsing the hex or base32 form.
func (PublicKey) Verify ¶
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 ¶
GenerateSecretKey generates a new SecretKey using crypto/ed25519.
func NewSecretKey ¶
NewSecretKey constructs a SecretKey from its 32-byte seed.
func ParseSecretKey ¶
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 ¶
SecretKeyFromSlice constructs a SecretKey from a 32-byte seed slice. It returns ErrInvalidKeyLength if the slice is not 32 bytes.
func (SecretKey) Bytes ¶
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) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler, producing the 32-byte seed.
func (*SecretKey) UnmarshalBinary ¶
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 ¶
SignatureFromEd25519 constructs a Signature from a crypto/ed25519 signature.
func SignatureFromSlice ¶
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 ¶
Ed25519 returns the signature bytes used by crypto/ed25519. The returned slice is a copy and may be modified by the caller.
func (Signature) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler, producing the 64 raw signature bytes.
func (Signature) MarshalText ¶
MarshalText implements encoding.TextMarshaler, producing the hex form.
func (*Signature) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler from 64 raw bytes.
func (*Signature) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler, parsing the hex form.