keypair

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package keypair provides cryptographic key pair management functionality. It supports key generation, formatting, parsing, and manipulation for various cryptographic algorithms with different key formats.

Package keypair provides RSA key pair management functionality. It supports key generation, formatting, parsing, and manipulation for both PKCS1 and PKCS8 formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ed25519KeyPair added in v1.1.1

type Ed25519KeyPair struct {
	// PublicKey contains the PEM-encoded public key
	PublicKey []byte

	// PrivateKey contains the PEM-encoded private key
	PrivateKey []byte

	// Sign contains the signature bytes for verification
	Sign []byte
}

Ed25519KeyPair represents an ED25519 key pair with public and private keys. It supports PKCS8 format and provides methods for key generation, formatting, and parsing.

func NewEd25519KeyPair added in v1.1.1

func NewEd25519KeyPair() *Ed25519KeyPair

NewEd25519KeyPair returns a new Ed25519KeyPair instance.

func (*Ed25519KeyPair) GenKeyPair added in v1.1.1

func (k *Ed25519KeyPair) GenKeyPair()

GenKeyPair generates a new Ed25519KeyPair instance. The generated keys are formatted in PEM format using PKCS8 format.

Note: The generated keys are automatically formatted in PEM format using PKCS8 format.

func (*Ed25519KeyPair) LoadPrivateKey added in v1.1.1

func (k *Ed25519KeyPair) LoadPrivateKey(f fs.File) error

LoadPrivateKey loads a private key from a file. The file should contain a PEM-encoded private key. This method reads the entire file content and sets it as the private key.

Note: The file format is automatically detected from the PEM headers. Only PKCS8 format is supported for ED25519.

func (*Ed25519KeyPair) LoadPublicKey added in v1.1.1

func (k *Ed25519KeyPair) LoadPublicKey(f fs.File) error

LoadPublicKey loads a public key from a file. The file should contain a PEM-encoded public key. This method reads the entire file content and sets it as the public key.

Note: The file format is automatically detected from the PEM headers. Only PKCS8 format is supported for ED25519.

func (*Ed25519KeyPair) ParsePrivateKey added in v1.1.1

func (k *Ed25519KeyPair) ParsePrivateKey() (ed25519.PrivateKey, error)

ParsePrivateKey parses the private key from PEM format and returns a Go crypto/ed25519.PrivateKey. It supports PKCS8 format.

Note: This method automatically detects the key format from the PEM headers.

func (*Ed25519KeyPair) ParsePublicKey added in v1.1.1

func (k *Ed25519KeyPair) ParsePublicKey() (ed25519.PublicKey, error)

ParsePublicKey parses the public key from PEM format and returns a Go crypto/ed25519.PublicKey. It supports PKCS8 format.

Note: This method automatically detects the key format from the PEM headers.

func (*Ed25519KeyPair) SetPrivateKey added in v1.1.1

func (k *Ed25519KeyPair) SetPrivateKey(privateKey []byte)

SetPrivateKey sets the private key and formats it in PKCS8 format. The input key is expected to be in PEM format and will be reformatted if necessary.

func (*Ed25519KeyPair) SetPublicKey added in v1.1.1

func (k *Ed25519KeyPair) SetPublicKey(publicKey []byte)

SetPublicKey sets the public key and formats it in PKCS8 format. The input key is expected to be in PEM format and will be reformatted if necessary.

type InvalidPrivateKeyError

type InvalidPrivateKeyError struct {
	Err error
}

func (InvalidPrivateKeyError) Error

func (e InvalidPrivateKeyError) Error() string

type InvalidPublicKeyError

type InvalidPublicKeyError struct {
	Err error
}

func (InvalidPublicKeyError) Error

func (e InvalidPublicKeyError) Error() string

type KeyFormat

type KeyFormat string

KeyFormat represents the format of RSA keys. It can be either PKCS1 or PKCS8 format.

const (
	// PKCS1 represents the PKCS#1 format for RSA keys.
	// This format uses specific headers like "-----BEGIN RSA PUBLIC KEY-----".
	PKCS1 KeyFormat = "pkcs1"

	// PKCS8 represents the PKCS#8 format for RSA keys.
	// This format uses generic headers like "-----BEGIN PUBLIC KEY-----".
	PKCS8 KeyFormat = "pkcs8"
)

Key format constants for RSA key pairs.

type NilPemBlockError

type NilPemBlockError struct {
}

func (NilPemBlockError) Error

func (e NilPemBlockError) Error() string

type RsaKeyPair

type RsaKeyPair struct {
	// PublicKey contains the PEM-encoded public key
	PublicKey []byte

	// PrivateKey contains the PEM-encoded private key
	PrivateKey []byte

	// Sign contains the signature bytes for verification
	Sign []byte

	// Format specifies the key format (PKCS1 or PKCS8)
	Format KeyFormat

	// Hash specifies the hash function used for RSA operations
	// This is used for OAEP padding in encryption and for signature generation/verification
	Hash crypto.Hash
}

RsaKeyPair represents an RSA key pair with public and private keys. It supports both PKCS1 and PKCS8 formats and provides methods for key generation, formatting, and parsing.

func NewRsaKeyPair

func NewRsaKeyPair() *RsaKeyPair

NewRsaKeyPair returns a new RsaKeyPair instance. The default format is PKCS8 and the default hash function is SHA256.

func (*RsaKeyPair) GenKeyPair

func (k *RsaKeyPair) GenKeyPair(size int) error

GenKeyPair generates a new RsaKeyPair with the specified key size. The generated keys are formatted according to the current Format setting.

Note: The generated keys are automatically formatted in PEM format according to the current Format setting (PKCS1 or PKCS8).

func (*RsaKeyPair) LoadPrivateKey

func (k *RsaKeyPair) LoadPrivateKey(f fs.File) error

LoadPrivateKey loads a private key from a file. The file should contain a PEM-encoded private key. This method reads the entire file content and sets it as the private key.

Note: The file format is automatically detected from the PEM headers. Both PKCS1 and PKCS8 formats are supported.

func (*RsaKeyPair) LoadPublicKey

func (k *RsaKeyPair) LoadPublicKey(f fs.File) error

LoadPublicKey loads a public key from a file. The file should contain a PEM-encoded public key. This method reads the entire file content and sets it as the public key.

Note: The file format is automatically detected from the PEM headers. Both PKCS1 and PKCS8 formats are supported.

func (*RsaKeyPair) ParsePrivateKey

func (k *RsaKeyPair) ParsePrivateKey() (*rsa.PrivateKey, error)

ParsePrivateKey parses the private key from PEM format. It supports both PKCS1 and PKCS8 formats automatically.

Note: This method automatically detects the key format from the PEM headers.

func (*RsaKeyPair) ParsePublicKey

func (k *RsaKeyPair) ParsePublicKey() (*rsa.PublicKey, error)

ParsePublicKey parses the public key from PEM format. It supports both PKCS1 and PKCS8 formats automatically.

Note: This method automatically detects the key format from the PEM headers.

func (*RsaKeyPair) SetFormat

func (k *RsaKeyPair) SetFormat(format KeyFormat)

SetFormat sets the key format for the RSA key pair. This affects how keys are generated, formatted, and parsed. The format can be either PKCS1 or PKCS8.

func (*RsaKeyPair) SetHash

func (k *RsaKeyPair) SetHash(hash crypto.Hash)

SetHash sets the hash function used for OAEP padding in RSA operations. This is particularly important for PKCS8 format keys that use OAEP padding.

func (*RsaKeyPair) SetPrivateKey

func (k *RsaKeyPair) SetPrivateKey(privateKey []byte)

SetPrivateKey sets the private key and formats it according to the current format. The input key is expected to be in PEM format and will be reformatted if necessary.

func (*RsaKeyPair) SetPublicKey

func (k *RsaKeyPair) SetPublicKey(publicKey []byte)

SetPublicKey sets the public key and formats it according to the current format. The input key is expected to be in PEM format and will be reformatted if necessary.

Source Files

  • ed25519.go
  • errors.go
  • keypair.go
  • rsa.go

Jump to

Keyboard shortcuts

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