usermngmt/internal/helper.go

39 lines
803 B
Go

package internal
import (
"fmt"
"github.com/Selly-Modules/mongodb"
"golang.org/x/crypto/bcrypt"
)
// HashPassword ...
func HashPassword(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), passwordHashingCost)
return string(bytes)
}
// CheckPasswordHash ...
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// GetSearchString ...
func GetSearchString(fieldList ...string) string {
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...)
}