crypto

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeAESKey

func DecodeAESKey(encodingAESKey string) ([]byte, error)

DecodeAESKey 解码微信开放平台AES密钥(符合微信官方规范)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Technical_Plan.html" index="1">1</mcreference>

func DecryptMiniProgramData added in v0.1.0

func DecryptMiniProgramData(sessionKey, encryptedData, iv string, result interface{}) error

DecryptMiniProgramData 解密微信小程序加密数据(用户信息、手机号等)

参数:

  • sessionKey: 登录接口获取的 session_key,Base64 编码
  • encryptedData: 前端返回的 encryptedData,Base64 编码
  • iv: 前端返回的 iv,Base64 编码
  • result: 解密后 JSON 反序列化的目标结构体指针

返回:

  • error: 解密或反序列化失败时返回错误

说明:

  • 使用 AES-128-CBC(兼容 AES-192/AES-256)与 PKCS7 填充

func DecryptMsg

func DecryptMsg(encryptedMsg string, aesKey []byte) (string, error)

DecryptMsg 解密微信消息(符合微信官方规范)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Technical_Plan.html" index="1">1</mcreference>

func EncryptMsg

func EncryptMsg(plainText, appID string, aesKey []byte) (string, error)

EncryptMsg 加密微信消息(符合微信官方规范)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Technical_Plan.html" index="1">1</mcreference>

Types

type CryptoCache

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

CryptoCache 加密解密缓存

func NewCryptoCache

func NewCryptoCache() *CryptoCache

NewCryptoCache 创建新的加密解密缓存

func (*CryptoCache) Get

func (c *CryptoCache) Get(appID string) *WXBizMsgCrypt

Get 获取缓存的加密解密实例

func (*CryptoCache) Set

func (c *CryptoCache) Set(appID string, crypto *WXBizMsgCrypt)

Set 设置缓存的加密解密实例

type CryptoClient

type CryptoClient struct{}

CryptoClient 加密解密客户端

func NewCryptoClient

func NewCryptoClient() *CryptoClient

NewCryptoClient 创建新的加密解密客户端

func (*CryptoClient) DecodeAESKey

func (c *CryptoClient) DecodeAESKey(encodingAESKey string) ([]byte, error)

DecodeAESKey 解码微信开放平台AES密钥

func (*CryptoClient) DecryptMiniProgramData added in v0.1.0

func (c *CryptoClient) DecryptMiniProgramData(sessionKey, encryptedData, iv string, result interface{}) error

DecryptMiniProgramData 解密微信小程序加密数据(用户信息、手机号等)

参数:

  • sessionKey: 登录接口获取的 session_key,Base64 编码
  • encryptedData: 前端返回的 encryptedData,Base64 编码
  • iv: 前端返回的 iv,Base64 编码
  • result: 解密后 JSON 反序列化的目标结构体指针

返回:

  • error: 解密或反序列化失败时返回错误

func (*CryptoClient) DecryptMsg

func (c *CryptoClient) DecryptMsg(encryptedMsg string, aesKey []byte) (string, error)

DecryptMsg 解密微信消息

func (*CryptoClient) EncryptMsg

func (c *CryptoClient) EncryptMsg(plainText, appID string, aesKey []byte) (string, error)

EncryptMsg 加密微信消息

type WXBizMsgCrypt

type WXBizMsgCrypt struct {
	Token              string
	EncodingAESKey     string
	PrevEncodingAESKey string // 上一次的EncodingAESKey(官方要求支持)
	AppID              string
	// contains filtered or unexported fields
}

WXBizMsgCrypt 微信消息加解密实例(符合微信官方规范)

func NewWXBizMsgCrypt

func NewWXBizMsgCrypt(token, encodingAESKey, appID string) *WXBizMsgCrypt

NewWXBizMsgCrypt 创建新的微信消息加解密实例

func NewWXBizMsgCryptWithStorage

func NewWXBizMsgCryptWithStorage(token, encodingAESKey, appID string, storage storage.TokenStorage) (*WXBizMsgCrypt, error)

NewWXBizMsgCryptWithStorage 创建新的微信消息加解密实例(使用存储系统)

func (*WXBizMsgCrypt) DecryptMsg

func (c *WXBizMsgCrypt) DecryptMsg(msgSignature, timestamp, nonce, encryptedMsg string) (string, error)

DecryptMsg 解密消息(符合微信官方规范,支持使用上一次的EncodingAESKey)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Message_encryption_and_decryption.html" index="0">0</mcreference>

func (*WXBizMsgCrypt) EncryptMsg

func (c *WXBizMsgCrypt) EncryptMsg(replyMsg, timestamp, nonce string) (string, string, error)

EncryptMsg 加密消息(符合微信官方规范)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Message_encryption_and_decryption.html" index="0">0</mcreference>

func (*WXBizMsgCrypt) GenerateSignature

func (c *WXBizMsgCrypt) GenerateSignature(timestamp, nonce, encryptedMsg string) string

GenerateSignature 生成消息签名(符合微信官方规范)

func (*WXBizMsgCrypt) SetPrevEncodingAESKey

func (c *WXBizMsgCrypt) SetPrevEncodingAESKey(prevKey string) error

SetPrevEncodingAESKey 设置上一次的EncodingAESKey(符合微信官方规范)

func (*WXBizMsgCrypt) VerifySignature

func (c *WXBizMsgCrypt) VerifySignature(msgSignature, timestamp, nonce, encryptedMsg string) bool

VerifySignature 验证消息签名(符合微信官方规范)

func (*WXBizMsgCrypt) VerifyURL

func (c *WXBizMsgCrypt) VerifyURL(msgSignature, timestamp, nonce, echostr string) (string, error)

VerifyURL 验证URL(符合微信官方规范)<mcreference link="https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Message_encryption_and_decryption.html" index="0">0</mcreference>

Jump to

Keyboard shortcuts

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