go_wechat_encryption

package module
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

微信公众号开发消息回复 SDK Go语言实现

微信公众号服务器消息回复 SDK go语言实现
    这里只做加解密功能,自动回复功能请参考其他语言demo
    如需要微信自动回复功能,可联系邮件chenxue4076@163.com

Documentation

Index

Constants

View Source
const (
	WXBizMsgCryptOK                     = 0
	WXBizMsgCryptValidateSignatureError = -40001
	WXBizMsgCryptParseXmlError          = -40002
	WXBizMsgCryptComputeSignatureError  = -40003
	WXBizMsgCryptIllegalAesKey          = -40004
	WXBizMsgCryptValidateAppidError     = -40005
	WXBizMsgCryptEncryptAESError        = -40006
	WXBizMsgCryptDecryptAESError        = -40007
	WXBizMsgCryptIllegalBuffer          = -40008
	WXBizMsgCryptEncodeBase64Error      = -40009
	WXBizMsgCryptDecodeBase64Error      = -40010
	WXBizMsgCryptGenReturnXmlError      = -40011
)

*

  • error code 说明.
  • <ul>
  • <li>-40001: 签名验证错误</li>
  • <li>-40002: xml解析失败</li>
  • <li>-40003: sha加密生成签名失败</li>
  • <li>-40004: encodingAesKey 非法</li>
  • <li>-40005: appid 校验错误</li>
  • <li>-40006: aes 加密失败</li>
  • <li>-40007: aes 解密失败</li>
  • <li>-40008: 解密后得到的buffer非法</li>
  • <li>-40009: base64加密失败</li>
  • <li>-40010: base64解密失败</li>
  • <li>-40011: 生成xml失败</li>
  • </ul>

Variables

View Source
var PKCS7EncoderBlockSize = 32

Functions

func Bytes4ToInt

func Bytes4ToInt(b []byte) int

4个字节转换成整形

func GetSHA1

func GetSHA1(token, timestamp, nonce, encryptMsg string) (msg string)

*

  • 用SHA1算法生成安全签名
  • @param string $token 票据
  • @param string $timestamp 时间戳
  • @param string $nonce 随机字符串
  • @param string $encrypt 密文消息

func IntToBytes4

func IntToBytes4(n int) []byte

整形转换成字节4位

func PKCS7EncoderDecode

func PKCS7EncoderDecode(origData []byte) []byte

*

  • 对解密后的明文进行补位删除
  • @param decrypted 解密后的明文
  • @return 删除填充补位后的明文

func PKCS7EncoderEncode

func PKCS7EncoderEncode(cipherText []byte) []byte

func WXBizMsgErrorMsg

func WXBizMsgErrorMsg(errorCode int) string

func XmlParseExtract

func XmlParseExtract(xmlText []byte) (errCode int, encrypt string, toUserName string)

*

  • 提取出xml数据包中的加密消息
  • @param string $xmltext 待提取的xml字符串
  • @return string 提取出的加密消息字符串

func XmlParseGenerate

func XmlParseGenerate(encrypt, signature, timestamp, nonce string) []byte

*

  • 生成xml消息
  • @param string $encrypt 加密后的消息密文
  • @param string $signature 安全签名
  • @param string $timestamp 时间戳
  • @param string $nonce 随机字符串

Types

type MsgDecryptFormatEvent added in v1.3.4

type MsgDecryptFormatEvent struct {
	ToUserName   string  `xml:"ToUserName"`
	FromUserName string  `xml:"FromUserName"`
	CreateTime   string  `xml:"CreateTime"`
	MsgType      string  `xml:"MsgType"`
	Content      string  `xml:"Content"`   //文本消息
	MsgId        string  `xml:"MsgId"`     //文本消息
	Event        string  `xml:"Event"`     //非文本时含有
	EventKey     string  `xml:"EventKey"`  //扫二维码带此参数
	Ticket       string  `xml:"Ticket"`    //扫二维码带此参数
	Latitude     float64 `xml:"Latitude"`  //地理位置
	Longitude    float64 `xml:"Longitude"` //地理位置
	Precision    float64 `xml:"Precision"` //地理位置
}

用户向微信发送的事件

type MsgEncryptFormat

type MsgEncryptFormat struct {
	Encrypt      string `xml:"Encrypt"`
	MsgSignature string `xml:"MsgSignature"`
	TimeStamp    string `xml:"TimeStamp"`
	Nonce        string `xml:"Nonce"`
}

type MsgFormat

type MsgFormat struct {
	ToUserName string `xml:"ToUserName"`
	Encrypt    string `xml:"Encrypt"`
}

type MsgFormatArticlesItem added in v1.3.3

type MsgFormatArticlesItem struct {
	Title       string `xml:"Title"`
	Description string `xml:"Description"`
	PicUrl      string `xml:"PicUrl"`
	Url         string `xml:"Url"`
}

type MsgFormatArticlesList added in v1.3.3

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

type MsgFormatImage added in v1.3.3

type MsgFormatImage struct {
	MediaId string `xml:"MediaId"`
}

type MsgFormatMusic added in v1.3.3

type MsgFormatMusic struct {
	Title        string `xml:"Title"`
	Description  string `xml:"Description"`
	MusicUrl     string `xml:"MusicUrl"`
	HQMusicUrl   string `xml:"HQMusicUrl"`
	ThumbMediaId string `xml:"ThumbMediaId"`
}

type MsgFormatVideo added in v1.3.3

type MsgFormatVideo struct {
	MediaId     string `xml:"MediaId"`
	Title       string `xml:"Title"`
	Description string `xml:"Description"`
}

type MsgFormatVoice added in v1.3.3

type MsgFormatVoice struct {
	MediaId string `xml:"MediaId"`
}

type MsgReplyFormatArticles added in v1.3.4

type MsgReplyFormatArticles struct {
	ToUserName   string                  `xml:"ToUserName"`
	FromUserName string                  `xml:"FromUserName"`
	CreateTime   string                  `xml:"CreateTime"`
	MsgType      string                  `xml:"MsgType"`
	ArticleCount string                  `xml:"ArticleCount"`
	Articles     []MsgFormatArticlesList `xml:"Articles"`
}

type MsgReplyFormatImage added in v1.3.4

type MsgReplyFormatImage struct {
	ToUserName   string           `xml:"ToUserName"`
	FromUserName string           `xml:"FromUserName"`
	CreateTime   string           `xml:"CreateTime"`
	MsgType      string           `xml:"MsgType"`
	Image        []MsgFormatImage `xml:"Image"`
}

type MsgReplyFormatMusic added in v1.3.4

type MsgReplyFormatMusic struct {
	ToUserName   string           `xml:"ToUserName"`
	FromUserName string           `xml:"FromUserName"`
	CreateTime   string           `xml:"CreateTime"`
	MsgType      string           `xml:"MsgType"`
	Music        []MsgFormatMusic `xml:"Music"`
}

type MsgReplyFormatText added in v1.3.4

type MsgReplyFormatText struct {
	ToUserName   string `xml:"ToUserName"`
	FromUserName string `xml:"FromUserName"`
	CreateTime   string `xml:"CreateTime"`
	MsgType      string `xml:"MsgType"`
	Content      string `xml:"Content"`
}

本地服务器返回给微信服务器的消息格式

type MsgReplyFormatVideo added in v1.3.4

type MsgReplyFormatVideo struct {
	ToUserName   string           `xml:"ToUserName"`
	FromUserName string           `xml:"FromUserName"`
	CreateTime   string           `xml:"CreateTime"`
	MsgType      string           `xml:"MsgType"`
	Video        []MsgFormatVideo `xml:"Video"`
}

type MsgReplyFormatVoice added in v1.3.4

type MsgReplyFormatVoice struct {
	ToUserName   string           `xml:"ToUserName"`
	FromUserName string           `xml:"FromUserName"`
	CreateTime   string           `xml:"CreateTime"`
	MsgType      string           `xml:"MsgType"`
	Voice        []MsgFormatVoice `xml:"Voice"`
}

type Prpcrypt

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

func PrpcryptDefault

func PrpcryptDefault(key string) *Prpcrypt

func (*Prpcrypt) Decrypt

func (p *Prpcrypt) Decrypt(encrypted []byte, appId string) (result []byte, errorCode int)

func (*Prpcrypt) Encrypt

func (p *Prpcrypt) Encrypt(text, appId string) (result []byte, errorCode int)

type WXBizMsgCrypt

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

*

  • 构造函数
  • @param $token string 公众平台上,开发者设置的token
  • @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
  • @param $appId string 公众平台的appId

func Default

func Default(token, encodingAesKey, appId string) *WXBizMsgCrypt

func (*WXBizMsgCrypt) DecryptMsg

func (wx *WXBizMsgCrypt) DecryptMsg(msgSignature, timeStamp, nonce string, postData []byte) (msg []byte, errorCode int)

*

  • 检验消息的真实性,并且获取解密后的明文.
  • <ol>
  • <li>利用收到的密文生成安全签名,进行签名验证</li>
  • <li>若验证通过,则提取xml中的加密消息</li>
  • <li>对消息进行解密</li>
  • </ol> *
  • @param $msgSignature string 签名串,对应URL参数的msg_signature
  • @param $timestamp string 时间戳 对应URL参数的timestamp
  • @param $nonce string 随机串,对应URL参数的nonce
  • @param $postData string 密文,对应POST请求的数据
  • @param &$msg string 解密后的原文,当return返回0时有效 *
  • @return int 成功0,失败返回对应的错误码

func (*WXBizMsgCrypt) EncryptMsg

func (wx *WXBizMsgCrypt) EncryptMsg(replyMsg, timeStamp, nonce string) (encryptMsg []byte, errorCode int)

*

  • 将公众平台回复用户的消息加密打包.
  • <ol>
  • <li>对要发送的消息进行AES-CBC加密</li>
  • <li>生成安全签名</li>
  • <li>将消息密文和安全签名打包成xml格式</li>
  • </ol> *
  • @param $replyMsg string 公众平台待回复用户的消息,xml格式的字符串
  • @param $timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp
  • @param $nonce string 随机串,可以自己生成,也可以用URL参数的nonce
  • @param &$encryptMsg string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串,
  • 当return返回0时有效 *
  • @return int 成功0,失败返回对应的错误码

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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