rsa

package
v1.45.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const BITS = 2048 // 密钥长度 1024,2048

Variables

This section is empty.

Functions

func Base64DecodeString

func Base64DecodeString(encode string) (pubKey []byte, err error)

Base64DecodeString BASE64解码

func Base64EncodeString

func Base64EncodeString(pubPEM []byte) (basePubKey string)

Base64EncodeString BASE64编码

func DecodingByPrivateKey

func DecodingByPrivateKey(privateKey string, result []byte) (decodeStr []byte, err error)

DecodingByPrivateKey 私钥解密 privateKey 为 Base64 编码的 PEM 私钥;密钥格式或解密失败都会返回错误。

func DecryptByPrivateKey added in v1.33.0

func DecryptByPrivateKey(privateKeyPEM []byte, ciphertext []byte) ([]byte, error)

DecryptByPrivateKey 使用 PEM 私钥解密数据(与 EncryptByPublicKey 配套)。

func EncryptByPublicKey added in v1.33.0

func EncryptByPublicKey(publicKeyPEM []byte, plaintext []byte) ([]byte, error)

EncryptByPublicKey 使用 PEM 公钥加密数据(公钥加密,私钥解密)。 与 DecodingByPrivateKey 组成完整加解密闭环(来自 sg-mes-api 实战场景)。 使用 PKCS1v15 填充(兼容主流实现);超长数据请先分段或使用混合加密。

func ExportPrivateKeyAsPEM

func ExportPrivateKeyAsPEM(privateKey *rsa.PrivateKey) []byte

ExportPrivateKeyAsPEM 将 RSA 私钥导出为 PEM 格式

func ExportPublicKeyAsPEM

func ExportPublicKeyAsPEM(publicKey *rsa.PublicKey) []byte

ExportPublicKeyAsPEM 将 RSA 公钥导出为 PEM 格式

func GenerateRSAKey

func GenerateRSAKey() (*rsa.PrivateKey, *rsa.PublicKey)

GenerateRSAKey 生成 RSA 密钥对 生成失败时返回 nil,nil,调用方必须检查后再使用。

func GetPublicKeyFromPriKey

func GetPublicKeyFromPriKey(privKey []byte) *rsa.PublicKey

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

func LoadPublicKey(pubPEM []byte) *rsa.PublicKey

LoadPublicKey 加载 RSA 公钥;解析失败时返回 nil 并记录日志,不再 panic。 新代码建议使用 LoadPublicKeySafe 获取具体错误。

func LoadPublicKeySafe

func LoadPublicKeySafe(pubPEM []byte) (*rsa.PublicKey, error)

LoadPublicKeySafe 加载 RSA 公钥并返回解析错误。

Types

type KeyPair added in v1.33.0

type KeyPair struct {
	PrivateKeyPEM []byte // 私钥(PKCS1 PEM)
	PublicKeyPEM  []byte // 公钥(PKIX PEM)
}

KeyPair 密钥对(生成结果)。

func GenerateKeyPairPEM added in v1.33.0

func GenerateKeyPairPEM(bits int) (*KeyPair, error)

GenerateKeyPairPEM 生成 RSA 密钥对(PEM 格式,一键产出)。 bits 建议 2048/4096;返回的私钥/公钥可直接存储与分发。 对标 sg-mes-api 的 generatedKey,但纯内存生成、不落盘,更安全。

Jump to

Keyboard shortcuts

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