This commit is contained in:
史悦
2025-08-13 19:03:20 +08:00
commit d62a2e9ed9
73 changed files with 7296 additions and 0 deletions

19
pkg/util/crypto.go Normal file
View File

@@ -0,0 +1,19 @@
package util
import (
"crypto/md5"
"fmt"
"ripper/pkg/crypto"
)
// CheckPassword 用于将用户输入的密码与数据库取出的密码进行比对
func CheckPassword(PlainText, SecretKey, CipherText string) bool {
chK, _ := crypto.AesEcrypt([]byte(PlainText), []byte(SecretKey))
return fmt.Sprintf("%x", md5.Sum(chK)) == CipherText
}
// CreatePassword 用于将用户输入的密码进行加密
func CreatePassword(SecretKey, PlainText string) string {
chK, _ := crypto.AesEcrypt([]byte(PlainText), []byte(SecretKey))
return fmt.Sprintf("%x", md5.Sum(chK))
}

63
pkg/util/encrypt.go Normal file
View File

@@ -0,0 +1,63 @@
package util
import (
"encoding/base64"
"regexp"
"strconv"
"strings"
)
// EmojiEncode Emoji表情转码
func EmojiDecode(s string) string {
//emoji表情的数据表达式
var re *regexp.Regexp
var reg *regexp.Regexp
var src []string
re = regexp.MustCompile("\\[[\\\\u0-9a-zA-Z]")
//提取emoji数据表达式
reg = regexp.MustCompile("\\[\\\\u|]")
src = re.FindAllString(s, -1)
for i := 0; i < len(src); i++ {
var e = reg.ReplaceAllString(src[i], "")
var p, err = strconv.ParseInt(e, 16, 32)
if err == nil {
s = strings.Replace(s, src[i], string(rune(p)), -1)
}
}
return s
}
// 表情转换
func EmojiCode(s string) string {
var ret string
var rs []rune
rs = []rune(s)
for i := 0; i < len(rs); i++ {
if len(string(rs[i])) == 4 {
ret += `[\u` + strconv.FormatInt(int64(rs[i]), 16) + `]`
} else {
ret += string(rs[i])
}
}
return ret
}
// BaseEncode Base64编码
func BaseEncode(s string) string {
data := []byte(s)
dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(dst, data)
return string(dst)
}
// BaseDecode Base64解码
func BaseDecode(s string) string {
data := []byte(s)
dst := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
n, err := base64.StdEncoding.Decode(dst, data)
if err != nil {
return ""
}
return string(dst[:n])
}

9
pkg/util/memory.go Normal file
View File

@@ -0,0 +1,9 @@
package util
import "unsafe"
func DeepCoyp(s string) string {
b := make([]byte, len(s))
copy(b, s)
return *(*string)(unsafe.Pointer(&b))
}

50
pkg/util/misc.go Normal file
View File

@@ -0,0 +1,50 @@
package util
import (
"github.com/gofrs/uuid"
"math/rand"
"time"
"unsafe"
)
// Ifs 三目运算的函数
func Ifs[T any](a bool, b, c T) T {
if a {
return b
}
return c
}
func GetUUID() (string, error) {
u2, err := uuid.NewV4()
return u2.String(), err
}
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var src = rand.NewSource(time.Now().UnixNano())
const (
// 6 bits to represent a letter index
letterIdBits = 6
// All 1-bits as many as letterIdBits
letterIdMask = 1<<letterIdBits - 1
letterIdMax = 63 / letterIdBits
)
func RandomStr(n int) string {
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdMax
}
if idx := int(cache & letterIdMask); idx < len(letters) {
b[i] = letters[idx]
i--
}
cache >>= letterIdBits
remain--
}
return *(*string)(unsafe.Pointer(&b))
}