usermngmt/internal/helper.go

49 lines
1008 B
Go
Raw Normal View History

2021-11-10 01:44:22 +00:00
package internal
2021-11-08 10:04:01 +00:00
2021-11-09 04:48:24 +00:00
import (
2021-11-09 05:26:51 +00:00
"fmt"
2021-11-10 07:50:43 +00:00
"strings"
2021-11-09 05:26:51 +00:00
2022-10-10 08:55:47 +00:00
"git.selly.red/Selly-Modules/mongodb"
2021-11-09 04:48:24 +00:00
"golang.org/x/crypto/bcrypt"
)
2021-11-08 10:04:01 +00:00
2021-11-10 04:52:07 +00:00
// HashPassword ...
2021-11-10 01:44:22 +00:00
func HashPassword(password string) string {
2021-11-08 10:04:01 +00:00
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), passwordHashingCost)
return string(bytes)
}
2021-11-10 04:52:07 +00:00
// CheckPasswordHash ...
2021-11-10 01:44:22 +00:00
func CheckPasswordHash(password, hash string) bool {
2021-11-08 10:04:01 +00:00
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
2021-11-09 05:26:51 +00:00
2021-11-10 04:52:07 +00:00
// GetSearchString ...
2021-11-10 01:44:22 +00:00
func GetSearchString(fieldList ...string) string {
2021-11-09 05:26:51 +00:00
var (
searchList = make([]interface{}, 0)
format = ""
)
for i, value := range fieldList {
searchList = append(searchList, mongodb.NonAccentVietnamese(value))
if i == 0 {
format += "%s"
continue
}
format += " %s"
}
return fmt.Sprintf(format, searchList...)
}
2021-11-10 07:50:43 +00:00
2021-11-10 08:00:36 +00:00
// GenerateCode ...
func GenerateCode(s string) string {
2021-11-10 07:50:43 +00:00
var (
underscore = "_"
2021-11-10 08:00:36 +00:00
emptySpace = " "
2021-11-10 07:50:43 +00:00
)
2021-11-10 08:00:36 +00:00
return strings.ReplaceAll(mongodb.NonAccentVietnamese(s), emptySpace, underscore)
2021-11-10 07:50:43 +00:00
}