You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
902 B
49 lines
902 B
package utils
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func GetSHA1(rawStr string) (result string) {
|
|
//s := "sha2 this string"
|
|
|
|
/*
|
|
生成一个hash的模式是sha1.New()
|
|
sha1.Write(bytes)
|
|
sha1.Sum()
|
|
*/
|
|
|
|
h := sha1.New()
|
|
h.Write([]byte(rawStr))
|
|
bs := h.Sum(nil)
|
|
fmt.Printf("%x \n", bs)
|
|
result = fmt.Sprintf("%X", bs)
|
|
return
|
|
}
|
|
|
|
func GetSHA1FromFile(fileName string) (result string) {
|
|
|
|
content, err := os.ReadFile(fileName)
|
|
log.Printf("err=%v", err)
|
|
h := sha1.New()
|
|
h.Write(content)
|
|
bs := h.Sum(nil)
|
|
result = fmt.Sprintf("%X", bs)
|
|
log.Printf("SHA1_file=%s", result)
|
|
return
|
|
}
|
|
|
|
// GetRnd 获取当前时间戳 到秒
|
|
func GetRnd() (rnd string) {
|
|
t := time.Now().Unix()
|
|
log.Printf("%d", t)
|
|
return
|
|
}
|
|
|
|
func GetSign(postStr, rnd, AppKey, AppSecret string) (sign string) {
|
|
return GetSHA1(postStr + rnd + AppKey + AppSecret)
|
|
}
|
|
|