Documentation
¶
Index ¶
- func DecodeAESKey(encodingAESKey string) ([]byte, error)
- func DecryptMiniProgramData(sessionKey, encryptedData, iv string, result interface{}) error
- func DecryptMsg(encryptedMsg string, aesKey []byte) (string, error)
- func EncryptMsg(plainText, appID string, aesKey []byte) (string, error)
- type CryptoCache
- type CryptoClient
- func (c *CryptoClient) DecodeAESKey(encodingAESKey string) ([]byte, error)
- func (c *CryptoClient) DecryptMiniProgramData(sessionKey, encryptedData, iv string, result interface{}) error
- func (c *CryptoClient) DecryptMsg(encryptedMsg string, aesKey []byte) (string, error)
- func (c *CryptoClient) EncryptMsg(plainText, appID string, aesKey []byte) (string, error)
- type WXBizMsgCrypt
- func (c *WXBizMsgCrypt) DecryptMsg(msgSignature, timestamp, nonce, encryptedMsg string) (string, error)
- func (c *WXBizMsgCrypt) DecryptMsgWithKey(msgSignature, timestamp, nonce, encryptedMsg string) (plainText string, usedAesKey string, err error)
- func (c *WXBizMsgCrypt) EncryptMsg(replyMsg, timestamp, nonce string) (string, string, error)
- func (c *WXBizMsgCrypt) EncryptMsgWithKey(replyMsg, timestamp, nonce, encodingAESKey string) (string, string, error)
- func (c *WXBizMsgCrypt) GenerateSignature(timestamp, nonce, encryptedMsg string) string
- func (c *WXBizMsgCrypt) SetPrevEncodingAESKey(prevKey string) error
- func (c *WXBizMsgCrypt) VerifySignature(msgSignature, timestamp, nonce, encryptedMsg string) bool
- func (c *WXBizMsgCrypt) VerifyURL(msgSignature, timestamp, nonce, echostr string) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeAESKey ¶
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
DecryptMiniProgramData 解密微信小程序加密数据(用户信息、手机号等)
参数:
- sessionKey: 登录接口获取的 session_key,Base64 编码
- encryptedData: 前端返回的 encryptedData,Base64 编码
- iv: 前端返回的 iv,Base64 编码
- result: 解密后 JSON 反序列化的目标结构体指针
返回:
- error: 解密或反序列化失败时返回错误
说明:
- 小程序 session_key 为 16 字节,即 AES-128-CBC + PKCS7 填充(密钥长度固定校验为 16 字节)
func DecryptMsg ¶
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 ¶
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 (*CryptoCache) Set ¶
func (c *CryptoCache) Set(appID string, crypto *WXBizMsgCrypt)
Set 设置缓存的加密解密实例
type CryptoClient ¶
type CryptoClient struct{}
CryptoClient 加密解密客户端
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>
兼容方法:不关心「实际解密成功所用密钥」时使用。 密钥轮换过渡期需要按官方要求「用哪个 AESKey 解密成功,就用此 AESKey 加密回包」时,请改用 DecryptMsgWithKey。
func (*WXBizMsgCrypt) DecryptMsgWithKey ¶ added in v0.2.0
func (c *WXBizMsgCrypt) DecryptMsgWithKey(msgSignature, timestamp, nonce, encryptedMsg string) (plainText string, usedAesKey string, err error)
DecryptMsgWithKey 解密消息并返回实际解密成功所用的 EncodingAESKey(符合微信官方规范)
首先验证消息签名;使用当前密钥解密失败时,降级尝试上一次的密钥(密钥轮换过渡期)。
参数:
- msgSignature: 消息签名 msg_signature
- timestamp: 时间戳
- nonce: 随机数
- encryptedMsg: 密文消息
返回:
- plainText: 解密后的明文
- usedAesKey: 实际解密成功所用的 EncodingAESKey(当前密钥成功时返回当前密钥,降级成功时返回上一次的密钥)
- error: 验签或解密失败时返回错误
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) EncryptMsgWithKey ¶ added in v0.2.0
func (c *WXBizMsgCrypt) EncryptMsgWithKey(replyMsg, timestamp, nonce, encodingAESKey string) (string, string, error)
EncryptMsgWithKey 用指定的 EncodingAESKey 加密回包(密钥轮换过渡期使用)
官方要求:用哪个 AESKey 解密成功,就用此 AESKey 加密回包。 过渡期收到用旧密钥加密的消息时,应将 DecryptMsgWithKey 返回的 usedAesKey 传入本方法回包。
参数:
- replyMsg: 回复消息明文
- timestamp: 时间戳
- nonce: 随机数
- encodingAESKey: 用于加密的 EncodingAESKey(通常为 DecryptMsgWithKey 返回的 usedAesKey)
返回:
- encrypted: 加密后的密文
- signature: 消息签名(签名使用当前配置的 Token,Token 不随密钥轮换变化)
- error: 密钥解码或加密失败时返回错误
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>