refactor: 重构用户模型和密码验证逻辑 feat(epay): 添加支付类型支持 docs: 添加安装和配置指南文档 fix: 修复优惠券过期检查逻辑 perf: 优化设备解绑缓存清理流程 test: 添加密码验证测试用例 chore: 更新依赖版本
This commit is contained in:
@@ -2,12 +2,14 @@ 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}
|
||||
@@ -32,3 +34,24 @@ func Md5Encode(str string, isUpper bool) string {
|
||||
}
|
||||
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
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
package tool
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncodePassWord(t *testing.T) {
|
||||
t.Logf("EncodePassWord: %v", EncodePassWord("password"))
|
||||
}
|
||||
|
||||
func TestMultiPasswordVerify(t *testing.T) {
|
||||
pwd := "$2y$10$WFO17pdtohfeBILjEChoGeVxpDG.u9kVCKhjDAeEeNmCjIlj3tDRy"
|
||||
status := MultiPasswordVerify("bcrypt", "", "admin1", pwd)
|
||||
t.Logf("MultiPasswordVerify: %v", status)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user