1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

Merge pull request #133 from LXY1226/UUID

加快图片UUID生成 减少内存占用
This commit is contained in:
Mrs4s 2021-03-06 20:02:52 +08:00 committed by GitHub
commit 7c15c6b049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 30 deletions

View File

@ -17,9 +17,7 @@ func isZero(a []byte) bool { // MAGIC
return *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) == 0
}
type TEA struct {
key [4]uint32
}
type TEA [4]uint32
// http://bbs.chinaunix.net/thread-583468-1-1.html
// 感谢xichen大佬对TEA的解释
@ -138,8 +136,8 @@ var sumTable = [0x10]uint32{
func (t *TEA) encode(src, dst []byte) {
v0, v1 := unpack(src)
for i := 0; i < 0x10; i++ {
v0 += ((v1 << 4) + t.key[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t.key[1])
v1 += ((v0 << 4) + t.key[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t.key[3])
v0 += ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
v1 += ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
}
repack(dst, v0, v1)
}
@ -149,8 +147,8 @@ func (t *TEA) encode(src, dst []byte) {
func (t *TEA) decode(src, dst []byte) {
v0, v1 := unpack(src)
for i := 0xf; i >= 0; i-- {
v1 -= ((v0 << 4) + t.key[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t.key[3])
v0 -= ((v1 << 4) + t.key[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t.key[1])
v1 -= ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
v0 -= ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
}
repack(dst, v0, v1)
}
@ -161,9 +159,9 @@ func NewTeaCipher(key []byte) *TEA {
return nil
}
t := new(TEA)
t.key[3] = binary.BigEndian.Uint32(key[12:])
t.key[2] = binary.BigEndian.Uint32(key[8:])
t.key[1] = binary.BigEndian.Uint32(key[4:])
t.key[0] = binary.BigEndian.Uint32(key[0:])
t[3] = binary.BigEndian.Uint32(key[12:])
t[2] = binary.BigEndian.Uint32(key[8:])
t[1] = binary.BigEndian.Uint32(key[4:])
t[0] = binary.BigEndian.Uint32(key[0:])
return t
}

View File

@ -6,10 +6,9 @@ import (
"compress/zlib"
binary2 "encoding/binary"
"encoding/hex"
"fmt"
"github.com/Mrs4s/MiraiGo/utils"
"io"
"net"
"strings"
)
func ZlibUncompress(src []byte) []byte {
@ -47,24 +46,40 @@ func GZipUncompress(src []byte) []byte {
}
func CalculateImageResourceId(md5 []byte) string {
return strings.ToUpper(fmt.Sprintf(
"{%s}.png", GenUUID(md5),
))
id := make([]byte, 36+6)[:0]
id = append(id, '{')
AppendUUID(id[1:], md5)
id = id[:37]
id = append(id, "}.png"...)
return utils.B2S(bytes.ToUpper(id))
}
func GenUUID(uuid []byte) string {
u := uuid[0:16]
buf := make([]byte, 36)
hex.Encode(buf[0:], u[0:4])
buf[8] = '-'
hex.Encode(buf[9:], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u[10:16])
return string(buf)
func GenUUID(uuid []byte) []byte {
return AppendUUID(nil, uuid)
}
func AppendUUID(dst []byte, uuid []byte) []byte {
_ = uuid[15]
if cap(dst) > 36 {
dst = dst[:36]
dst[8] = '-'
dst[13] = '-'
dst[18] = '-'
dst[23] = '-'
} else { // Need Grow
dst = append(dst, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"...)
}
hex.Encode(dst[0:], uuid[0:4])
hex.Encode(dst[9:], uuid[4:6])
hex.Encode(dst[14:], uuid[6:8])
hex.Encode(dst[19:], uuid[8:10])
hex.Encode(dst[24:], uuid[10:16])
return dst
}
func genUUID(uuid []byte, dst []byte) {
}
func ToIPV4Address(arr []byte) string {

View File

@ -173,7 +173,7 @@ func GenRandomDevice() {
rand.Read(r)
SystemDeviceInfo.Display = []byte("MIRAI." + utils.RandomStringRange(6, NumberRange) + ".001")
SystemDeviceInfo.FingerPrint = []byte("mamoe/mirai/mirai:10/MIRAI.200122.001/" + utils.RandomStringRange(7, NumberRange) + ":user/release-keys")
SystemDeviceInfo.BootId = []byte(binary.GenUUID(r))
SystemDeviceInfo.BootId = binary.GenUUID(r)
SystemDeviceInfo.ProcVersion = []byte("Linux version 3.0.31-" + utils.RandomString(8) + " (android-build@xxx.xxx.xxx.xxx.com)")
rand.Read(r)
t := md5.Sum(r)