crypto

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: BSD-3-Clause Imports: 5 Imported by: 40

Documentation

Overview

Package crypto provides functionality for interacting with Ed25519 public and private keys. Package crypto uses the "crypto/ed25519" package from Go's standard library for the underlying cryptography.

Index

Constants

View Source
const (
	PublicKeyLen  = ed25519.PublicKeySize
	PrivateKeyLen = ed25519.PrivateKeySize
	SignatureLen  = ed25519.SignatureSize
)

Variables

View Source
var (
	EmptyPublicKey  = [ed25519.PublicKeySize]byte{}
	EmptyPrivateKey = [ed25519.PrivateKeySize]byte{}
	EmptySignature  = [ed25519.SignatureSize]byte{}
)
View Source
var (
	ErrInvalidPrivateKey = errors.New("invalid private key")
	ErrInvalidPublicKey  = errors.New("invalid public key")
	ErrIncorrectHrp      = errors.New("incorrect hrp")
	ErrInvalidSignature  = errors.New("invalid signature")
)

Functions

func Address

func Address(hrp string, p PublicKey) string

Address returns a Bech32 address from hrp and p. This function uses avalanchego's FormatBech32 function.

func Verify

func Verify(msg []byte, p PublicKey, s Signature) bool

Verify returns whether s is a valid signature of msg by p.

Types

type PrivateKey

type PrivateKey [ed25519.PrivateKeySize]byte

func GeneratePrivateKey

func GeneratePrivateKey() (PrivateKey, error)

GeneratePrivateKey returns a Ed25519 PrivateKey. It uses the crypto/ed25519 go package to generate the key.

func HexToKey

func HexToKey(key string) (PrivateKey, error)

HexToKey Converts a hexadecimal encoded key into a PrivateKey. Returns an EmptyPrivateKey and error if key is invalid.

func LoadKey

func LoadKey(filename string) (PrivateKey, error)

LoadKey returns a PrivateKey from a file filename. If there is an error reading the file, or the file contains an invalid PrivateKey, LoadKey returns an EmptyPrivateKey and an error.

func (PrivateKey) PublicKey

func (p PrivateKey) PublicKey() PublicKey

PublicKey returns a PublicKey associated with the Ed25519 PrivateKey p. The PublicKey is the last 32 bytes of p.

func (PrivateKey) Save

func (p PrivateKey) Save(filename string) error

Save writes PrivateKey to a file [filename]. If filename does not exist, it creates a new file with read/write permissions (0o600).

func (PrivateKey) ToHex

func (p PrivateKey) ToHex() string

ToHex converts a PrivateKey to a hex string.

type PublicKey

type PublicKey [ed25519.PublicKeySize]byte

func ParseAddress

func ParseAddress(hrp, saddr string) (PublicKey, error)

ParseAddress parses a Bech32 encoded address string and extracts its public key. If there is an error reading the address or the hrp value is not valid, ParseAddress returns an EmptyPublicKey and error.

type Signature

type Signature [ed25519.SignatureSize]byte

func Sign

func Sign(msg []byte, pk PrivateKey) Signature

Sign returns a valid signature for msg using pk.

Jump to

Keyboard shortcuts

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