This commit is contained in:
陈兔子
2025-11-14 19:15:00 +08:00
parent f61e97b861
commit 3978db210a
2 changed files with 93 additions and 5 deletions

View File

@@ -20,7 +20,7 @@ import (
"github.com/spf13/cast"
)
type HashClass struct{}
type HashClass struct {}
// Hash - 哈希加密
var Hash *HashClass
@@ -124,13 +124,13 @@ func (this *AESRequest) Encrypt(text any) (result *AESResponse) {
// 每个块的大小
blockSize := block.BlockSize()
// 计算需要填充的长度
padding := blockSize - len([]byte(cast.ToString(text)))%blockSize
padding := blockSize - len([]byte(cast.ToString(text)))%blockSize
// 填充
fill := append([]byte(cast.ToString(text)), bytes.Repeat([]byte{byte(padding)}, padding)...)
fill := append([]byte(cast.ToString(text)), bytes.Repeat([]byte{byte(padding)}, padding)...)
encode := make([]byte, len(fill))
item := cipher.NewCBCEncrypter(block, []byte(this.Iv))
item := cipher.NewCBCEncrypter(block, []byte(this.Iv))
item.CryptBlocks(encode, fill)
result.Byte = encode
@@ -184,7 +184,7 @@ func (this *AESRequest) Decrypt(text any) (result *AESResponse) {
var RSA *RSAClass
type RSAClass struct{}
type RSAClass struct {}
type RSAResponse struct {
// 私钥
@@ -344,4 +344,21 @@ func (this *RSAClass) PrivatePem(key string) (cert string) {
if err := pem.Encode(&PEM, block); err != nil { return "" }
return PEM.String()
}
type Md5Class struct {}
// Md5 - MD5 加密
var Md5 *Md5Class
// Encrypt 计算字符串的 MD5 哈希值(返回十六进制字符串)
func (this *Md5Class) Encrypt(value string) string {
// 创建 MD5 哈希对象
hash := md5.New()
// 写入数据(可以多次调用 Write 累加数据)
hash.Write([]byte(value))
// 计算哈希值,返回 []byte
hashBytes := hash.Sum(nil)
// 转为十六进制字符串
return hex.EncodeToString(hashBytes)
}

View File

@@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"sync"
"time"
"github.com/spf13/cast"
"github.com/spf13/viper"
@@ -155,4 +156,74 @@ func (this *GetClass) HostProtocol(url string) (isTLS bool, host string) {
// 判断是否为TLS
return protocol == "https://", matches[2]
}
// TodayTimestamp - 获取今天开始和结束时间戳
func (this *GetClass) TodayTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
return today.Unix(), today.Add(24 * time.Hour - time.Second).Unix()
}
// YesterdayTimestamp - 获取昨天开始和结束时间戳
func (this *GetClass) YesterdayTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
yesterday := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, loc)
return yesterday.Unix(), yesterday.Add(24 * time.Hour - time.Second).Unix()
}
// WeekTimestamp 获取本周开始和结束时间戳
func (this *GetClass) WeekTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
offset := int(time.Monday - now.Weekday())
if offset > 0 { offset = -6 }
weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc).AddDate(0, 0, offset)
weekEnd := weekStart.AddDate(0, 0, 6).Add(23*time.Hour + 59*time.Minute + 59*time.Second)
return weekStart.Unix(), weekEnd.Unix()
}
// LastWeekTimestamp 获取上周开始和结束时间戳
func (this *GetClass) LastWeekTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
offset := int(time.Monday - now.Weekday())
if offset > 0 { offset = -6 }
weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc).AddDate(0, 0, offset-7)
weekEnd := weekStart.AddDate(0, 0, 6).Add(23 * time.Hour + 59 * time.Minute + 59 * time.Second)
return weekStart.Unix(), weekEnd.Unix()
}
// MonthTimestamp 获取本月开始和结束时间戳
func (this *GetClass) MonthTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, loc)
monthEnd := monthStart.AddDate(0, 1, -1).Add(23 * time.Hour + 59 * time.Minute + 59 * time.Second)
return monthStart.Unix(), monthEnd.Unix()
}
// LastMonthTimestamp 获取上月开始和结束时间戳
func (this *GetClass) LastMonthTimestamp() (start int64, end int64) {
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
monthStart := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, loc)
monthEnd := monthStart.AddDate(0, 1, -1).Add(23 * time.Hour + 59 * time.Minute + 59 * time.Second)
return monthStart.Unix(), monthEnd.Unix()
}