wx

package
v0.0.0-...-4a54426 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeBytesToUint32

func DecodeBytesToUint32(b []byte) uint32

DecodeBytesToUint32 从 4 字节的网络字节序里解析出整数 uint32

func EncodeUint32ToBytes

func EncodeUint32ToBytes(i uint32) []byte

EncodeUint32ToBytes 把整数 uint32 格式化成 4 字节的网络字节序

func FormatMap2XML

func FormatMap2XML(m WXML) ([]byte, error)

FormatMap2XML format map to xml

func FormatMap2XMLForTest

func FormatMap2XMLForTest(m WXML) ([]byte, error)

FormatMap2XMLForTest 用于单元测试

func HMacSHA256

func HMacSHA256(s, key string) string

HMacSHA256 generates a keyed sha256 hash value.

func HTTPDo

func HTTPDo(ctx context.Context, method, reqURL string, body []byte, options ...HTTPOption) ([]byte, error)

HTTPDo sends an HTTP request and returns an HTTP response

func HTTPGet

func HTTPGet(ctx context.Context, reqURL string, options ...HTTPOption) ([]byte, error)

HTTPGet issues a GET to the specified URL.

func HTTPPost

func HTTPPost(ctx context.Context, reqURL string, body []byte, options ...HTTPOption) ([]byte, error)

HTTPPost issues a POST to the specified URL.

func HTTPPostForm

func HTTPPostForm(ctx context.Context, reqURL string, data url.Values, options ...HTTPOption) ([]byte, error)

HTTPPostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

func HTTPUpload

func HTTPUpload(ctx context.Context, reqURL string, form UploadForm, options ...HTTPOption) ([]byte, error)

HTTPUpload issues a UPLOAD to the specified URL.

func LoadCertFromPfxFile

func LoadCertFromPfxFile(pfxfile, mchid string) (tls.Certificate, error)

LoadCertFromPfxFile 通过pfx(p12)证书文件生成TLS证书

func MD5

func MD5(s string) string

MD5 calculates the md5 hash of a string.

func MarshalNoEscapeHTML

func MarshalNoEscapeHTML(v interface{}) ([]byte, error)

MarshalNoEscapeHTML marshal with no escape HTML

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.

func Nonce

func Nonce(size uint8) string

Nonce returns nonce string, param `size` better for even number.

func PKCS5Padding

func PKCS5Padding(cipherText []byte, blockSize int) []byte

func PKCS5Unpadding

func PKCS5Unpadding(plainText []byte, blockSize int) []byte

func SHA1

func SHA1(s string) string

SHA1 calculates the sha1 hash of a string.

func SHA256

func SHA256(s string) string

SHA256 calculates the sha256 hash of a string.

func ZeroPadding

func ZeroPadding(cipherText []byte, blockSize int) []byte

func ZeroUnPadding

func ZeroUnPadding(plainText []byte) []byte

Types

type AESCrypto

type AESCrypto interface {
	// Encrypt encrypts the plain text.
	Encrypt(plainText []byte) ([]byte, error)

	// Decrypt decrypts the cipher text.
	Decrypt(cipherText []byte) ([]byte, error)
}

AESCrypto is the interface for aes crypto.

func NewCBCCrypto

func NewCBCCrypto(key, iv []byte, mode AESPaddingMode) AESCrypto

NewCBCCrypto returns a new aes-cbc crypto.

func NewECBCrypto

func NewECBCrypto(key []byte, mode AESPaddingMode) AESCrypto

NewECBCrypto returns a new aes-ecb crypto.

type AESPaddingMode

type AESPaddingMode int

AESPaddingMode aes padding mode

const (
	// AES_ZERO zero padding mode
	AES_ZERO AESPaddingMode = iota
	// AES_PKCS5 PKCS#5 padding mode
	AES_PKCS5
	// AES_PKCS7 PKCS#7 padding mode
	AES_PKCS7
)

type Action

type Action interface {
	// Method returns action method
	Method() string

	// URL returns request url
	URL(accessToken ...string) string

	// WXML returns body for xml request
	WXML(mchid, apikey, nonce string) (WXML, error)

	// Body returns body for post request
	Body() ([]byte, error)

	// UploadForm returns form for uploading media
	UploadForm() (UploadForm, error)

	// Decode decodes response
	Decode(b []byte) error

	// IsUpload specifies the request does upload
	IsUpload() bool

	// TLS specifies the request with certificate
	IsTLS() bool
}

Action is the interface that handle wechat api

func NewAction

func NewAction(method string, reqURL string, options ...ActionOption) Action

NewAction returns a new action

func NewGetAction

func NewGetAction(reqURL string, options ...ActionOption) Action

NewGetAction returns a new action with GET method

func NewPostAction

func NewPostAction(reqURL string, options ...ActionOption) Action

NewPostAction returns a new action with POST method

type ActionOption

type ActionOption func(a *action)

ActionOption configures how we set up the action

func WithBody

func WithBody(f func() ([]byte, error)) ActionOption

WithBody sets post body for action.

func WithDecode

func WithDecode(f func(b []byte) error) ActionOption

WithDecode sets response decode for action.

func WithQuery

func WithQuery(key, value string) ActionOption

WithQuery sets query params for action.

func WithTLS

func WithTLS() ActionOption

WithTLS sets request with tls for action.

func WithUpload

func WithUpload(f func() (UploadForm, error)) ActionOption

WithUpload sets uploadform for action.

func WithWXML

func WithWXML(f func(mchid, apikey, nonce string) (WXML, error)) ActionOption

WithWXML sets post with xml for action.

type CDATA

type CDATA string

CDATA XML CDATA section which is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup.

func (CDATA) MarshalXML

func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML encodes the receiver as zero or more XML elements.

type FormFileFunc

type FormFileFunc func(w io.Writer) error

FormFileFunc writes file content to multipart writer.

type HTTPClient

type HTTPClient interface {
	// Do sends an HTTP request and returns an HTTP response.
	// Should use context to specify the timeout for request.
	Do(ctx context.Context, method, reqURL string, body []byte, options ...HTTPOption) ([]byte, error)

	// Upload issues a UPLOAD to the specified URL.
	// Should use context to specify the timeout for request.
	Upload(ctx context.Context, reqURL string, form UploadForm, options ...HTTPOption) ([]byte, error)
}

HTTPClient is the interface for a http client.

func NewDefaultClient

func NewDefaultClient(certs ...tls.Certificate) HTTPClient

NewDefaultClient returns a default http client

func NewHTTPClient

func NewHTTPClient(client *http.Client) HTTPClient

NewHTTPClient returns a new http client

type HTTPOption

type HTTPOption func(s *httpSetting)

HTTPOption configures how we set up the http request.

func WithHTTPClose

func WithHTTPClose() HTTPOption

WithHTTPClose specifies close the connection after replying to this request (for servers) or after sending this request and reading its response (for clients).

func WithHTTPCookies

func WithHTTPCookies(cookies ...*http.Cookie) HTTPOption

WithHTTPCookies specifies the cookies to http request.

func WithHTTPHeader

func WithHTTPHeader(key, value string) HTTPOption

WithHTTPHeader specifies the header to http request.

type M

type M map[string]interface{}

M is a convenient alias for a map[string]interface{}.

type PrivateKey

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

PrivateKey RSA private key

func NewPrivateKeyFromPemBlock

func NewPrivateKeyFromPemBlock(mode RSAPaddingMode, pemBlock []byte) (*PrivateKey, error)

NewPrivateKeyFromPemBlock returns new private key with pem block.

func NewPrivateKeyFromPemFile

func NewPrivateKeyFromPemFile(mode RSAPaddingMode, pemFile string) (*PrivateKey, error)

NewPrivateKeyFromPemFile returns new private key with pem file.

func NewPrivateKeyFromPfxFile

func NewPrivateKeyFromPfxFile(pfxFile, password string) (*PrivateKey, error)

NewPrivateKeyFromPfxFile returns private key with pfx(p12) file.

func (*PrivateKey) Decrypt

func (pk *PrivateKey) Decrypt(cipherText []byte) ([]byte, error)

Decrypt rsa decrypt with PKCS #1 v1.5

func (*PrivateKey) DecryptOAEP

func (pk *PrivateKey) DecryptOAEP(hash crypto.Hash, cipherText []byte) ([]byte, error)

DecryptOAEP rsa decrypt with PKCS #1 OAEP.

func (*PrivateKey) Sign

func (pk *PrivateKey) Sign(hash crypto.Hash, data []byte) ([]byte, error)

Sign returns sha-with-rsa signature.

type PublicKey

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

PublicKey RSA public key

func NewPublicKeyFromDerBlock

func NewPublicKeyFromDerBlock(pemBlock []byte) (*PublicKey, error)

NewPublicKeyFromDerBlock returns public key with DER block. NOTE: PEM format with -----BEGIN CERTIFICATE----- | -----END CERTIFICATE----- CMD: openssl x509 -inform der -in cert.cer -out cert.pem

func NewPublicKeyFromDerFile

func NewPublicKeyFromDerFile(pemFile string) (*PublicKey, error)

NewPublicKeyFromDerFile returns public key with DER file. NOTE: PEM format with -----BEGIN CERTIFICATE----- | -----END CERTIFICATE----- CMD: openssl x509 -inform der -in cert.cer -out cert.pem

func NewPublicKeyFromPemBlock

func NewPublicKeyFromPemBlock(mode RSAPaddingMode, pemBlock []byte) (*PublicKey, error)

NewPublicKeyFromPemBlock returns new public key with pem block.

func NewPublicKeyFromPemFile

func NewPublicKeyFromPemFile(mode RSAPaddingMode, pemFile string) (*PublicKey, error)

NewPublicKeyFromPemFile returns new public key with pem file.

func (*PublicKey) Encrypt

func (pk *PublicKey) Encrypt(plainText []byte) ([]byte, error)

Encrypt rsa encrypt with PKCS #1 v1.5

func (*PublicKey) EncryptOAEP

func (pk *PublicKey) EncryptOAEP(hash crypto.Hash, plainText []byte) ([]byte, error)

EncryptOAEP rsa encrypt with PKCS #1 OAEP.

func (*PublicKey) Verify

func (pk *PublicKey) Verify(hash crypto.Hash, data, signature []byte) error

Verify verifies the sha-with-rsa signature.

type RSAPaddingMode

type RSAPaddingMode int

RSAPaddingMode pem block type which taken from the preamble.

const (
	// RSA_PKCS1 this kind of key is commonly encoded in PEM blocks of type "RSA PRIVATE KEY" and "RSA PUBLIC KEY"
	RSA_PKCS1 RSAPaddingMode = iota
	// RSA_PKCS8 this kind of key is commonly encoded in PEM blocks of type "PRIVATE KEY" and "PUBLIC KEY"
	RSA_PKCS8
)

type SignType

type SignType string

签名类型

const (
	SignMD5        SignType = "MD5"
	SignHMacSHA256 SignType = "HMAC-SHA256"
)

func (SignType) Do

func (st SignType) Do(key string, m WXML, toUpper bool) string

type UploadField

type UploadField func(f *uploadform)

UploadField configures how we set up the upload from.

func WithFormField

func WithFormField(fieldname, fieldvalue string) UploadField

WithFormField specifies the form field to upload from.

func WithFormFile

func WithFormFile(fieldname, filename string, fn FormFileFunc) UploadField

WithFormFile specifies the file field to upload from.

type UploadForm

type UploadForm interface {
	// Write writes fields to multipart writer
	Write(w *multipart.Writer) error
}

UploadForm is the interface for http upload.

func NewUploadForm

func NewUploadForm(fields ...UploadField) UploadForm

NewUploadForm returns an upload form

type WXML

type WXML map[string]string

WXML deal with xml for wechat

func ParseXML2Map

func ParseXML2Map(b []byte) (WXML, error)

ParseXML2Map parse xml to map

Jump to

Keyboard shortcuts

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