mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
perf(tea): use runtime.fastrand
name old time/op new time/op delta TEAen/16-8 242ns ± 1% 225ns ± 2% -6.98% (p=0.008 n=5+5) TEAen/256-8 1.71µs ± 0% 1.68µs ± 0% -1.58% (p=0.008 n=5+5) TEAen/4K-8 25.0µs ± 1% 25.0µs ± 0% ~ (p=1.000 n=5+5) TEAen/32K-8 202µs ± 1% 202µs ± 0% ~ (p=0.548 n=5+5) TEAde/16-8 208ns ± 0% 207ns ± 0% ~ (p=0.198 n=5+5) TEAde/256-8 1.65µs ± 0% 1.64µs ± 0% -0.39% (p=0.048 n=5+5) TEAde/4K-8 24.6µs ± 0% 24.6µs ± 1% ~ (p=1.000 n=5+5) TEAde/32K-8 199µs ± 0% 199µs ± 0% ~ (p=0.905 n=4+5) name old speed new speed delta TEAen/16-8 66.2MB/s ± 1% 71.2MB/s ± 2% +7.51% (p=0.008 n=5+5) TEAen/256-8 150MB/s ± 0% 152MB/s ± 0% +1.61% (p=0.008 n=5+5) TEAen/4K-8 164MB/s ± 1% 164MB/s ± 0% ~ (p=1.000 n=5+5) TEAen/32K-8 162MB/s ± 1% 162MB/s ± 0% ~ (p=0.548 n=5+5) TEAde/16-8 154MB/s ± 0% 154MB/s ± 0% ~ (p=0.222 n=5+5) TEAde/256-8 165MB/s ± 0% 165MB/s ± 0% ~ (p=0.056 n=5+5) TEAde/4K-8 167MB/s ± 0% 167MB/s ± 1% ~ (p=1.000 n=5+5) TEAde/32K-8 165MB/s ± 0% 165MB/s ± 0% ~ (p=0.825 n=4+5)
This commit is contained in:
parent
ec4cd4a6f3
commit
ec053573f1
@ -2,11 +2,15 @@ package binary
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
_ "unsafe" // required by go:linkname
|
||||
)
|
||||
|
||||
type TEA [4]uint32
|
||||
|
||||
// Uint32 returns a lock free uint32 value.
|
||||
//go:linkname Uint32 runtime.fastrand
|
||||
func Uint32() uint32
|
||||
|
||||
// Encrypt tea 加密
|
||||
// http://bbs.chinaunix.net/thread-583468-1-1.html
|
||||
// 感谢xichen大佬对TEA的解释
|
||||
@ -14,7 +18,9 @@ func (t TEA) Encrypt(src []byte) (dst []byte) {
|
||||
lens := len(src)
|
||||
fill := 10 - (lens+1)%8
|
||||
dst = make([]byte, fill+lens+7)
|
||||
_, _ = rand.Read(dst[0:fill])
|
||||
binary.LittleEndian.PutUint32(dst, Uint32())
|
||||
binary.LittleEndian.PutUint32(dst[4:], Uint32())
|
||||
binary.LittleEndian.PutUint32(dst[8:], Uint32())
|
||||
dst[0] = byte(fill-3) | 0xF8 // 存储pad长度
|
||||
copy(dst[fill:], src)
|
||||
|
||||
@ -36,14 +42,13 @@ func (t TEA) Decrypt(data []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
dst := make([]byte, len(data))
|
||||
var iv1, iv2, holder, tmp uint64
|
||||
var iv1, iv2, holder uint64
|
||||
for i := 0; i < len(dst); i += 8 {
|
||||
block := binary.BigEndian.Uint64(data[i:])
|
||||
tmp = t.decode(block ^ iv2)
|
||||
iv2 = tmp
|
||||
holder = tmp ^ iv1
|
||||
iv1 = block
|
||||
binary.BigEndian.PutUint64(dst[i:], holder)
|
||||
iv1 = binary.BigEndian.Uint64(data[i:])
|
||||
iv2 ^= iv1
|
||||
iv2 = t.decode(iv2)
|
||||
binary.BigEndian.PutUint64(dst[i:], iv2^holder)
|
||||
holder = iv1
|
||||
}
|
||||
return dst[dst[0]&7+3 : len(data)-7]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user