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.
103 lines
2.1 KiB
103 lines
2.1 KiB
package commonus
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"math/big"
|
|
"strconv"
|
|
"time"
|
|
|
|
"gin_server_admin/commonus/snowflake"
|
|
"gin_server_admin/global"
|
|
)
|
|
|
|
// import (
|
|
// "crypto/md5"
|
|
// "exam_server/config"
|
|
// "fmt"
|
|
// )
|
|
|
|
// func Md5(str string, key ...string) string {
|
|
// key_val := config.AppConfig.AppKey
|
|
// if len(key) > 0 {
|
|
// key_val = key[0]
|
|
// }
|
|
// str = str + key_val
|
|
// data := []byte(str)
|
|
// has := md5.Sum(data)
|
|
// md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制
|
|
// return md5str1
|
|
// }
|
|
|
|
// 获取编号(副本)
|
|
func GetFileNumberEs() (num int64) {
|
|
|
|
node, err := snowflake.NewWorker(2)
|
|
if err == nil {
|
|
num = node.GetId()
|
|
} else {
|
|
// esult, _ := rand.Int(rand.Reader, big.NewInt(100))
|
|
result, _ := rand.Int(rand.Reader, big.NewInt(10000))
|
|
result64 := result.Int64()
|
|
// timeVal := time.Now().Unix()
|
|
timeVal := time.Now().UnixNano() / 1e6
|
|
num, _ = strconv.ParseInt(strconv.FormatInt(timeVal, 10)+strconv.FormatInt(result64, 10), 10, 64)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
*加密算法
|
|
*/
|
|
type Md5Encryption struct {
|
|
Code string `json:"code"`
|
|
AppKey string `json:"appKey"`
|
|
}
|
|
|
|
func (m *Md5Encryption) Md5EncryptionAlgorithm() (md5Val string) {
|
|
if m.AppKey == "" {
|
|
m.Md5EncryptionInit(m.Code)
|
|
}
|
|
// fmt.Printf("Code ====> %v\n", m.Code)
|
|
// fmt.Printf("AppKey ====> %v\n", m.AppKey)
|
|
|
|
mdNew := md5.New()
|
|
mdNew.Write([]byte(m.AppKey))
|
|
keyMd5 := fmt.Sprintf("%x", mdNew.Sum(nil))
|
|
|
|
codeNewMd1 := md5.New()
|
|
codeNewMd1.Write([]byte(m.Code))
|
|
codeMd1 := fmt.Sprintf("%x", codeNewMd1.Sum(nil))
|
|
|
|
yiCeng := codeMd1 + keyMd5
|
|
|
|
yiCengNew := md5.New()
|
|
yiCengNew.Write([]byte(yiCeng))
|
|
yiCengMd5 := fmt.Sprintf("%x", yiCengNew.Sum(nil))
|
|
|
|
erCeng := yiCengMd5 + m.AppKey
|
|
|
|
erCengNew := md5.New()
|
|
erCengNew.Write([]byte(erCeng))
|
|
md5Val = fmt.Sprintf("%x", erCengNew.Sum(nil))
|
|
|
|
// md5Val = codeMd1
|
|
return
|
|
}
|
|
|
|
// 初始化程序
|
|
func (m *Md5Encryption) Md5EncryptionInit(code string) {
|
|
m.AppKey = global.GVA_CONFIG.MyConfig.AppKey
|
|
m.Code = code
|
|
}
|
|
|
|
// sha1算法
|
|
func Sha1Encryption(str string) string {
|
|
sha1 := sha1.New()
|
|
sha1.Write([]byte(str))
|
|
return hex.EncodeToString(sha1.Sum(nil))
|
|
}
|
|
|