Documentation
¶
Index ¶
- Constants
- func Base64DecodeString(encode string) (pubKey []byte, err error)
- func Base64EncodeString(pubPEM []byte) (basePubKey string)
- func DecodingByPrivateKey(privateKey string, result []byte) (decodeStr []byte, err error)
- func DecryptByPrivateKey(privateKeyPEM []byte, ciphertext []byte) ([]byte, error)
- func EncryptByPublicKey(publicKeyPEM []byte, plaintext []byte) ([]byte, error)
- func ExportPrivateKeyAsPEM(privateKey *rsa.PrivateKey) []byte
- func ExportPublicKeyAsPEM(publicKey *rsa.PublicKey) []byte
- func GenerateRSAKey() (*rsa.PrivateKey, *rsa.PublicKey)
- func GetPublicKeyFromPriKey(privKey []byte) *rsa.PublicKey
- func LoadPrivateKey(privPEM []byte) *rsa.PrivateKey
- func LoadPrivateKeySafe(privPEM []byte) (*rsa.PrivateKey, error)
- func LoadPublicKey(pubPEM []byte) *rsa.PublicKey
- func LoadPublicKeySafe(pubPEM []byte) (*rsa.PublicKey, error)
- type KeyPair
Constants ¶
const BITS = 2048 // 密钥长度 1024,2048
Variables ¶
This section is empty.
Functions ¶
func Base64DecodeString ¶
Base64DecodeString BASE64解码
func Base64EncodeString ¶
Base64EncodeString BASE64编码
func DecodingByPrivateKey ¶
DecodingByPrivateKey 私钥解密 privateKey 为 Base64 编码的 PEM 私钥;密钥格式或解密失败都会返回错误。
func DecryptByPrivateKey ¶ added in v1.33.0
DecryptByPrivateKey 使用 PEM 私钥解密数据(与 EncryptByPublicKey 配套)。
func EncryptByPublicKey ¶ added in v1.33.0
EncryptByPublicKey 使用 PEM 公钥加密数据(公钥加密,私钥解密)。 与 DecodingByPrivateKey 组成完整加解密闭环(来自 sg-mes-api 实战场景)。 使用 PKCS1v15 填充(兼容主流实现);超长数据请先分段或使用混合加密。
func ExportPrivateKeyAsPEM ¶
func ExportPrivateKeyAsPEM(privateKey *rsa.PrivateKey) []byte
ExportPrivateKeyAsPEM 将 RSA 私钥导出为 PEM 格式
func ExportPublicKeyAsPEM ¶
ExportPublicKeyAsPEM 将 RSA 公钥导出为 PEM 格式
func GenerateRSAKey ¶
func GenerateRSAKey() (*rsa.PrivateKey, *rsa.PublicKey)
GenerateRSAKey 生成 RSA 密钥对 生成失败时返回 nil,nil,调用方必须检查后再使用。
func GetPublicKeyFromPriKey ¶
GetPublicKeyFromPriKey 通过密钥获取公钥;私钥无效时返回 nil。
func LoadPrivateKey ¶
func LoadPrivateKey(privPEM []byte) *rsa.PrivateKey
LoadPrivateKey 加载 RSA 私钥;解析失败时返回 nil 并记录日志,不再 panic。 新代码建议使用 LoadPrivateKeySafe 获取具体错误。
func LoadPrivateKeySafe ¶
func LoadPrivateKeySafe(privPEM []byte) (*rsa.PrivateKey, error)
LoadPrivateKeySafe 加载 RSA 私钥并返回解析错误。
func LoadPublicKey ¶
LoadPublicKey 加载 RSA 公钥;解析失败时返回 nil 并记录日志,不再 panic。 新代码建议使用 LoadPublicKeySafe 获取具体错误。
Types ¶
type KeyPair ¶ added in v1.33.0
KeyPair 密钥对(生成结果)。
func GenerateKeyPairPEM ¶ added in v1.33.0
GenerateKeyPairPEM 生成 RSA 密钥对(PEM 格式,一键产出)。 bits 建议 2048/4096;返回的私钥/公钥可直接存储与分发。 对标 sg-mes-api 的 generatedKey,但纯内存生成、不落盘,更安全。