keyinfra

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sign

func Sign(privateKey crypto.PrivateKey, hash crypto.Hash, data []byte) ([]byte, error)

Sign generates a digital signature for the given data using the provided private key and hash algorithm. It supports RSA and Ed25519 private keys. Returns the signature as a byte slice or an error if signing fails.

func Verify

func Verify(publicKey crypto.PublicKey, hash crypto.Hash, data, signature []byte) error

Verify validates the provided signature for the given data using the specified public key and hash algorithm. It supports RSA and Ed25519 public key types and returns an error if verification fails or the key type is unsupported.

Types

type EdDSAKeyStrategy

type EdDSAKeyStrategy struct{}

EdDSAKeyStrategy implements the KeyAlgorithmStrategy interface for the EdDSA algorithm using ed25519 keys.

func (*EdDSAKeyStrategy) Export

func (s *EdDSAKeyStrategy) Export(privateKey any) (string, error)

Export exports an Ed25519 private key into a PEM-encoded PKCS#8 format string. Returns the PEM-encoded private key string or an error if the input key type is invalid or marshalling fails.

func (*EdDSAKeyStrategy) Generate

func (s *EdDSAKeyStrategy) Generate(now time.Time) (*KeyPair, error)

Generate creates a new Ed25519 key pair and generates a unique key ID (kid) associated with the keys.

func (*EdDSAKeyStrategy) Import

func (s *EdDSAKeyStrategy) Import(serializedPrivateKey string) (any, any, error)

Import decodes a PEM-encoded Ed25519 private key, parses it, and returns the private key, public key, or an error.

type KeyAlgorithmStrategy

type KeyAlgorithmStrategy interface {
	Generate(now time.Time) (*KeyPair, error)
	Import(serializedPrivateKey string) (any, any, error)
	Export(privateKey any) (string, error)
}

KeyAlgorithmStrategy defines the behavior for generating, importing, and exporting cryptographic key pairs.

func GetKeyStrategy

func GetKeyStrategy(jwa string) KeyAlgorithmStrategy

GetKeyStrategy returns the appropriate KeyAlgorithmStrategy for the given JWA algorithm or panics if unsupported.

type KeyPair

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

KeyPair represents a cryptographic key pair consisting of a public key, private key, key ID, and creation timestamp.

func (*KeyPair) CreatedAt

func (k *KeyPair) CreatedAt() time.Time

CreatedAt returns the timestamp indicating when the KeyPair was created.

func (*KeyPair) Kid

func (k *KeyPair) Kid() string

Kid returns the key identifier (KID) associated with the KeyPair.

func (*KeyPair) PrivateKey

func (k *KeyPair) PrivateKey() crypto.PrivateKey

PrivateKey returns the private key of the KeyPair.

func (*KeyPair) PublicKey

func (k *KeyPair) PublicKey() crypto.PublicKey

PublicKey returns the public key associated with the KeyPair.

type RSAKeyStrategy

type RSAKeyStrategy struct{}

RSAKeyStrategy implements the KeyAlgorithmStrategy interface for generating, importing, and exporting RSA key pairs.

func (*RSAKeyStrategy) Export

func (s *RSAKeyStrategy) Export(privateKey any) (string, error)

Export converts an RSA private key to its PEM-encoded string representation. Returns the PEM-encoded private key or an error if the input is not a valid *rsa.PrivateKey.

func (*RSAKeyStrategy) Generate

func (s *RSAKeyStrategy) Generate(now time.Time) (*KeyPair, error)

Generate creates a new RSA key pair with a 4096-bit private key and computes a key identifier (KID) based on the public key.

func (*RSAKeyStrategy) Import

func (s *RSAKeyStrategy) Import(serializedPrivateKey string) (any, any, error)

Import parses the serialized private key and returns the decoded RSA private key, its public key, and any error encountered.

Jump to

Keyboard shortcuts

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