新增后台管理

This commit is contained in:
2025-06-08 15:14:34 +08:00
parent 7762ad46fe
commit e75c7b3dd3
488 changed files with 20273 additions and 15879 deletions

View File

@@ -172,7 +172,7 @@ func hexEncodingExample() {
```yaml
# etc/main.yaml
Name: user-api
Name: main-api
Host: 0.0.0.0
Port: 8888

View File

@@ -0,0 +1,28 @@
package crypto
import (
"golang.org/x/crypto/bcrypt"
)
// PasswordHash 使用bcrypt对密码进行加密
// cost参数确定加密的复杂度默认为10越高越安全但性能消耗越大
func PasswordHash(password string, cost ...int) (string, error) {
defaultCost := 10
if len(cost) > 0 && cost[0] > 0 {
defaultCost = cost[0]
}
bytes, err := bcrypt.GenerateFromPassword([]byte(password), defaultCost)
if err != nil {
return "", err
}
return string(bytes), nil
}
// PasswordVerify 验证密码是否匹配
// password是用户输入的明文密码hash是存储的加密密码
func PasswordVerify(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}

View File

@@ -9,8 +9,8 @@ import (
func TestAesEcbMobileEncryption(t *testing.T) {
// 测试手机号加密
mobile := "13800138000"
key := []byte("1234567890abcdef") // 16字节AES-128密钥
mobile := "13280082033 "
key := []byte("ff83609b2b24fc73196aac3d3dfb874f") // 16字节AES-128密钥
keyStr := hex.EncodeToString(key)
// 测试加密
@@ -18,13 +18,14 @@ func TestAesEcbMobileEncryption(t *testing.T) {
if err != nil {
t.Fatalf("手机号加密失败: %v", err)
}
fmt.Println(encrypted)
fmt.Printf("encrypted: %s\n", encrypted)
jmStr := "oEpLcrIpDPN63rOlESXTDg=="
// 测试解密
decrypted, err := DecryptMobile(encrypted, keyStr)
decrypted, err := DecryptMobile(jmStr, keyStr)
if err != nil {
t.Fatalf("手机号解密失败: %v", err)
}
fmt.Println(decrypted)
fmt.Printf("decrypted: %s\n", decrypted)
// 验证结果
if decrypted != mobile {
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", mobile, decrypted)