From f64dd9f191dff341b4eb430d7bacc44a3db9a279 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 04 三月 2022 10:12:45 +0800
Subject: [PATCH] fix nil

---
 extend/util/util.go |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)

diff --git a/extend/util/util.go b/extend/util/util.go
index 1ae905f..3887458 100644
--- a/extend/util/util.go
+++ b/extend/util/util.go
@@ -5,16 +5,33 @@
 	"crypto/md5"
 	"crypto/sha256"
 	"encoding/base64"
+	"encoding/hex"
+	"fmt"
+	"math/rand"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"strconv"
+	"strings"
+	"time"
 	"unsafe"
 )
+
+func Sha256(message string) string {
+	h := sha256.New()
+	h.Write([]byte(message))
+	res := h.Sum(nil)
+	return hex.EncodeToString(res)
+}
+
 func ComputeHmacSha256Base64(message string, secret string) string {
 	key := []byte(secret)
 	h := hmac.New(sha256.New, key)
 	h.Write([]byte(message))
 	abs := h.Sum(nil)
-
 	return base64.StdEncoding.EncodeToString(abs)
 }
+
 
 func Hik_ComputeSignatureBase64(appKey string, appSecret string, httpMethod string, accept string, contentType string, date string, url string) string {
 	httpHeaders := httpMethod +"\n" + accept + "\n" + contentType + "\n"+ date +"\n"
@@ -43,4 +60,42 @@
 func MD5Base64(s []byte) string {
 	sum := md5.Sum(s)
 	return base64.StdEncoding.EncodeToString(sum[:])
+}
+
+func GenValidateCode(width int) string {
+	numeric := [10]byte{0,1,2,3,4,5,6,7,8,9}
+	r := len(numeric)
+	rand.Seed(time.Now().UnixNano())
+
+	var sb strings.Builder
+	for i := 0; i < width; i++ {
+		fmt.Fprintf(&sb, "%d", numeric[ rand.Intn(r) ])
+	}
+	str := sb.String()
+	if strings.HasPrefix(str, "0") {
+		pI := RandInt(1,9)
+		str = strconv.Itoa(pI) + str[1:]
+	}
+	return str
+}
+
+//鐢熸垚鍖洪棿闅忔満鏁�
+func RandInt(min, max int) int {
+	if min >= max || min ==0 ||max == 0 {
+		return max
+	}
+	return rand.Intn(max-min) + min
+}
+
+
+func GetAppRootPath() string {
+	file,err:= exec.LookPath(os.Args[0])
+	if err != nil {
+		return ""
+	}
+	p, err := filepath.Abs(file)
+	if err != nil {
+		return ""
+	}
+	return filepath.Dir(p)
 }
\ No newline at end of file

--
Gitblit v1.8.0