2022-08-02 21:44:01 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-18 22:07:37 +08:00
|
|
|
"mayfly-go/internal/pkg/config"
|
2023-07-22 20:51:46 +08:00
|
|
|
"regexp"
|
2022-08-02 21:44:01 +08:00
|
|
|
)
|
|
|
|
|
|
2023-07-22 20:51:46 +08:00
|
|
|
// 检查用户密码安全等级
|
|
|
|
|
func CheckAccountPasswordLever(ps string) bool {
|
|
|
|
|
if len(ps) < 8 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-11-20 22:43:53 +08:00
|
|
|
// 包含大写字母
|
|
|
|
|
charRegex := regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
|
// 包含数字
|
|
|
|
|
digitRegex := regexp.MustCompile(`[0-9]`)
|
|
|
|
|
// 包含特殊符号
|
|
|
|
|
specialCharRegex := regexp.MustCompile(`[!@#$%^&*(),.?":{}|<>]`)
|
|
|
|
|
|
|
|
|
|
return charRegex.MatchString(ps) &&
|
|
|
|
|
digitRegex.MatchString(ps) &&
|
|
|
|
|
specialCharRegex.MatchString(ps)
|
2023-07-22 20:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-02 21:44:01 +08:00
|
|
|
// 使用config.yml的aes.key进行密码加密
|
2024-01-05 08:55:34 +08:00
|
|
|
func PwdAesEncrypt(password string) (string, error) {
|
2022-08-02 21:44:01 +08:00
|
|
|
if password == "" {
|
2024-01-05 08:55:34 +08:00
|
|
|
return "", nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
|
|
|
|
aes := config.Conf.Aes
|
2023-09-02 17:24:18 +08:00
|
|
|
if aes.Key == "" {
|
2024-01-05 08:55:34 +08:00
|
|
|
return password, nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
|
|
|
|
encryptPwd, err := aes.EncryptBase64([]byte(password))
|
2024-01-05 08:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return encryptPwd, nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用config.yml的aes.key进行密码解密
|
2024-01-05 08:55:34 +08:00
|
|
|
func PwdAesDecrypt(encryptPwd string) (string, error) {
|
2022-08-04 20:47:13 +08:00
|
|
|
if encryptPwd == "" {
|
2024-01-05 08:55:34 +08:00
|
|
|
return "", nil
|
2022-08-04 20:47:13 +08:00
|
|
|
}
|
2022-08-02 21:44:01 +08:00
|
|
|
aes := config.Conf.Aes
|
2023-09-02 17:24:18 +08:00
|
|
|
if aes.Key == "" {
|
2024-01-05 08:55:34 +08:00
|
|
|
return encryptPwd, nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
|
|
|
|
decryptPwd, err := aes.DecryptBase64(encryptPwd)
|
2024-01-05 08:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2022-08-02 21:44:01 +08:00
|
|
|
// 解密后的密码
|
2024-01-05 08:55:34 +08:00
|
|
|
return string(decryptPwd), nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|