crypto

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package crypto 提供加解密与 JWT 能力

使用示例:

// 配置全局客户端
crypto.Configure(&crypto.Config{
    AESKey: "32-byte-key-for-aes-256-gcm!",
})

// AES 加密
encrypted, err := crypto.EncryptAES([]byte("secret"))
if err != nil {
    return err
}

// AES 解密
decrypted, err := crypto.DecryptAES(encrypted)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingClient 客户端未配置
	ErrMissingClient = errors.New("crypto: client not configured, call Configure first")

	// ErrInvalidKey 无效的密钥
	ErrInvalidKey = errors.New("crypto: invalid key")

	// ErrInvalidCiphertext 无效的密文
	ErrInvalidCiphertext = errors.New("crypto: invalid ciphertext")

	// ErrInvalidSignature 签名验证失败
	ErrInvalidSignature = errors.New("crypto: signature verification failed")

	// ErrInvalidToken 无效的 JWT token
	ErrInvalidToken = errors.New("crypto: invalid JWT token")

	// ErrTokenExpired JWT token 已过期
	ErrTokenExpired = errors.New("crypto: JWT token expired")

	// ErrUnsupportedAlg 不支持的算法
	ErrUnsupportedAlg = errors.New("crypto: unsupported algorithm")
)

Functions

func Configure

func Configure(config *Config) error

Configure 配置默认客户端

func DecryptAES

func DecryptAES(ciphertext []byte) ([]byte, error)

func DecryptAESString

func DecryptAESString(ciphertext string) (string, error)

func DecryptAESWithKey

func DecryptAESWithKey(ciphertext []byte, key string) ([]byte, error)

func DecryptRSA

func DecryptRSA(ciphertext []byte) ([]byte, error)

func DecryptRSAWithKey

func DecryptRSAWithKey(ciphertext []byte, privateKey []byte) ([]byte, error)

func EncryptAES

func EncryptAES(plaintext []byte) ([]byte, error)

func EncryptAESString

func EncryptAESString(plaintext string) (string, error)

func EncryptAESWithKey

func EncryptAESWithKey(plaintext []byte, key string) ([]byte, error)

func EncryptRSA

func EncryptRSA(plaintext []byte) ([]byte, error)

func EncryptRSAWithKey

func EncryptRSAWithKey(plaintext []byte, publicKey []byte) ([]byte, error)

func SignJWT

func SignJWT(claims JWTClaims) (string, error)

SignJWT 使用默认客户端和 HS256 算法签名 JWT

func SignJWTWithAlg

func SignJWTWithAlg(claims JWTClaims, alg string) (string, error)

SignJWTWithAlg 使用指定算法签名 JWT

func SignRSA

func SignRSA(data []byte) ([]byte, error)

func SignRSAWithKey

func SignRSAWithKey(data []byte, privateKey []byte) ([]byte, error)

func VerifyRSA

func VerifyRSA(data, signature []byte) error

func VerifyRSAWithKey

func VerifyRSAWithKey(data, signature []byte, publicKey []byte) error

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 加密客户端

func NewClient

func NewClient(config *Config) (*Client, error)

NewClient 创建新的加密客户端(用于测试)

func (*Client) DecryptAES

func (c *Client) DecryptAES(ciphertext []byte) ([]byte, error)

DecryptAES 使用默认客户端解密

func (*Client) DecryptRSA

func (c *Client) DecryptRSA(ciphertext []byte) ([]byte, error)

DecryptRSA 使用 RSA-OAEP 解密

func (*Client) EncryptAES

func (c *Client) EncryptAES(plaintext []byte) ([]byte, error)

EncryptAES 使用默认客户端加密

func (*Client) EncryptRSA

func (c *Client) EncryptRSA(plaintext []byte) ([]byte, error)

EncryptRSA 使用 RSA-OAEP 加密

func (*Client) ParseJWT

func (c *Client) ParseJWT(token string) (*JWTClaims, error)

ParseJWT 解析并验证 JWT

func (*Client) SignJWT

func (c *Client) SignJWT(claims JWTClaims) (string, error)

SignJWT 使用 HS256 算法签名 JWT

func (*Client) SignJWTWithAlg

func (c *Client) SignJWTWithAlg(claims JWTClaims, alg string) (string, error)

SignJWTWithAlg 使用指定算法签名 JWT(支持 HS256/HS384/HS512)

func (*Client) SignRSA

func (c *Client) SignRSA(data []byte) ([]byte, error)

SignRSA 使用 RSA-PSS 签名

func (*Client) VerifyRSA

func (c *Client) VerifyRSA(data, signature []byte) error

VerifyRSA 使用 RSA-PSS 验证签名

type Config

type Config struct {
	// AES 配置
	AESKey string // 32字节用于 AES-256-GCM

	// RSA 配置
	RSAPrivateKey []byte // PEM 格式私钥
	RSAPublicKey  []byte // PEM 格式公钥

	// JWT 配置
	JWTSecret     string        // HMAC 密钥
	JWTPrivateKey []byte        // RSA 私钥(用于 RS256)
	JWTPublicKey  []byte        // RSA 公钥(用于 RS256)
	JWTExpiry     time.Duration // 默认过期时间(默认 24h)
}

Config 加密配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置

type JWTClaims

type JWTClaims struct {
	jwt.RegisteredClaims
	Custom map[string]interface{} `json:"custom,omitempty"`
}

JWTClaims JWT 声明

func ParseJWT

func ParseJWT(token string) (*JWTClaims, error)

ParseJWT 使用默认客户端解析 JWT

Jump to

Keyboard shortcuts

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