refactor: 从 Beego 迁移到 GoFrame v2
- 替换 Beego 框架为 GoFrame v2 - 重构项目结构: controller/service/dao/middleware 分层 - 替换自定义 crons 包为 gcron - 模板从 views/ 迁移到 resource/template/ - 配置从 conf/app.conf 迁移到 config.yml - 数据库从 MySQL 切换为 SQLite (modernc.org/sqlite) - 移除 agent/ 远程执行器(待后续迁移) - 移除 crons/ 自定义定时器包 - 静态资源整理到 resource/static/
This commit is contained in:
+39
-66
@@ -1,83 +1,56 @@
|
||||
/**********************************************
|
||||
** @Des: This file ...
|
||||
** @Author: haodaquan
|
||||
** @Date: 2017-09-08 00:24:25
|
||||
** @Last Modified by: haodaquan
|
||||
** @Last Modified time: 2017-09-17 10:12:06
|
||||
***********************************************/
|
||||
|
||||
package libs
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/axgle/mahonia"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
|
||||
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
|
||||
|
||||
// Md5 计算MD5哈希(仅用于向后兼容旧密码验证)
|
||||
func Md5(buf []byte) string {
|
||||
hash := md5.New()
|
||||
hash.Write(buf)
|
||||
return fmt.Sprintf("%x", hash.Sum(nil))
|
||||
return fmt.Sprintf("%x", md5.Sum(buf))
|
||||
}
|
||||
|
||||
func SizeFormat(size float64) string {
|
||||
units := []string{"Byte", "KB", "MB", "GB", "TB"}
|
||||
n := 0
|
||||
for size > 1024 {
|
||||
size /= 1024
|
||||
n += 1
|
||||
// HashPassword 生成密码哈希(使用 SHA-256 + 随机盐值)
|
||||
// 格式: $sha256$<salt>$<hash>
|
||||
func HashPassword(password string) (string, string) {
|
||||
salt := grand.S(16)
|
||||
h := sha256.Sum256([]byte(salt + password))
|
||||
hash := fmt.Sprintf("$sha256$%s$%x", salt, h)
|
||||
return hash, salt
|
||||
}
|
||||
|
||||
// VerifyPassword 验证密码,兼容新旧两种哈希格式
|
||||
func VerifyPassword(password, storedHash, salt string) bool {
|
||||
// 新格式: $sha256$<salt>$<hash>
|
||||
if strings.HasPrefix(storedHash, "$sha256$") {
|
||||
parts := strings.Split(storedHash, "$")
|
||||
if len(parts) != 4 {
|
||||
return false
|
||||
}
|
||||
h := sha256.Sum256([]byte(parts[2] + password))
|
||||
expected := fmt.Sprintf("$sha256$%s$%x", parts[2], h)
|
||||
return storedHash == expected
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.2f %s", size, units[n])
|
||||
// 旧格式: MD5(password + salt)
|
||||
return Md5([]byte(password+salt)) == storedHash
|
||||
}
|
||||
|
||||
func IsEmail(b []byte) bool {
|
||||
return emailPattern.Match(b)
|
||||
}
|
||||
|
||||
func Password(len int, pwdO string) (pwd string, salt string) {
|
||||
salt = GetRandomString(4)
|
||||
defaultPwd := "george518"
|
||||
if pwdO != "" {
|
||||
defaultPwd = pwdO
|
||||
// Password 生成密码和盐值(已弃用,使用 HashPassword 代替)
|
||||
func Password(length int, pwdO string) (pwd string, salt string) {
|
||||
if pwdO == "" {
|
||||
pwdO = "george518"
|
||||
}
|
||||
pwd = Md5([]byte(defaultPwd + salt))
|
||||
return pwd, salt
|
||||
pwd, salt = HashPassword(pwdO)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成32位MD5
|
||||
// func MD5(text string) string{
|
||||
// ctx := md5.New()
|
||||
// ctx.Write([]byte(text))
|
||||
// return hex.EncodeToString(ctx.Sum(nil))
|
||||
// }
|
||||
|
||||
//生成随机字符串
|
||||
func GetRandomString(lens int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < lens; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func GbkAsUtf8(str string) string {
|
||||
srcDecoder := mahonia.NewDecoder("gbk")
|
||||
desDecoder := mahonia.NewDecoder("utf-8")
|
||||
resStr := srcDecoder.ConvertString(str)
|
||||
_, resBytes, _ := desDecoder.Translate([]byte(resStr), true)
|
||||
return string(resBytes)
|
||||
}
|
||||
|
||||
//任务识别码
|
||||
func JobKey(taskId, serverId int) int {
|
||||
return taskId*100000 + serverId
|
||||
// Sha256Hex 计算 SHA-256 十六进制字符串
|
||||
func Sha256Hex(data []byte) string {
|
||||
h := sha256.Sum256(data)
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user