parsec/utils.go
2023-06-02 15:41:51 +08:00

66 lines
1.2 KiB
Go

package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"math/rand"
"os"
"time"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func GetRandomString(l int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyz"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func GetSHA256HashCode(message []byte) string {
hash := sha256.New()
//输入数据
hash.Write(message)
//计算哈希值
bytes := hash.Sum(nil)
//将字符串编码为16进制格式,返回字符串
hashCode := hex.EncodeToString(bytes)
//返回哈希值
return hashCode
}
func formatJson(v interface{}) []byte {
marshal, _ := json.Marshal(v)
return marshal
//var b bytes.Buffer
//json.Indent(&b, marshal, "", " ")
//
//return b.Bytes()
}
func StringListContain(list []string, target string) bool {
if list == nil {
return false
}
for _, item := range list {
if item == target {
return true
}
}
return false
}