hi-server/pkg/tool/encryption.go
shanshanzhong 0f38b3fcd3
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 6m39s
refactor(auth): 优化设备登录逻辑,移除冗余代码并添加设备缓存
feat(database): 添加用户算法和盐字段的迁移脚本

fix(subscribe): 修复服务器用户列表缓存问题,临时禁用缓存

style(model): 清理用户模型注释,简化代码结构

chore: 删除无用脚本和测试文件

docs: 添加用户绑定流程文档

perf(login): 优化设备登录性能,添加设备缓存键

fix(unbind): 修复设备解绑时的缓存清理逻辑

refactor(verify): 简化邮箱验证逻辑,移除冗余代码

build(docker): 更新Dockerfile配置,使用scratch基础镜像
2025-10-28 20:46:21 -07:00

59 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tool
import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"strings"
"github.com/anaskhan96/go-password-encoder"
"golang.org/x/crypto/bcrypt"
)
var options = &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
func EncodePassWord(str string) string {
salt, encodedPwd := password.Encode(str, options)
newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
return newPassword
}
func VerifyPassWord(passwd, EncodePasswd string) bool {
info := strings.Split(EncodePasswd, "$")
return password.Verify(passwd, info[2], info[3], options)
}
func Md5Encode(str string, isUpper bool) string {
sum := md5.Sum([]byte(str))
res := hex.EncodeToString(sum[:])
//转大写strings.ToUpper(res)
if isUpper {
res = strings.ToUpper(res)
}
return res
}
func MultiPasswordVerify(algo, salt, password, hash string) bool {
switch algo {
case "md5":
sum := md5.Sum([]byte(password))
return hex.EncodeToString(sum[:]) == hash
case "sha256":
sum := sha256.Sum256([]byte(password))
return hex.EncodeToString(sum[:]) == hash
case "md5salt":
sum := md5.Sum([]byte(password + salt))
return hex.EncodeToString(sum[:]) == hash
case "default": // PPanel's default algorithm
return VerifyPassWord(password, hash)
case "bcrypt":
// Bcrypt (corresponding to PHP's password_hash/password_verify)
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
default:
return VerifyPassWord(password, hash)
}
}