diff --git a/rand_1.21.go b/rand_1.21.go new file mode 100644 index 0000000..dc678e5 --- /dev/null +++ b/rand_1.21.go @@ -0,0 +1,19 @@ +//go:build !go1.22 + +package tea + +import ( + _ "unsafe" // required by go:linkname +) + +// randuint32 returns a lock free uint32 value. +// +// Too much legacy code has go:linkname references +// to runtime.fastrand and friends, so keep these around for now. +// Code should migrate to math/rand/v2.Uint64, +// which is just as fast, but that's only available in Go 1.22+. +// It would be reasonable to remove these in Go 1.24. +// Do not call these from package runtime. +// +//go:linkname randuint32 runtime.fastrand +func randuint32() uint32 diff --git a/rand_1.22.go b/rand_1.22.go new file mode 100644 index 0000000..f2a802d --- /dev/null +++ b/rand_1.22.go @@ -0,0 +1,11 @@ +//go:build go1.22 + +package tea + +import ( + "math/rand/v2" +) + +func randuint32() uint32 { + return rand.Uint32() +} diff --git a/tea.go b/tea.go index 08d8141..2754482 100644 --- a/tea.go +++ b/tea.go @@ -4,15 +4,10 @@ package tea import ( "encoding/binary" - _ "unsafe" // required by go:linkname ) type TEA [4]uint32 -// randuint32 returns a lock free uint32 value. -//go:linkname randuint32 runtime.fastrand -func randuint32() uint32 - //go:nosplit func NewTeaCipher(key []byte) (t TEA) { if len(key) == 16 { diff --git a/tea_test.go b/tea_test.go index f3e8809..4ec66f8 100644 --- a/tea_test.go +++ b/tea_test.go @@ -1,6 +1,3 @@ -//go:build ignore -// +build ignore - package tea import ( @@ -8,8 +5,6 @@ import ( "crypto/rand" "encoding/hex" "testing" - - "github.com/Mrs4s/MiraiGo/utils" ) var testTEA = NewTeaCipher([]byte("0123456789ABCDEF")) @@ -32,7 +27,7 @@ var sampleData = func() [][3]string { } for i := range out { c, _ := hex.DecodeString(out[i][ENC]) - out[i][ENC] = utils.B2S(c) + out[i][ENC] = string(c) } return out }() @@ -40,13 +35,13 @@ var sampleData = func() [][3]string { func TestTEA(t *testing.T) { // Self Testing for _, sample := range sampleData { - tea := NewTeaCipher(utils.S2B(sample[KEY])) - dat := utils.B2S(tea.Decrypt(utils.S2B(sample[ENC]))) + tea := NewTeaCipher([]byte(sample[KEY])) + dat := string(tea.Decrypt([]byte(sample[ENC]))) if dat != sample[DAT] { t.Fatalf("error decrypt %v %x", sample, dat) } - enc := utils.B2S(tea.Encrypt(utils.S2B(sample[DAT]))) - dat = utils.B2S(tea.Decrypt(utils.S2B(enc))) + enc := string(tea.Encrypt([]byte(sample[DAT]))) + dat = string(tea.Decrypt([]byte(enc))) if dat != sample[DAT] { t.Fatal("error self test", sample) }