Documentation
¶
Overview ¶
Package keypair provides RSA key pair management functionality. It supports key generation, formatting, parsing, and manipulation for both PKCS1 and PKCS8 formats.
Index ¶
- 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) SetBase64Sign(sign []byte)
- func (k *RsaKeyPair) SetFormat(format KeyFormat)
- func (k *RsaKeyPair) SetHash(hash crypto.Hash)
- func (k *RsaKeyPair) SetHexSign(sign []byte)
- func (k *RsaKeyPair) SetPrivateKey(privateKey []byte)
- func (k *RsaKeyPair) SetPublicKey(publicKey []byte)
- func (k *RsaKeyPair) SetRawSign(sign []byte)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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) SetBase64Sign ¶
func (k *RsaKeyPair) SetBase64Sign(sign []byte)
SetBase64Sign sets the signature in Base64 format. This method decodes the Base64 string to raw bytes before setting the signature.
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) SetHexSign ¶
func (k *RsaKeyPair) SetHexSign(sign []byte)
SetHexSign sets the signature in hexadecimal format. This method decodes the hex string to raw bytes before setting the signature.
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.
func (*RsaKeyPair) SetRawSign ¶
func (k *RsaKeyPair) SetRawSign(sign []byte)
SetRawSign sets the signature in raw byte format. This method directly assigns the signature bytes without any decoding or conversion.
Source Files
¶
- errors.go
- keypair.go
- rsa.go