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.

71 lines
1.3 KiB

2 years ago
package untils
import (
"fmt"
"golang.org/x/crypto/bcrypt"
"reflect"
"strings"
"viviman.top/controllers"
)
2 years ago
// HashAndSalt 加密
2 years ago
func HashAndSalt(pwd []byte) string {
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
if err != nil {
controllers.ErrMsg(err.Error())
}
return string(hash)
}
2 years ago
// ComparePwd 密码验证
2 years ago
func ComparePwd(hashPwd string, plainPwd []byte) bool {
byteHash := []byte(hashPwd)
err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
if err != nil {
return false
}
return true
}
2 years ago
// Contains 判断array contain item
2 years ago
func Contains(array interface{}, val interface{}) (index int) {
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
{
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) {
index = i
return
}
}
}
}
return
}
2 years ago
// Convert 数组转字符串
2 years ago
//[a] -> a -> a
//[a b c] -> a b c -> a,b,c
func Convert(array interface{}) string {
return strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1)
}
2 years ago
// IntToBool 数值转布尔
2 years ago
func IntToBool(num int8) bool {
if num > 0 {
return true
}
return false
}
func ReturnQ(length int) string {
var str string
for i := 0; i < length; i++ {
str += ",?"
}
return str[1:]
}