utils

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufPool = &bufferPool{pool: sync.Pool{
	New: func() interface{} {
		return bytes.NewBuffer(make([]byte, 0, 4<<10))
	},
}}

BufPool buffer pool

View Source
var DefaultHTTPClient = &HTTPClient{
	client: &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			DialContext: (&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 60 * time.Second,
			}).DialContext,
			MaxIdleConns:          0,
			MaxIdleConnsPerHost:   1000,
			MaxConnsPerHost:       1000,
			IdleConnTimeout:       60 * time.Second,
			TLSHandshakeTimeout:   10 * time.Second,
			ExpectContinueTimeout: 1 * time.Second,
		},
	},
	timeout: defaultHTTPTimeout,
}

DefaultHTTPClient default http client

View Source
var Rand = rand.New(rand.NewSource(time.Now().UnixNano()))

Functions

func AESCBCDecrypt

func AESCBCDecrypt(cipherText, key []byte, iv ...byte) ([]byte, error)

AESCBCDecrypt AES CBC decrypt with PKCS#7 unpadding

func AESCBCEncrypt

func AESCBCEncrypt(plainText, key []byte, iv ...byte) ([]byte, error)

AESCBCEncrypt AES CBC encrypt with PKCS#7 padding

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) (string, error)

FormatMap2XML format map to xml

func HMAC

func HMAC(algo HashAlgo, s, key string) string

HMAC Generate a keyed hash value, expects: MD5, SHA1, SHA224, SHA256, SHA384, SHA512.

func Hash

func Hash(algo HashAlgo, s string) string

Hash Generate a hash value, expects: MD5, SHA1, SHA224, SHA256, SHA384, SHA512.

func MD5

func MD5(s string) string

MD5 calculate the md5 hash of a string.

func NonceStr

func NonceStr() string

NonceStr 随机字符串

func PKCS7Padding

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

PKCS7Padding PKCS#7 padding

func PKCS7UnPadding

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

PKCS7UnPadding PKCS#7 unpadding

func RSADecrypt added in v1.1.0

func RSADecrypt(cipherText, privateKey []byte) ([]byte, error)

RSADecrypt rsa decrypt with private key

func RSAEncrypt added in v1.1.0

func RSAEncrypt(data, publicKey []byte) ([]byte, error)

RSAEncrypt rsa encrypt with public key

func RandomStr

func RandomStr(n int) string

RandomStr 随机字符串

func SHA1

func SHA1(s string) string

SHA1 calculate the sha1 hash of a string.

Types

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 HTTPClient

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

HTTPClient http client

func NewHTTPClient

func NewHTTPClient(options ...HTTPClientOption) *HTTPClient

NewHTTPClient returns a new http client

func (*HTTPClient) Get

func (h *HTTPClient) Get(url string, options ...HTTPRequestOption) ([]byte, error)

Get http get request

func (*HTTPClient) GetXML

func (h *HTTPClient) GetXML(uri string, body WXML, options ...HTTPRequestOption) (WXML, error)

GetXML http xml get request

func (*HTTPClient) Post

func (h *HTTPClient) Post(url string, body []byte, options ...HTTPRequestOption) ([]byte, error)

Post http post request

func (*HTTPClient) PostXML

func (h *HTTPClient) PostXML(url string, body WXML, options ...HTTPRequestOption) (WXML, error)

PostXML http xml post request

type HTTPClientOption

type HTTPClientOption interface {
	// contains filtered or unexported methods
}

HTTPClientOption configures how we set up the http client

func WithHTTPDefaultTimeout

func WithHTTPDefaultTimeout(d time.Duration) HTTPClientOption

WithHTTPDefaultTimeout specifies the `DefaultTimeout` to http client.

func WithHTTPDialFallbackDelay

func WithHTTPDialFallbackDelay(d time.Duration) HTTPClientOption

WithHTTPDialFallbackDelay specifies the `FallbackDelay` to net.Dialer.

func WithHTTPDialKeepAlive

func WithHTTPDialKeepAlive(d time.Duration) HTTPClientOption

WithHTTPDialKeepAlive specifies the `KeepAlive` to net.Dialer.

func WithHTTPDialTimeout

func WithHTTPDialTimeout(d time.Duration) HTTPClientOption

WithHTTPDialTimeout specifies the `DialTimeout` to net.Dialer.

func WithHTTPExpectContinueTimeout

func WithHTTPExpectContinueTimeout(d time.Duration) HTTPClientOption

WithHTTPExpectContinueTimeout specifies the `ExpectContinueTimeout` to http client.

func WithHTTPIdleConnTimeout

func WithHTTPIdleConnTimeout(d time.Duration) HTTPClientOption

WithHTTPIdleConnTimeout specifies the `IdleConnTimeout` to http client.

func WithHTTPMaxConnsPerHost

func WithHTTPMaxConnsPerHost(n int) HTTPClientOption

WithHTTPMaxConnsPerHost specifies the `MaxConnsPerHost` to http client.

func WithHTTPMaxIdleConns

func WithHTTPMaxIdleConns(n int) HTTPClientOption

WithHTTPMaxIdleConns specifies the `MaxIdleConns` to http client.

func WithHTTPMaxIdleConnsPerHost

func WithHTTPMaxIdleConnsPerHost(n int) HTTPClientOption

WithHTTPMaxIdleConnsPerHost specifies the `MaxIdleConnsPerHost` to http client.

func WithHTTPTLSConfig

func WithHTTPTLSConfig(c *tls.Config) HTTPClientOption

WithHTTPTLSConfig specifies the `TLSClientConfig` to http client.

func WithHTTPTLSHandshakeTimeout

func WithHTTPTLSHandshakeTimeout(d time.Duration) HTTPClientOption

WithHTTPTLSHandshakeTimeout specifies the `TLSHandshakeTimeout` to http client.

type HTTPRequestOption

type HTTPRequestOption interface {
	// contains filtered or unexported methods
}

HTTPRequestOption configures how we set up the http request

func WithRequestClose

func WithRequestClose(b bool) HTTPRequestOption

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

func WithRequestCookies

func WithRequestCookies(cookies ...*http.Cookie) HTTPRequestOption

WithRequestCookies specifies the cookies to http request.

func WithRequestHeader

func WithRequestHeader(key, value string) HTTPRequestOption

WithRequestHeader specifies the headers to http request.

func WithRequestTimeout

func WithRequestTimeout(d time.Duration) HTTPRequestOption

WithRequestTimeout specifies the timeout to http request.

type HashAlgo added in v1.1.0

type HashAlgo string
const (
	AlgoMD5    HashAlgo = "md5"
	AlgoSha1   HashAlgo = "sha1"
	AlgoSha224 HashAlgo = "sha224"
	AlgoSha256 HashAlgo = "sha256"
	AlgoSha384 HashAlgo = "sha384"
	AlgoSha512 HashAlgo = "sha512"
)

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

type X

type X map[string]interface{}

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

Jump to

Keyboard shortcuts

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