| 1 |
package mns |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/hmac" |
| 5 |
"crypto/md5" |
| 6 |
"crypto/sha1" |
| 7 |
"encoding/base64" |
| 8 |
"encoding/hex" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
前时间戳(毫秒) |
| 15 |
func GetCurrentMillisecond() int64 { |
| 16 |
return time.Now().UnixNano() / int64(time.Millisecond) |
| 17 |
} |
| 18 |
|
| 19 |
前时间戳(秒) |
| 20 |
func GetCurrentUnixMicro() int64 { |
| 21 |
return time.Now().Unix() * 1000 |
| 22 |
} |
| 23 |
|
| 24 |
进行sha1 计算 |
| 25 |
func Sha1(data string) string { |
| 26 |
t := sha1.New() |
| 27 |
io.WriteString(t, data) |
| 28 |
return fmt.Sprintf("%x", t.Sum(nil)) |
| 29 |
} |
| 30 |
|
| 31 |
进行md5计算 |
| 32 |
func Md5(byteMessage []byte) string { |
| 33 |
h := md5.New() |
| 34 |
h.Write(byteMessage) |
| 35 |
return hex.EncodeToString(h.Sum(nil)) |
| 36 |
} |
| 37 |
|
| 38 |
func HamSha1(data string, key []byte) string { |
| 39 |
hmac := hmac.New(sha1.New, key) |
| 40 |
hmac.Write([]byte(data)) |
| 41 |
|
| 42 |
return base64.StdEncoding.EncodeToString(hmac.Sum(nil)) |
| 43 |
} |
| 44 |
|
| 45 |
func dump(data interface{}) { |
| 46 |
fmt.Println(data) |
| 47 |
} |