x

package module
v0.0.0-...-41150e2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: MIT Imports: 40 Imported by: 0

README

x

Build Status Go Report Card GoDoc

My util library

Install

$ go get -u github.com/txthinking/x

License

Licensed under The MIT License

Documentation

Index

Constants

View Source
const AES256KeyLength = 32

AES256KeyLength is the length of key for AES 256 crypt

View Source
const MailgunAPIURL = "https://api.mailgun.net/v3"

MailgunAPIURL is the base url of maingun api

Variables

View Source
var DefaultDial = &Dial{
	Timeout: 10 * time.Second,
}

Functions

func AESCBCDecrypt

func AESCBCDecrypt(c, k []byte) ([]byte, error)

AESCBCDecrypt decrypt c with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCBCEncrypt

func AESCBCEncrypt(s, k []byte) ([]byte, error)

AESCBCEncrypt encrypt s with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCFBDecrypt

func AESCFBDecrypt(c, k []byte) ([]byte, error)

AESDecrypt decrypt c with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCFBEncrypt

func AESCFBEncrypt(s, k []byte) ([]byte, error)

AESCFBEncrypt encrypt s with given k. k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits. First 16 bytes of cipher data is the IV.

func AESGCMDecrypt

func AESGCMDecrypt(c, k, n []byte) ([]byte, error)

AESGCMDecrypt decrypt s use k and nonce

func AESGCMEncrypt

func AESGCMEncrypt(s, k, n []byte) ([]byte, error)

AESGCMEncrypt encrypt s use k and nonce

func AESMake256Key

func AESMake256Key(k []byte) []byte

AESMake256Key cut or append empty data on the key and make sure the key lenth equal 32

func CheckHmacSha1

func CheckHmacSha1(message, messageMAC, key []byte) (bool, error)

func CheckHmacSha256

func CheckHmacSha256(message, messageMAC, key []byte) (bool, error)

func Decimal2IP

func Decimal2IP(n int64) string

Decimal2IP transform a decimal IP to x.x.x.x format.

func Decimal2IP0

func Decimal2IP0(n int64) (ip string, err error)

Decimal2IP0 transform a decimal IP to x.x.x.x format. ref: https://zh.wikipedia.org/wiki/IPv4

func Decimal2IP1

func Decimal2IP1(n int64) string

Decimal2IP1 transform a decimal IP to x.x.x.x format.

func DecryptStruct

func DecryptStruct(d []byte, v interface{}, k []byte) error

JSON Struct

func DecryptStructFrom

func DecryptStructFrom(r io.Reader, v interface{}, k []byte) error

JSON Struct

func EncryptStruct

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

JSON Struct

func EncryptStructTo

func EncryptStructTo(w io.Writer, v interface{}, k []byte) error

func GBK2UTF8

func GBK2UTF8(s []byte) ([]byte, error)

GBK2UTF8 transform s from GBK to UTF8 format

func GetAddressFromURL

func GetAddressFromURL(s string) (string, error)

func HasPort

func HasPort(host string) bool

func HkdfSha1RandomSalt

func HkdfSha1RandomSalt(secret, info []byte, sl int) (key []byte, salt []byte, err error)

func HkdfSha1WithSalt

func HkdfSha1WithSalt(secret, salt, info []byte) (key []byte, err error)

func HkdfSha256RandomSalt

func HkdfSha256RandomSalt(secret, info []byte, sl int) (key []byte, salt []byte, err error)

func HkdfSha256WithSalt

func HkdfSha256WithSalt(secret, salt, info []byte) (key []byte, err error)

func HmacSha1

func HmacSha1(message, key []byte) ([]byte, error)

func HmacSha256

func HmacSha256(message, key []byte) ([]byte, error)

func IP2Decimal

func IP2Decimal(ipstr string) (int64, error)

IP2Decimal transform ip format like x.x.x.x to decimal.

func IP2Decimal0

func IP2Decimal0(ip string) (n int64, err error)

IP2Decimal0 transform ip format like x.x.x.x to decimal. ref: https://zh.wikipedia.org/wiki/IPv4

func IP2Decimal1

func IP2Decimal1(ipstr string) (int64, error)

IP2Decimal1 transform ip format like x.x.x.x to decimal.

func IsBankCard

func IsBankCard(n int64) (ok bool, err error)

IsBankCard determine whether it is bankcard number

func IsChineseID

func IsChineseID(s string) (ok bool, err error)

IsChineseID determine whether it is Chinese ID Card Number

func IsChineseWords

func IsChineseWords(words string) (ok bool, err error)

IsChineseWords determine whether it is Chinese words Notice: NOT ALL

func IsEmail

func IsEmail(email string) (ok bool, err error)

IsEmail determine whether it is email address

func JSON

func JSON(w http.ResponseWriter, v interface{})

func MD5

func MD5(s string) (r string)

MD5 encrypt s according to md5 algorithm

func Markdown

func Markdown(markdown []byte) (html []byte)

Markdown format input to html

func MultipartFormDataFromFile

func MultipartFormDataFromFile(params, files map[string][]string, boundary string) (ior io.Reader, err error)

MultipartFormDataFromFile generate multipart form data according to RFC 2388. files is the paths of your files

func MultipartFormDataFromReader

func MultipartFormDataFromReader(params map[string][]string, files map[string][]io.Reader, boundary string) (ior io.Reader, err error)

MultipartFormDataFromReader generate multipart form data according to RFC 2388.

func OpenRedPacket

func OpenRedPacket(amount, quantity int64) int64

OpenRedPacket opens a red packet, amount and quantity are remaining data. amount and quantity must > 0. amount must >= quantity.

func PKCS5Padding

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

PKCS5Padding can append data of PKCS5 Common blockSize is aes.BlockSize

func PKCS5UnPadding

func PKCS5UnPadding(s []byte) ([]byte, error)

PKCS5UnPadding can unappend data of PKCS5

func Random

func Random(min, max int64) int64

Random used to get a random number between min and max

func RandomNumber

func RandomNumber() (i int64)

RandomNumber used to get a random number

func ReadJSON

func ReadJSON(r *http.Request, o interface{}) error

func SHA1

func SHA1(s string) (r string)

SHA1 encrypt s according to sha1 algorithm

func SHA256

func SHA256(s []byte) ([]byte, error)

SHA256 encrypt s according to sha256 algorithm

func SHA256String

func SHA256String(s string) (r string)

SHA256 encrypt s according to sha256 algorithm

func ToString

func ToString(data interface{}) (string, error)

ToString convert data to string, data must be one of map[string]string, map[string]interface{}, string, []string, struct

func URIEscape

func URIEscape(s string) string

func URIUnescape

func URIUnescape(s string) (string, error)

func UTF82GBK

func UTF82GBK(s []byte) ([]byte, error)

UTF82GBK transform s from UTF8 to GBK format

Types

type CIDRInfo

type CIDRInfo struct {
	First   string
	Last    string
	Block   int64
	Network string
	Count   int64
}

CIDRInfo is the struct of CIDR

func CIDR

func CIDR(cidr string) (c *CIDRInfo, err error)

CIDR return *CIDRInfo from like this x.x.x.x/x ref: http://goo.gl/AEUIi8

type Chat

type Chat struct {
	URL string
}

func (*Chat) SendCard

func (c *Chat) SendCard(img, text, link string) error

func (*Chat) SendText

func (c *Chat) SendText(data interface{}) error

Send data to hangout chat data must be one of map[string]string, map[string]interface{}, string, []string, struct

type CryptKV

type CryptKV struct {
	AESKey    []byte
	LifeCycle int
}

CryptKV can be used to crypt value by key and give it a lifecycle (s). Can be used in cookies

func (*CryptKV) Decrypt

func (kv *CryptKV) Decrypt(c string, k string) (string, error)

Decrypt key, value

func (*CryptKV) DecryptFromRequest

func (kv *CryptKV) DecryptFromRequest(r *http.Request, k string) (string, error)

Decrypt from http.Request

func (*CryptKV) Encrypt

func (kv *CryptKV) Encrypt(k string, v string) (string, error)

Encrypt key, value

type DB

type DB struct {
	DB *sqlx.DB
	// contains filtered or unexported fields
}

func NewDB

func NewDB(dsn string, connLifeTime time.Duration, s bool) (*DB, error)

connLifeTime must < mysql.wait_timeout

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

func (*DB) Delete

func (db *DB) Delete(o interface{}) error

Delete

func (*DB) Get

func (db *DB) Get(id int64, o interface{}) error

Get

func (*DB) Insert

func (db *DB) Insert(o interface{}) (int64, error)

Insert

func (*DB) Update

func (db *DB) Update(o interface{}) error

Update

func (*DB) UpdateExcept

func (db *DB) UpdateExcept(o interface{}, ignore ...string) error

Update

func (*DB) UpdateOnly

func (db *DB) UpdateOnly(o interface{}, only ...string) error

Update

type Dial

type Dial struct {
	Timeout time.Duration
}

DefaultDial is the default dialer which dial with tcp network

func (*Dial) Dial

func (d *Dial) Dial(network, addr string) (net.Conn, error)

Dial a remote address

type Dialer

type Dialer interface {
	Dial(network, addr string) (net.Conn, error)
}

Dialer is a common interface for dialing

type JPush

type JPush struct {
	AppKey       string
	MasterSecret string
}

func (*JPush) Message

func (jp *JPush) Message(ids []string, extras interface{}) error

If ids length equal 0 means push to all, extras must can be encoded into json object.

func (*JPush) Notification

func (jp *JPush) Notification(ids []string, alert string, extras interface{}, production bool) error

If ids length equal 0 means push to all, extras must can be encoded into json object.

type Mailgun

type Mailgun struct {
	Domain string
	APIKey string
}

Mailgun is your maingun domain, api key config

func (*Mailgun) Send

func (m *Mailgun) Send(msg *mailx.Message) error

Send can send email message by Maingun

type SafeMap

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

SafeMap is concurrent security map

func NewSafeMap

func NewSafeMap() *SafeMap

NewSafeMap get a new concurrent security map

func (*SafeMap) Delete

func (m *SafeMap) Delete(k interface{})

Delete used to delete a key

func (*SafeMap) Get

func (m *SafeMap) Get(k interface{}) interface{}

Get used to get a value by key

func (*SafeMap) IsExists

func (m *SafeMap) IsExists(k interface{}) bool

IsExists determine whether k exists

func (*SafeMap) Set

func (m *SafeMap) Set(k interface{}, v interface{}) bool

Set used to set value with key

type TencentSMS

type TencentSMS struct {
	AppID  string
	AppKey string
}

func (*TencentSMS) Send

func (t *TencentSMS) Send(message, mobile string) error

type Tx

type Tx struct {
	Tx *sqlx.Tx
	// contains filtered or unexported fields
}

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) Delete

func (tx *Tx) Delete(o interface{}) error

Delete

func (*Tx) Get

func (tx *Tx) Get(id int64, o interface{}) error

Get

func (*Tx) Insert

func (tx *Tx) Insert(o interface{}) (int64, error)

Insert

func (*Tx) Rollback

func (tx *Tx) Rollback(err error) error

func (*Tx) Update

func (tx *Tx) Update(o interface{}) error

Update

func (*Tx) UpdateExcept

func (tx *Tx) UpdateExcept(o interface{}, ignore ...string) error

Update

func (*Tx) UpdateOnly

func (tx *Tx) UpdateOnly(o interface{}, only ...string) error

Update

type YunpianSMS

type YunpianSMS struct {
	ApiKey string
}

func (*YunpianSMS) Send

func (y *YunpianSMS) Send(message, mobile string) error

Jump to

Keyboard shortcuts

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