package utils import ( "bytes" "compress/flate" "crypto/hmac" "crypto/sha256" "encoding/base64" "encoding/json" "io/ioutil" "log" "strconv" "strings" "time" //"net/http" ) /* Get a epoch time eg: 1521221737 */ func EpochTime() string { millisecond := time.Now().UnixNano() / 1000000 epoch := strconv.Itoa(int(millisecond)) epochBytes := []byte(epoch) epoch = string(epochBytes[:10]) return epoch } /* signing a message using: hmac sha256 + base64 eg: message = Pre_hash function comment secretKey = E65791902180E9EF4510DB6A77F6EBAE return signed string = TO6uwdqz+31SIPkd4I+9NiZGmVH74dXi+Fd5X0EzzSQ= */ func HmacSha256Base64Signer(message string, secretKey string) (string, error) { mac := hmac.New(sha256.New, []byte(secretKey)) _, err := mac.Write([]byte(message)) if err != nil { return "", err } return base64.StdEncoding.EncodeToString(mac.Sum(nil)), nil } /* the pre hash string eg: timestamp = 2018-03-08T10:59:25.789Z method = POST request_path = /orders?before=2&limit=30 body = {"product_id":"BTC-USD-0309","order_id":"377454671037440"} return pre hash string = 2018-03-08T10:59:25.789ZPOST/orders?before=2&limit=30{"product_id":"BTC-USD-0309","order_id":"377454671037440"} */ func PreHashString(timestamp string, method string, requestPath string, body string) string { return timestamp + strings.ToUpper(method) + requestPath + body } /* struct convert json string */ func Struct2JsonString(raw interface{}) (jsonString string, err error) { //fmt.Println("转化json,", raw) data, err := json.Marshal(raw) if err != nil { log.Println("convert json failed!", err) return "", err } //log.Println(string(data)) return string(data), nil } // 解压缩消息 func GzipDecode(in []byte) ([]byte, error) { reader := flate.NewReader(bytes.NewReader(in)) defer reader.Close() return ioutil.ReadAll(reader) } /* Get a iso time eg: 2018-03-16T18:02:48.284Z */ func IsoTime() string { utcTime := time.Now().UTC() iso := utcTime.String() isoBytes := []byte(iso) iso = string(isoBytes[:10]) + "T" + string(isoBytes[11:23]) + "Z" return iso }