61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package tools
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"math/rand"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
//md5加密
|
|
func Md5(src string) string {
|
|
m := md5.New()
|
|
m.Write([]byte(src))
|
|
res := hex.EncodeToString(m.Sum(nil))
|
|
return res
|
|
}
|
|
|
|
//Sha256加密
|
|
func Sha256(src string) string {
|
|
m := sha256.New()
|
|
m.Write([]byte(src))
|
|
res := hex.EncodeToString(m.Sum(nil))
|
|
return res
|
|
}
|
|
func Base64Encode(subject string) string {
|
|
return base64.StdEncoding.EncodeToString([]byte(subject))
|
|
}
|
|
func Base64Decode2(subject string) ([]byte, error) {
|
|
return base64.StdEncoding.DecodeString(subject)
|
|
}
|
|
func Base64Decode(str string) string {
|
|
reader := strings.NewReader(str)
|
|
decoder := base64.NewDecoder(base64.RawStdEncoding, reader)
|
|
// 以流式解码
|
|
buf := make([]byte, 1024)
|
|
// 保存解码后的数据
|
|
dst := ""
|
|
for {
|
|
n, err := decoder.Read(buf)
|
|
dst += string(buf[:n])
|
|
if n == 0 || err != nil {
|
|
break
|
|
}
|
|
}
|
|
return dst
|
|
}
|
|
|
|
//生成随机数
|
|
func RandNum(n int) string {
|
|
rand.Seed(time.Now().UnixNano())
|
|
res := ""
|
|
for i := 1; i <= n; i++ {
|
|
res += strconv.Itoa(rand.Intn(10))
|
|
}
|
|
return res
|
|
}
|