代码拉取完成,页面将自动刷新
package utils
import (
"crypto/md5"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/url"
"os"
"strings"
"time"
"unsafe"
"reflect"
"runtime"
"github.com/google/uuid"
"github.com/intel-go/cpuid"
)
// FuncName return func name
func FuncName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// LocalIP return local ip
func LocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("Can not find the client ip address")
}
// RandomStr random str
func RandomStr(len int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 97
bytes[i] = byte(b)
}
return string(bytes)
}
// RandomIP ip address
func RandomIP() string {
rand.Seed(time.Now().Unix())
return fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
}
// Md5 for string
func Md5(data string) string {
h := md5.New()
h.Write(Str2Bytes(data))
return hex.EncodeToString(h.Sum(nil))
}
// FileMd5 calc file md5
func FileMd5(file string) string {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return ""
}
h := md5.New()
io.Copy(h, f)
return hex.EncodeToString(h.Sum(nil))
}
// Sha512 for string
func Sha512(data string) string {
h := md5.New()
h.Write(Str2Bytes(data))
return hex.EncodeToString(h.Sum(nil))
}
// FileSha512 calc file sha512
func FileSha512(file string) string {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return ""
}
h := sha512.New()
io.Copy(h, f)
return hex.EncodeToString(h.Sum(nil))
}
// UUID unique id
func UUID() string {
return strings.Replace(uuid.New().String(), "-", "", -1)
}
// CPUID unique cpu id
func CPUID() string {
features := []string{}
for i := uint64(0); i < 64; i++ {
if cpuid.HasExtendedFeature(1 << i) {
features = append(features, cpuid.ExtendedFeatureNames[1<<i])
}
}
return Md5(strings.Join(features, " "))
}
// URLJoin join urls
func URLJoin(base, href string) string {
uri, err := url.Parse(href)
if err != nil {
return href
}
baseURL, err := url.Parse(base)
if err != nil {
return href
}
return baseURL.ResolveReference(uri).String()
}
// Exists check
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// IsDir check
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// IsFile check
func IsFile(path string) bool {
return Exists(path) && !IsDir(path)
}
// ResetColor reset terminal color
func ResetColor() {
fmt.Printf("%c[0m", 0x1B)
}
// Str2Bytes convert string to byte
func Str2Bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
// Bytes2Str convert bytes to str
func Bytes2Str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。