34 lines
642 B
Go
34 lines
642 B
Go
package helper
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"hash/fnv"
|
|
"math"
|
|
"os"
|
|
)
|
|
|
|
func FileExists(filePath string) bool {
|
|
_, err := os.Stat(filePath)
|
|
return err == nil || os.IsExist(err)
|
|
}
|
|
|
|
func HashMod(hashStr string) int {
|
|
hash := fnv.New32a()
|
|
_, _ = hash.Write([]byte(hashStr))
|
|
|
|
hashValue := hash.Sum32()
|
|
return int(math.Mod(float64(hashValue), 32))
|
|
}
|
|
|
|
func Md5(str string) string {
|
|
// 创建一个 MD5 哈希对象
|
|
hash := md5.New()
|
|
// 写入待加密的数据
|
|
hash.Write([]byte(str))
|
|
// 获取 MD5 哈希值
|
|
hashBytes := hash.Sum(nil)
|
|
// 将 MD5 哈希值转换为16进制字符串
|
|
return hex.EncodeToString(hashBytes)
|
|
}
|