Documentation
¶
Overview ¶
Package grsa provides useful API for RSA encryption/decryption algorithms.
This package includes functionality for: - Generating RSA key pairs in PKCS#1 and PKCS#8 formats - Encrypting and decrypting data with various key formats - Handling Base64 encoded keys - Detecting private key types
Security Considerations ¶
This package provides two padding schemes for RSA encryption:
1. PKCS#1 v1.5 (legacy): Used by Encrypt*, DecryptPKCS1*, DecryptPKCS8* functions. This padding scheme is considered less secure and vulnerable to padding oracle attacks. It is provided for backward compatibility with existing systems.
2. OAEP (recommended): Used by EncryptOAEP*, DecryptOAEP* functions. OAEP (Optimal Asymmetric Encryption Padding) is the recommended padding scheme for new applications as it provides better security guarantees.
For new implementations, prefer using OAEP functions (EncryptOAEP, DecryptOAEP, etc.).
Index ¶
- Constants
- func Decrypt(cipherText, privateKey []byte) ([]byte, error)
- func DecryptBase64(cipherTextBase64, privateKeyBase64 string) ([]byte, error)
- func DecryptOAEP(cipherText, privateKey []byte) ([]byte, error)
- func DecryptOAEPBase64(cipherTextBase64, privateKeyBase64 string) ([]byte, error)
- func DecryptOAEPWithHash(cipherText, privateKey, label []byte, hash hash.Hash) ([]byte, error)
- func DecryptPKCS1(cipherText, privateKey []byte) ([]byte, error)
- func DecryptPKCS1Base64(cipherTextBase64, privateKeyBase64 string) ([]byte, error)
- func DecryptPKCS8(cipherText, privateKey []byte) ([]byte, error)
- func DecryptPKCS8Base64(cipherTextBase64, privateKeyBase64 string) ([]byte, error)
- func Encrypt(plainText, publicKey []byte) ([]byte, error)
- func EncryptBase64(plainText []byte, publicKeyBase64 string) (string, error)
- func EncryptOAEP(plainText, publicKey []byte) ([]byte, error)
- func EncryptOAEPBase64(plainText []byte, publicKeyBase64 string) (string, error)
- func EncryptOAEPWithHash(plainText, publicKey, label []byte, hash hash.Hash) ([]byte, error)
- func EncryptPKCS1(plainText, publicKey []byte) ([]byte, error)
- func EncryptPKCS1Base64(plainText []byte, publicKeyBase64 string) (string, error)
- func EncryptPKCS8(plainText, publicKey []byte) ([]byte, error)deprecated
- func EncryptPKCS8Base64(plainText []byte, publicKeyBase64 string) (string, error)deprecated
- func EncryptPKIX(plainText, publicKey []byte) ([]byte, error)
- func EncryptPKIXBase64(plainText []byte, publicKeyBase64 string) (string, error)
- func ExtractPKCS1PublicKey(privateKey []byte) ([]byte, error)
- func GenerateDefaultKeyPair() (privateKey, publicKey []byte, err error)
- func GenerateKeyPair(bits int) (privateKey, publicKey []byte, err error)
- func GenerateKeyPairPKCS8(bits int) (privateKey, publicKey []byte, err error)
- func GetPrivateKeyType(privateKey []byte) (string, error)
- func GetPrivateKeyTypeBase64(privateKeyBase64 string) (string, error)
Constants ¶
const ( // DefaultRSAKeyBits is the default bit size for RSA key generation DefaultRSAKeyBits = 2048 // KeyTypePKCS1 represents PKCS#1 format private key KeyTypePKCS1 = "PKCS#1" // KeyTypePKCS8 represents PKCS#8 format private key KeyTypePKCS8 = "PKCS#8" )
Variables ¶
This section is empty.
Functions ¶
func Decrypt ¶
Decrypt decrypts data with private key using PKCS#1 v1.5 padding (auto-detect format). The privateKey can be either PKCS#1 or PKCS#8 format.
Security Warning: PKCS#1 v1.5 padding is vulnerable to padding oracle attacks. For new applications, consider using DecryptOAEP instead.
func DecryptBase64 ¶
DecryptBase64 decrypts base64-encoded data with base64-encoded private key (auto-detect format).
func DecryptOAEP ¶
DecryptOAEP decrypts data with private key using OAEP padding (auto-detect format). The privateKey can be either PKCS#1 or PKCS#8 format. Uses SHA-256 as the hash function by default.
func DecryptOAEPBase64 ¶
DecryptOAEPBase64 decrypts base64-encoded data with private key using OAEP padding.
func DecryptOAEPWithHash ¶
DecryptOAEPWithHash decrypts data with private key using OAEP padding with custom hash. The privateKey can be either PKCS#1 or PKCS#8 format. The label parameter must match the label used during encryption (nil if not used). The hash parameter must match the hash function used during encryption.
func DecryptPKCS1 ¶
DecryptPKCS1 decrypts data with private key by PKCS#1 format.
func DecryptPKCS1Base64 ¶
DecryptPKCS1Base64 decrypts base64-encoded data with PKCS#1 private key.
func DecryptPKCS8 ¶
DecryptPKCS8 decrypts data with private key by PKCS#8 format.
func DecryptPKCS8Base64 ¶
DecryptPKCS8Base64 decrypts data with private key by PKCS#8 format and decode base64 input.
func Encrypt ¶
Encrypt encrypts data with public key using PKCS#1 v1.5 padding (auto-detect format). The publicKey can be either PKCS#1 or PKCS#8 (PKIX) format.
Note: RSA encryption has a size limit based on key size. For PKCS#1 v1.5 padding, max plaintext size = key_size_in_bytes - 11. For example, a 2048-bit key can encrypt at most 245 bytes.
Security Warning: PKCS#1 v1.5 padding is vulnerable to padding oracle attacks. For new applications, consider using EncryptOAEP instead.
func EncryptBase64 ¶
EncryptBase64 encrypts data with base64-encoded public key (auto-detect format) and returns base64-encoded result.
func EncryptOAEP ¶
EncryptOAEP encrypts data with public key using OAEP padding (auto-detect format). The publicKey can be either PKCS#1 or PKCS#8 (PKIX) format. Uses SHA-256 as the hash function by default.
OAEP (Optimal Asymmetric Encryption Padding) is more secure than PKCS#1 v1.5 and is recommended for new applications.
Note: For OAEP with SHA-256, max plaintext size = key_size_in_bytes - 2*32 - 2. For a 2048-bit key, this is 190 bytes.
func EncryptOAEPBase64 ¶
EncryptOAEPBase64 encrypts data with public key using OAEP padding and returns base64-encoded result.
func EncryptOAEPWithHash ¶
EncryptOAEPWithHash encrypts data with public key using OAEP padding with custom hash. The publicKey can be either PKCS#1 or PKCS#8 (PKIX) format. The label parameter can be nil for most use cases. The hash parameter specifies the hash function to use (e.g., sha256.New()).
func EncryptPKCS1 ¶
EncryptPKCS1 encrypts data with public key in PKCS#1 format.
Note: RSA encryption has a size limit based on key size. For PKCS#1 v1.5 padding, max plaintext size = key_size_in_bytes - 11.
func EncryptPKCS1Base64 ¶
EncryptPKCS1Base64 encrypts data with PKCS#1 public key and returns base64-encoded result.
func EncryptPKCS8
deprecated
func EncryptPKCS8Base64
deprecated
func EncryptPKIX ¶
EncryptPKIX encrypts data with public key in PKIX (X.509) format. PKIX is the standard format for public keys, often referred to as "PKCS#8 public key".
Note: RSA encryption has a size limit based on key size. For PKCS#1 v1.5 padding, max plaintext size = key_size_in_bytes - 11.
func EncryptPKIXBase64 ¶
EncryptPKIXBase64 encrypts data with PKIX public key and returns base64-encoded result.
func ExtractPKCS1PublicKey ¶
ExtractPKCS1PublicKey extracts PKCS#1 public key from private key.
func GenerateDefaultKeyPair ¶
GenerateDefaultKeyPair generates a new RSA key pair with default bits (2048).
func GenerateKeyPair ¶
GenerateKeyPair generates a new RSA key pair with the given bits.
func GenerateKeyPairPKCS8 ¶
GenerateKeyPairPKCS8 generates a new RSA key pair with the given bits in PKCS#8 format.
func GetPrivateKeyType ¶
GetPrivateKeyType detects the type of private key (PKCS#1 or PKCS#8). It attempts to parse the key in both formats to determine the actual type.
func GetPrivateKeyTypeBase64 ¶
GetPrivateKeyTypeBase64 detects the type of base64 encoded private key (PKCS#1 or PKCS#8).
Types ¶
This section is empty.