util

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AesKeyBits128 = 128
	AesKeyBits192 = 192
	AesKeyBits256 = 256
)

Variables

This section is empty.

Functions

func BuildURL

func BuildURL(baseURL string, obj interface{}) (string, error)

BuildURL 构建 URL,将对象参数附加到基础 URL 后 例如: BuildURL("http://example.com/api", obj) => "http://example.com/api?name=test&age=18"

func DecryptAESGCM

func DecryptAESGCM(key, ciphertext string) (string, error)

DecryptAESGCM 解密 EncryptAESGCM 的产物。 密文或 nonce 被篡改时返回 error(认证失败),故无需额外 MAC。

func DetachContext

func DetachContext(parent context.Context, opts ...DetachOption) (context.Context, context.CancelFunc)

DetachContext 复制父 ctx 的全部 Value 链,但切断其取消信号与 deadline, 用于异步 / 延时任务:请求响应后原 ctx 会被取消,但后台协程仍需保持之前的会话内容。 实现基于 Go 1.21+ 的 context.WithoutCancel。

始终返回 (ctx, cancel):cancel 永远非 nil 且可安全调用—— 使用了 WithTimeout / WithCancel 时是真实句柄,纯脱离时为 no-op。 这样调用方可以无条件 `defer cancel()` 释放资源。

用法:

ctx, _ := util.DetachContext(reqCtx)                                        // 纯脱离(fire-and-forget)
ctx, cancel := util.DetachContext(reqCtx, util.WithTimeout(10*time.Second)) // 脱离 + 独立超时
ctx, cancel := util.DetachContext(reqCtx, util.WithCancel())                // 脱离 + 手动取消
ctx, cancel := util.DetachContext(reqCtx, util.WithTimeout(10*time.Second), util.WithCancel())

func DoRequest

func DoRequest(meta *Meta, param map[string]interface{}, body interface{}, callback func(bytes []byte) error) error

func DoRequestWithContext added in v0.2.0

func DoRequestWithContext(ctx context.Context, client *http.Client, meta *Meta, param map[string]interface{}, body interface{}, callback func(bytes []byte) error) error

func EccDecrypt added in v0.2.0

func EccDecrypt(privateKey, ciphertext string) (string, error)

EccDecrypt 解密 EccEncrypt 的产物。

func EccEncrypt added in v0.2.0

func EccEncrypt(publicKey, data string) (string, error)

EccEncrypt 使用 X25519 ECIES 风格混合加密。 返回 base64(version || ephemeralPublicDERLen || ephemeralPublicDER || nonce || ciphertext || tag)。

func EncryptAESGCM

func EncryptAESGCM(key, plaintext string) (string, error)

EncryptAESGCM 用 AES-GCM 加密(AEAD,自带认证标签,推荐主路径)。 key 为 base64 编码的 AES 密钥(16/24/32 字节原文)。 返回 base64(nonce || ciphertext || tag),调用方无需单独管理 nonce。

func ForwardContext

func ForwardContext(ctx context.Context) context.Context

ForwardContext 按统一传播策略将安全的入站 gRPC metadata 写入出站 context。 local、未声明和敏感字段不会跨进程传播;没有 metadata 时原样返回。 Deprecated: transport 默认策略会自动完成传播,新代码不应手动调用。

func GenerateAESKey

func GenerateAESKey(bits int) (string, error)

GenerateAESKey 生成 AES 随机密钥(base64 编码)。 bits 仅允许 128 / 192 / 256(对应密钥字节长度 16 / 24 / 32)。

func GenerateEccKey added in v0.2.0

func GenerateEccKey() (prv, pub string, err error)

GenerateEccKey 生成 X25519 公私钥对字符串。 私钥使用 PKCS#8 编码,公钥使用 PKIX 编码。

func GenerateRsaKey

func GenerateRsaKey(bits int) (prv, pub string, err error)

GenerateRsaKey 生成公私钥秘钥对字符串。 私钥使用 PKCS#8 编码(算法无关,更通用),公钥使用 PKIX 编码。

func RandomCode

func RandomCode(length int) (string, error)

func RandomHex

func RandomHex(length int) (string, error)

func RandomNumber

func RandomNumber(length int) (string, error)

func RsaDecrypt

func RsaDecrypt(privateKey, ciphertext string) (string, error)

RsaDecrypt 私钥解密(OAEP + SHA-256)。

func RsaEncrypt

func RsaEncrypt(publicKey, data string) (string, error)

RsaEncrypt 公钥加密(OAEP + SHA-256)。 使用 OAEP 填充以避免 PKCS1v15 的 Bleichenbacher padding-oracle 攻击。

func RsaSignature

func RsaSignature(privateKey, data string) (string, error)

RsaSignature 私钥签名(PSS + SHA-256)。 PSS 是随机化签名,安全性优于确定性的 PKCS1v15。

func RsaVerification

func RsaVerification(publicKey, signature, data string) error

RsaVerification 公钥验签(PSS + SHA-256)。

func ToURLParams

func ToURLParams(obj interface{}) (string, error)

ToURLParams 将对象转换为 URL 查询参数字符串 支持 proto.Message 和普通 struct 例如: {Name: "test", Age: 18} => "name=test&age=18" 只支持一层结构,不支持嵌套

Types

type DetachOption

type DetachOption func(*detachConfig)

DetachOption 配置 DetachContext 的行为。

func WithCancel

func WithCancel() DetachOption

WithCancel 表示需要一个可主动调用的 cancel 句柄(使用即开启)。 不带超时时,脱离后的 ctx 仅在调用返回的 cancel 时取消; 与 WithTimeout 组合时等价于超时本身提供的 cancel(两者返回的是同一个句柄)。

func WithTimeout

func WithTimeout(d time.Duration) DetachOption

WithTimeout 在脱离父 ctx 之后,给新 ctx 设置一个独立的全新超时。 适合延时任务,避免脱离后协程因无 deadline 而无限期挂起。

type Meta

type Meta struct {
	Scheme     string // https, http
	Method     string // POST, GET, PUT, DELETE, PATCH ...
	Host       string // 域名
	Path       string // 路径URI
	ResultType string // 响应结果序列化方案
}

Jump to

Keyboard shortcuts

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