Help us shape the future of Go! Take the 2026 Go Developer Survey

crypto

package
v1.9.0 Latest Latest
Warning

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

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

README

Crypto 包

Crypto 包提供加解密与 JWT 能力,支持 AES-256-GCM 对称加密、RSA-OAEP/PSS 非对称加密和签名、以及 JWT HMAC-SHA256 签名验证。

功能列表

  • AES-256-GCM 对称加密 - 提供安全的对称加密解密功能
  • RSA-OAEP/PSS 非对称加密和签名 - 支持 RSA 公钥加密、私钥解密、私钥签名、公钥验证
  • JWT HMAC-SHA256 签名验证 - 支持 JWT Token 的生成与解析验证

安装

go get github.com/tsopia/go-kit/crypto

使用示例

配置全局客户端
import "github.com/tsopia/go-kit/crypto"

// 配置全局客户端
err := crypto.Configure(&crypto.Config{
    AESKey:    "32-byte-key-for-aes-256-gcm!",
    JWTSecret: "your-jwt-secret-key",
    JWTExpiry: 24 * time.Hour,
})
if err != nil {
    log.Fatal(err)
}
AES 加密解密示例
// 加密
plaintext := []byte("secret data")
encrypted, err := crypto.EncryptAES(plaintext)
if err != nil {
    return err
}

// 解密
decrypted, err := crypto.DecryptAES(encrypted)
if err != nil {
    return err
}

// 字符串加密(自动 Base64 编码)
encryptedStr, err := crypto.EncryptAESString("secret data")
if err != nil {
    return err
}

// 字符串解密(自动 Base64 解码)
decryptedStr, err := crypto.DecryptAESString(encryptedStr)
if err != nil {
    return err
}
RSA 加密解密和签名验证示例
// 配置 RSA 密钥
crypto.Configure(&crypto.Config{
    RSAPrivateKey: []byte(privateKeyPEM),
    RSAPublicKey:  []byte(publicKeyPEM),
})

// RSA 加密
plaintext := []byte("secret data")
encrypted, err := crypto.EncryptRSA(plaintext)
if err != nil {
    return err
}

// RSA 解密
decrypted, err := crypto.DecryptRSA(encrypted)
if err != nil {
    return err
}

// RSA 签名
data := []byte("data to sign")
signature, err := crypto.SignRSA(data)
if err != nil {
    return err
}

// RSA 验证签名
err = crypto.VerifyRSA(data, signature)
if err != nil {
    return err // 签名无效
}
JWT 签名验证示例
// 配置 JWT
crypto.Configure(&crypto.Config{
    JWTSecret: "your-secret-key",
    JWTExpiry: 24 * time.Hour,
})

// 生成 JWT Token
claims := crypto.JWTClaims{
    RegisteredClaims: jwt.RegisteredClaims{
        Subject: "user123",
    },
    Custom: map[string]interface{}{
        "role": "admin",
    },
}

token, err := crypto.SignJWT(claims)
if err != nil {
    return err
}

// 使用指定算法签名(支持 HS256/HS384/HS512)
token, err := crypto.SignJWTWithAlg(claims, crypto.JWTAlgHS256)

// 解析 JWT Token
parsedClaims, err := crypto.ParseJWT(token)
if err != nil {
    return err
}

fmt.Println(parsedClaims.Subject) // "user123"
使用独立客户端
// 创建独立客户端(适用于测试或多实例场景)
client, err := crypto.NewClient(&crypto.Config{
    AESKey:    "32-byte-key-for-aes-256-gcm!",
    JWTSecret: "your-jwt-secret-key",
})
if err != nil {
    return err
}

// 使用客户端方法
encrypted, err := client.EncryptAES([]byte("secret"))
decrypted, err := client.DecryptAES(encrypted)

token, err := client.SignJWT(claims)
parsedClaims, err := client.ParseJWT(token)

API 速查表

全局函数
函数 说明
Configure(config *Config) error 配置全局默认客户端
GetClient() *Client 获取全局默认客户端
NewClient(config *Config) (*Client, error) 创建新的独立客户端
AES 加密
函数 说明
EncryptAES(plaintext []byte) ([]byte, error) 使用全局客户端加密
DecryptAES(ciphertext []byte) ([]byte, error) 使用全局客户端解密
EncryptAESString(plaintext string) (string, error) 加密并返回 Base64 字符串
DecryptAESString(ciphertext string) (string, error) 解密 Base64 字符串
EncryptAESWithKey(plaintext []byte, key string) ([]byte, error) 使用指定密钥加密
DecryptAESWithKey(ciphertext []byte, key string) ([]byte, error) 使用指定密钥解密
RSA 加密与签名
函数 说明
EncryptRSA(plaintext []byte) ([]byte, error) 使用全局客户端 RSA-OAEP 加密
DecryptRSA(ciphertext []byte) ([]byte, error) 使用全局客户端 RSA-OAEP 解密
EncryptRSAWithKey(plaintext []byte, publicKey []byte) ([]byte, error) 使用指定公钥加密
DecryptRSAWithKey(ciphertext []byte, privateKey []byte) ([]byte, error) 使用指定私钥解密
SignRSA(data []byte) ([]byte, error) 使用全局客户端 RSA-PSS 签名
VerifyRSA(data, signature []byte) error 使用全局客户端验证签名
SignRSAWithKey(data []byte, privateKey []byte) ([]byte, error) 使用指定私钥签名
VerifyRSAWithKey(data, signature []byte, publicKey []byte) error 使用指定公钥验证签名
JWT 操作
函数 说明
SignJWT(claims JWTClaims) (string, error) 使用 HS256 签名 JWT
SignJWTWithAlg(claims JWTClaims, alg string) (string, error) 使用指定算法签名 JWT
ParseJWT(token string) (*JWTClaims, error) 解析并验证 JWT

算法常量: JWTAlgHS256、JWTAlgHS384、JWTAlgHS512

客户端方法
方法 说明
(c *Client) EncryptAES(plaintext []byte) ([]byte, error) AES 加密
(c *Client) DecryptAES(ciphertext []byte) ([]byte, error) AES 解密
(c *Client) EncryptRSA(plaintext []byte) ([]byte, error) RSA-OAEP 加密
(c *Client) DecryptRSA(ciphertext []byte) ([]byte, error) RSA-OAEP 解密
(c *Client) SignRSA(data []byte) ([]byte, error) RSA-PSS 签名
(c *Client) VerifyRSA(data, signature []byte) error RSA-PSS 验证签名
(c *Client) SignJWT(claims JWTClaims) (string, error) JWT 签名(HS256)
(c *Client) SignJWTWithAlg(claims JWTClaims, alg string) (string, error) JWT 签名(指定算法)
(c *Client) ParseJWT(token string) (*JWTClaims, error) JWT 解析验证
配置结构
type Config struct {
    AESKey        string        // 32字节用于 AES-256-GCM
    RSAPrivateKey []byte        // PEM 格式私钥
    RSAPublicKey  []byte        // PEM 格式公钥
    JWTSecret     string        // HMAC 密钥
    JWTPrivateKey []byte        // RSA 私钥(用于 RS256)
    JWTPublicKey  []byte        // RSA 公钥(用于 RS256)
    JWTExpiry     time.Duration // 默认过期时间(默认 24h)
}

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

View Source
const (
	JWTAlgHS256 = "HS256"
	JWTAlgHS384 = "HS384"
	JWTAlgHS512 = "HS512"
)

JWT 算法常量

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 GetClient added in v1.3.3

func GetClient() *Client

GetClient 获取全局默认客户端

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