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 ¶
- type Ed25519KeyPair
- func (k *Ed25519KeyPair) GenKeyPair() *Ed25519KeyPair
- func (k *Ed25519KeyPair) LoadPrivateKey(f fs.File)
- func (k *Ed25519KeyPair) LoadPublicKey(f fs.File)
- func (k *Ed25519KeyPair) ParsePrivateKey() (pri ed25519.PrivateKey, err error)
- func (k *Ed25519KeyPair) ParsePublicKey() (pub ed25519.PublicKey, err error)
- func (k *Ed25519KeyPair) SetBase64Sign(sign []byte)
- func (k *Ed25519KeyPair) SetHexSign(sign []byte)
- func (k *Ed25519KeyPair) SetPrivateKey(privateKey []byte)
- func (k *Ed25519KeyPair) SetPublicKey(publicKey []byte)
- func (k *Ed25519KeyPair) SetRawSign(sign []byte)
- type InvalidPrivateKeyError
- type InvalidPublicKeyError
- type KeyFormat
- type NilPemBlockError
- type RsaKeyPair
- func (k *RsaKeyPair) GenKeyPair(size int) *RsaKeyPair
- func (k *RsaKeyPair) LoadPrivateKey(f fs.File)
- func (k *RsaKeyPair) LoadPublicKey(f fs.File)
- func (k *RsaKeyPair) ParsePrivateKey() (pri *rsa.PrivateKey, err error)
- func (k *RsaKeyPair) ParsePublicKey() (pub *rsa.PublicKey, err error)
- func (k *RsaKeyPair) SetFormat(format KeyFormat)
- func (k *RsaKeyPair) SetHash(hash crypto.Hash)
- func (k *RsaKeyPair) SetPrivateKey(privateKey []byte)
- func (k *RsaKeyPair) SetPublicKey(publicKey []byte)
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
// Error stores any error that occurred during key operations
Error error
}
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 creates and returns a new ED25519 key pair instance.
func (*Ed25519KeyPair) GenKeyPair ¶ added in v1.1.1
func (k *Ed25519KeyPair) GenKeyPair() *Ed25519KeyPair
GenKeyPair generates a new ED25519 key pair. 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)
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)
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() (pri ed25519.PrivateKey, err 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() (pub ed25519.PublicKey, err 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) SetBase64Sign ¶ added in v1.1.1
func (k *Ed25519KeyPair) SetBase64Sign(sign []byte)
SetBase64Sign sets the signature in Base64 format. This method decodes the Base64 string to raw bytes before setting the signature.
func (*Ed25519KeyPair) SetHexSign ¶ added in v1.1.1
func (k *Ed25519KeyPair) SetHexSign(sign []byte)
SetHexSign sets the signature in hexadecimal format. This method decodes the hex string to raw bytes before setting the signature.
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.
func (*Ed25519KeyPair) SetRawSign ¶ added in v1.1.1
func (k *Ed25519KeyPair) SetRawSign(sign []byte)
SetRawSign sets the signature in raw byte format. This method directly assigns the signature bytes without any decoding or conversion.
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
// Error stores any error that occurred during key operations
Error error
}
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 creates and returns a new RSA key pair instance. The default format is PKCS8 and the default hash function is SHA256.
func (*RsaKeyPair) GenKeyPair ¶
func (k *RsaKeyPair) GenKeyPair(size int) *RsaKeyPair
GenKeyPair generates a new RSA key pair 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)
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)
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() (pri *rsa.PrivateKey, err error)
ParsePrivateKey parses the private key from PEM format and returns a Go crypto/rsa.PrivateKey. 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() (pub *rsa.PublicKey, err error)
ParsePublicKey parses the public key from PEM format and returns a Go crypto/rsa.PublicKey. 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