WXBizMsgCrypt

package module
v0.0.0-...-b5a6c8e Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2018 License: MIT Imports: 13 Imported by: 1

README

WXBizMsgCrypt

本包是 Golang 版本的weixin公众平台发送给公众账号的消息加解密的包.

Features

对消息进行解密以及加密.

Installation and Testing

Install the Go package:

go get -u -v github.com/conetse/WXBizMsgCrypt

or, you can install it as follow:

cd $GOPATH/src/github.com/conetse/
git clone https://github.com/conetse/WXBizMsgCrypt.git

Run the Tests

cd $GOPATH/src/github.com/conetse/WXBizMsgCrypt
go test

Example and Usage

package main

import (
    "fmt"
    "github.com/conetse/WXBizMsgCrypt"
)

func testWXBizMsgCrypt() {
    encodingAESKey := "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"
    token := "spamtest"
    appid := "wx2c2769f8efd9abc2"
    timestamp := int64(1409735669)

    to_xml := ` <xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType>  <![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo中文]]></Title><Descript  ion><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>`
    nonce := "1320562132"
    // 测试加密接口
    cryp_test, err := WXBizMsgCrypt.NewWXBizMsgCrypt(token, encodingAESKey, appid)
    if err != nil {
        return
    }
    ret, encrypt_xml := cryp_test.EncryptMsg(to_xml, nonce, timestamp)
    fmt.Println(ret)
    fmt.Println(encrypt_xml)

    // 测试解密接口
    msg_sign := "5d197aaffba7e9b25a30732f161a50dee96bd5fa"
    from_xml := `<xml><ToUserName><![CDATA[gh_10f6c3c3ac5a]]></ToUserName><FromUserName><![CDATA[oyORnuP8q7ou2gfYjqLzSIWZf0rs]]></FromUserName><CreateTime>1409735668</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[abcdteT]]></Content><MsgId>6054768590064713728</MsgId><Encrypt><![CDATA[hyzAe4OzmOMbd6TvGdIOO6uBmdJoD0Fk53REIHvxYtJlE2B655HuD0m8KUePWB3+LrPXo87wzQ1QLvbeUgmBM4x6F8PGHQHFVAFmOD2LdJF9FrXpbUAh0B5GIItb52sn896wVsMSHGuPE328HnRGBcrS7C41IzDWyWNlZkyyXwon8T332jisa+h6tEDYsVticbSnyU8dKOIbgU6ux5VTjg3yt+WGzjlpKn6NPhRjpA912xMezR4kw6KWwMrCVKSVCZciVGCgavjIQ6X8tCOp3yZbGpy0VxpAe+77TszTfRd5RJSVO/HTnifJpXgCSUdUue1v6h0EIBYYI1BD1DlD+C0CR8e6OewpusjZ4uBl9FyJvnhvQl+q5rv1ixrcpCumEPo5MJSgM9ehVsNPfUM669WuMyVWQLCzpu9GhglF2PE=]]></Encrypt></xml>`
    ret, decryp_xml := cryp_test.DecryptMsg(from_xml, msg_sign, timestamp, nonce)
    fmt.Println(ret)
    fmt.Println(decryp_xml)
}

func main() {
    testWXBizMsgCrypt()
}

weixin官方提供的加解密模块的其他语言(C#, C++, Java, PHP, Python)版本 Download.

License

This project is under the MIT License.

Documentation

Index

Constants

View Source
const (
	LETTERDIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	LD_COUNT     = 62
)
View Source
const (
	WXBizMsgCrypt_OK                      = 0
	WXBizMsgCrypt_ValidateSignature_Error = -40001
	WXBizMsgCrypt_ParseXml_Error          = -40002
	WXBizMsgCrypt_ComputeSignature_Error  = -40003
	WXBizMsgCrypt_IllegalAesKey           = -40004
	WXBizMsgCrypt_ValidateAppid_Error     = -40005
	WXBizMsgCrypt_EncryptAES_Error        = -40006
	WXBizMsgCrypt_DecryptAES_Error        = -40007
	WXBizMsgCrypt_IllegalBuffer           = -40008
	WXBizMsgCrypt_EncodeBase64_Error      = -40009
	WXBizMsgCrypt_DecodeBase64_Error      = -40010
	WXBizMsgCrypt_GenReturnXml_Error      = -40011
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PKCS7Encoder

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

提供基于PKCS7算法的加解密接口

func NewPKCS7Encoder

func NewPKCS7Encoder() *PKCS7Encoder

type Prpcrypt

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

func NewPrpcrypt

func NewPrpcrypt(key string) *Prpcrypt

提供接收和推送给公众平台消息的加解密接口

type WXBizMsgCrypt

type WXBizMsgCrypt interface {
	EncryptMsg(sReplyMsg string, sNonce string, timestamp int64) (int, string)
	DecryptMsg(sPostData, sMsgSignature string, sTimeStamp int64, sNonce string) (int, string)
}

func NewWXBizMsgCrypt

func NewWXBizMsgCrypt(sToken, sEncodingAESKey, sAppId string) (WXBizMsgCrypt, error)

sToken: 公众平台上,开发者设置的Token sEncodingAESKey: 公众平台上,开发者设置的EncodingAESKey sAppId: 企业号的AppId

type XMLParse

type XMLParse struct {
	XMLtmpl string
}

func NewXMLParse

func NewXMLParse() *XMLParse

提供提取消息格式中的密文及生成回复消息格式的接口

type XmlResources

type XmlResources struct {
	XMLName    xml.Name `xml:"xml"`
	Encrypt    string   `xml:"Encrypt"`
	ToUserName string   `xml:"ToUserName"`
}

Jump to

Keyboard shortcuts

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